在 WooCommerce 2.6 和 3+ 中删除特定类别的运输统一费率方法

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

我需要有关 woocommerce 运输选项的帮助,我想隐藏特定产品类别的统一费率,我只想显示本地交付或本地取货选项。

对于所有其他类别,所有选项都应该有效。

我已尝试使用该代码(添加到我的主题的 function.php 文件中):

function cart_has_product_with_orange_cats() {

    global $woocommerce;
    $product_in_cart = false;

    // start of the loop that fetches the cart items
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_cat' );
        // second level loop search, in case some items have several categories

        if($terms){

            foreach ($terms as $term) {
                $_categoryid = $term->term_id;

                if ( $_categoryid == 16 ) {
                    //category is in cart!
                    $product_in_cart = true;
                }
            }   
        }
    }
    return $product_in_cart;
}

// add filter and function to hide method

add_filter( 'woocommerce_available_shipping_methods', 'custom_shipping_methods' , 10, 1 );

function custom_shipping_methods( $available_methods ){

    if ( cart_has_product_with_orange_cats() ) {

        foreach($available_methods as $key => $method){
            if( $key == 'local_delivery' || $key == 'local_pickup'){
                continue;
            }
            unset($available_methods[$key]);

        }
        // remove the rate you want
    }
    // return the available methods without the one you unset.
    return $available_methods;
}

但它对我不起作用,可能是因为最新 WooCommerce 版本的代码过时或任何其他问题。

我怎样才能做到这一点?

谢谢。

更新 (与 WC 3+ 兼容,也适用于可变产品)

The fully functional tested and corrected code for this answer is located on another thread:


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 版本 2.6+ 引入新送货区。因此,所有与运费相关的 WooCommerce 先前版本的代码都是 过时的 并且 将不再起作用

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

我们将不再需要您的自定义条件函数,因为 WordPress 中已经存在一个条件函数,您可以将其定位到 WooCommerce 产品类别。此函数接受术语 ID、术语 slug 或术语名称:

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

所以我们可以在这段代码中使用has_term()条件函数:

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.

此代码在您的活动子主题或主题的 function.php 文件中。

此代码应该可以工作 如果您正确 set your shipping zones…


参考文献: