2017-12-06 17 views
1

슬러그가 performance_customer 인 맞춤형 사용자 역할을 설정했습니다. 현재 사용자가 "성과 고객"인지 확인하고 특정 카테고리의 제품에 특정 가격 할인을 적용 할 것인지 확인하고 있습니다.카트에 맞춤형 가격으로 제품을 추가 할 때 오류가 발생했습니다. - WooCommerce

여기 내 코드입니다 :

function return_custom_performance_dealer_price($price, $product) { 

    global $woocommerce; 
    global $post; 
    $terms = wp_get_post_terms($post->ID, 'product_cat'); 
    foreach ($terms as $term) $categories[] = $term->slug; 

    $origPrice = get_post_meta(get_the_ID(), '_regular_price', true); 
    $price = $origPrice; 

    //check if user role is performance dealer 
    $current_user = wp_get_current_user(); 
    if(in_array('performance_customer', $current_user->roles)){ 
     //if is category performance hard parts 
     if(in_array('new-hard-parts-150', $categories)){ 
      $price = $origPrice * .85; 
     } 
     //if is category performance clutches 
     elseif(in_array('performance-clutches-and-clutch-packs-150', $categories)){ 
      $price = $origPrice * .75; 
     } 
     //if is any other category 
     else{ 
      $price = $origPrice * .9; 
     } 
    } 
    return $price; 
} 
add_filter('woocommerce_get_price', 'return_custom_performance_dealer_price', 10, 2); 

기능은 제품 루프에서 완벽하게 작동하지만, 나는 그것이 폭발 장바구니에 제품을 추가 if(in_array('CATEGORY_NAME_HERE', $categories)){을 포함하는 각 줄을 나에게이 오류를 받았습니다.

Error: Warning: in_array() expects parameter 2 to be array, null given in…

나는 이것이 내가 각 제품이 속한 카테고리의 배열을 형성 wp_get_post_terms()를 사용하는 위의 코드의 5 라인과 관련이있다 같은데요. 이 작업을 수행하는 방법을 잘 모르겠습니다.

답변

1

첫째, woocommerce_product_get_price 지금 후크 woocommerce_get_price에게 사용되지 않는 대체 필터 후크 ...

당신이 워드 프레스 조건 전용 기능을 사용해야있어 오류를 방지하기 위해 has_term()

난 당신의 코드를 재 방문했다 조금만 시도해주세요. 대신 다음을 시도하십시오.

add_filter('woocommerce_product_get_price', 'return_custom_performance_dealer_price', 10, 2); 
function return_custom_performance_dealer_price($price, $product) { 

    $price = $product->get_regular_price(); 

    //check if user role is performance dealer 
    $current_user = wp_get_current_user(); 
    if(in_array('performance_customer', $current_user->roles)){ 

     //if is category performance hard parts 
     if(has_term('new-hard-parts-150', 'product_cat', $product->get_id())){ 
      $price *= .85; 
     } 
     //if is category performance clutches 
     elseif(has_term('performance-clutches-and-clutch-packs-150', 'product_cat', $product->get_id())){ 
      $price *= .75; 
     } 
     //if is any other category 
     else{ 
      $price *= .9; 
     } 
    } 
    return $price; 
} 

코드는 활성 자녀 테마 (또는 테마)의 function.php 파일 또는 모든 플러그인 파일에 있습니다.

이 WooCommerce 3 + 테스트 완료 ... 그것은 ... 지금이었다

+1

을 작동합니다. 위대한 작품, 고마워요. –