2016-01-27 3 views
4

Мне нужен другой налог, если у пользователя есть определенная роль, но только в категориях категорий сертификатов.Применять различные налоги в зависимости от роли пользователя и категории продукта (Woocommerce)

Пример: если клиент А с ролью «Vip» купить товар категории «Браво» или «Чарли» налог применяется будет на уровне 4% вместо 22%

Это код в часть пишет мной другая часть взята на google, но я не понимаю, где я ошибаюсь.

Пожалуйста, кто-нибудь может мне помочь?

function wc_diff_rate_for_user($tax_class, $product) { 
    global $woocommerce; 

    $lundi_in_cart = false; 

    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
     $_product = $values['data']; 
     $terms = get_the_terms($_product->id, 'product_cat'); 

      foreach ($terms as $term) { 
       $_categoryid = $term->term_id; 
      } 
       if (($_categoryid === 81) || ($_categoryid === 82))) { 

        if (is_user_logged_in() && current_user_can('VIP')) { 
         $tax_class = 'Reduced Rate'; 
        } 
       } 
    } 

    return $tax_class; 
} 
+0

ты пытался, как это http://stackoverflow.com/questions/17866674/role-based-taxes-in-woocommerce –

+0

что не работает? – Reigel

ответ

1

Налоги рассчитываются на строки позиций в корзине. Вам не нужно зацикливать корзину. Вместо этого проверьте, соответствует ли текущий элемент той категории, которую вы ищете.

попробовать, как это ...

add_filter('woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2); 
function wc_diff_rate_for_user($tax_class, $product) { 

    // not logged in users are not VIP, let's move on... 
    if (!is_user_logged_in()) {return $tax_class;} 

    // this user is not VIP, let's move on... 
    if (!current_user_can('VIP')) {return $tax_class;} 

    // it's already Reduced Rate, let's move on.. 
    if ($tax_class == 'Reduced Rate') {return $tax_class;} 

    // let's get all the product category for this product... 
    $terms = get_the_terms($product->id, 'product_cat'); 
    foreach ($terms as $term) { // checking each category 
     // if it's one of the category we'er looking for 
     if(in_array($term->term_id, array(81,82))) { 
      $tax_class = 'Reduced Rate'; 
      // found it... no need to check other $term 
      break; 
     } 
    } 

    return $tax_class; 
} 
+0

@ WillyVB91 Я ввел исправления во второй оператор 'if' ... – Reigel