Remove shipping Flat Rate method for particular Category in WooCommerce 2.6 and 3+

后端 未结 1 1820
Happy的楠姐
Happy的楠姐 2020-12-11 11:47

I need help in woocommerce shipping options, I want to hide flat rate for a particular product category, where I only want to show local delivery or local pickup options.

1条回答
  •  粉色の甜心
    2020-12-11 12:48

    Update (Compatible with WC 3+ and works with variable products too)

    The fully functional tested and corrected code for this answer is located on another thread:
    Shipping methods - Local Pickup option not available when Flat Rate is hidden


    Important:
    woocommerce_available_shipping_methods hook is deprecated since WC version 2.1+ and you will need to use instead woocommerce_package_rates filter hook.

    WooCommerce version 2.6+ introduce NEW shipping zones. So all WooCommerce prior version's code related to shipping rates is outdated and will not work anymore.

    For info:
    global $woocommerce;** with $woocommerce->cart has been replaced by WC()->cart.
    You will use instead WC()->cart->get_cart()

    We will not need anymore your custom conditional function, because there is already a conditional function that exist in WordPress, that you can target for WooCommerce product categories. This function accept the term ID, the term slug or the term name:

    has_term( 'your_category', 'product_cat', $post_id )
    

    So we can use has_term() conditional function in this code:

    add_filter( 'woocommerce_package_rates', 'conditional_hide_shipping_methods', 100, 2 );    
    function conditional_hide_shipping_methods( $rates, $package ){
    
        // Define/replace here your correct category slug (!)
        $product_category = 'your_category';
        $prod_cat = false;
    
        // Going through each item in cart to see if there is anyone of your category        
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product_id = $cart_item['product_id'];
            if ( has_term( $product_category, 'product_cat', $product_id ) ){
                $prod_cat = true;
            }
        }
    
        $rates_arr = array();
    
        if ( $prod_cat ) {
            foreach($rates as $key => $rate) {
                if ('free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id || 'local_delivery' === $rate->method_id) {
                    $rates_arr[ $rate_id ] = $rate;
                    break;
                }
            }
        }
        return !empty( $rates_arr ) ? $rates_arr : $rates;
    }
    

    Before adding the snippet, make sure you clear your WooCommerce cache (WooCommerce > System Status > Tools > WC Transients > Clear transients), as shipping methods are cached.

    This code goes on function.php file of your active child theme or theme.

    This code should work if you have correctly set your shipping zones…


    References:

    • Hide other shipping methods when FREE SHIPPING is available
    • WooCommerce - Hide other shipping methods when FREE SHIPPING is available
    • WooCommerce 2.6 - Hiding paid shipping when free shipping is triggered by reaching specific amount
    • WooCommerce Cart - Conditional Items categories validation
    • How to Setup Shipping Zones in WooCommerce 2.6

    0 讨论(0)
提交回复
热议问题