php - Woocommerce add or hide shipping method based on category -
i want remove or add shipping options depending on category of product in cart. example: have 1 product category x , want add shipping-method-x cart. , if have 1 product category x , 1 category, shipping-method-x disabled. function below checks if array contains category id=49 , if there category, doesn't work. nothing happens in cart :( please help
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' , 10, 1 ); function check_cart_for_share() { global $woocommerce; $cart = $woocommerce->cart->cart_contents; $found = false; foreach ($woocommerce->cart->cart_contents $key => $values ) { $terms = get_the_terms( $values['product_id'], 'product_cat' ); $tmp = array(); foreach ($terms $term) { array_push($tmp, $term->term_id); } array_unique($tmp); if (sizeof($tmp) > 1 , in_array('49', $tmp)) { $found = true; } } return $found; } function hide_shipping_based_on_tag( $available_methods ) { // use function above check cart categories. if ( check_cart_for_share() ) { // remove method want unset( $available_methods['flat_rate'] ); } // return available methods without 1 unset. return $available_methods; }
likely product in single category id of 49 makes if statement invalid. should check 0 or more entries.
if (sizeof($tmp) > 0 && in_array('49', $tmp)) { \\... in array
Comments
Post a Comment