2017-12-06 12 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は今、あなたがWordpressの条件付きの専用機能にhas_term()

を使用する必要があります私はあなたのコードを再訪していましたエラーを回避するには...

を非推奨フックwoocommerce_get_priceを交換していますもう一度試してみてください。

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

。素晴らしい作品です、ありがとうございました。 –

関連する問題