php - Apply different tax based on user role and product category (Woocommerce) -
i need apply different tax if user have specific role, in certains product's categories.
example: if customer role "vip" buy item of category "bravo" or "charlie" tax applied @ 4% instead 22%
this code in part wrote me part taken on google, don't understand wrong.
please can me?
function wc_diff_rate_for_user( $tax_class, $product ) { global $woocommerce; $lundi_in_cart = false; foreach ( $woocommerce->cart->get_cart() $cart_item_key => $values ) { $_product = $values['data']; $terms = get_the_terms( $_product->id, 'product_cat' ); foreach ($terms $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; }
taxes calculated per lines of items in cart.. don't have loop cart items. instead, check if current item has category looking for.
try this...
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 not vip, let's move on... if (!is_user_logged_in()) {return $tax_class;} // user not vip, let's move on... if (!current_user_can( 'vip' ) ) {return $tax_class;} // it's reduced rate, let's move on.. if ($tax_class == 'reduced rate') {return $tax_class;} // let's product category product... $terms = get_the_terms( $product->id, 'product_cat' ); foreach ( $terms $term ) { // checking each category // if it's 1 of category we'er looking if(in_array($term->term_id, array(81,82))) { $tax_class = 'reduced rate'; // found it... no need check other $term break; } } return $tax_class; }
Comments
Post a Comment