首页 文章

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

提问于
浏览
1

我在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版本或任何其他问题的过时代码 .

我怎样才能做到这一点?

谢谢 .

1 回答

  • 4

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

    此答案的全功能测试和更正代码位于另一个主题:装运方法 - 当隐藏平率时,本地提货选项不可用


    重要提示:自WC版本2.1以来,不推荐使用woocommerce_available_shipping_methods钩子,您需要使用woocommerce_package_rates过滤钩子 .

    WooCommerce version 2.6+ 介绍 NEW shipping zones . 因此,与运费相关的所有WooCommerce先前版本的代码均为 outdatedwill not work anymore .

    有关信息:全球$ woocommerce; **与$ woocommerce->购物车已被WC() - >购物车取代 . 您将使用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;
    }
    

    在添加代码段之前,请确保清除WooCommerce缓存(WooCommerce>系统状态>工具> WC瞬态>清除瞬态),因为运输方法已缓存 .

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

    这段代码应该有效 if you have correctly set your shipping zones…


    参考文献:

相关问题