• Hey! I want to remove or add some shipping options depending on the category of product that is in cart. For example: I have one product from category X and I want to add shipping-method-X to cart. And if I have one product from category X and one from another category, then shipping-method-X is disabled.

    For now I have this code:

    function check_product_in_cart($shipping_in_cart) {
    
    $shipping_NAME = 'flat_rate';
    
    global $woocommerce;
    
    foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    
    			$terms = get_the_terms( $values['product_id'], 'product_cat' );
    			$tmp = array();
    
    			foreach ($terms as $term) {
    
    				array_push($tmp, $term->term_id);
    			}
    
    			array_unique($tmp); // usuniecie duplikatow
    
    			if (sizeof($tmp) > 1 AND in_array('49', $tmp)) {
    				unset($shipping_in_cart[$shipping_NAME]);
    			}
    		}
    
    return $shipping_in_cart;
    }
    
    add_filter('woocommerce_shipping_methods','check_product_in_cart', 10, 1);

    Whats wrong with this code?

Viewing 1 replies (of 1 total)
  • Suggest that you also ask this question in the woocommerce support forums, thee is more relevant experience there.

Viewing 1 replies (of 1 total)
  • The topic ‘Woocommerce filtering shipping method in cart’ is closed to new replies.