【问题标题】:Hide some shipping rates based on taxonomy terms in WooCommerce 4.8+根据 WooCommerce 4.8+ 中的分类术语隐藏一些运费
【发布时间】:2021-01-05 09:40:36
【问题描述】:

我用它来取消带有特定标签的产品的运费:

function specific_products_shipping_methods( $rates, $package ){
    // etiquette cocolis
    $terms = array( 'cocolis' );
    $taxonomy = 'product_tag';

    foreach( $package['contents'] as $cart_item ) {
        if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) )
        {
            unset( $rates['oik_weight_zone_shipping_49'] );
            unset( $rates['chrono10'] );
            unset( $rates['chrono13'] );
            add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 2);
        }
    }
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );

function reset_default_shipping_method( $method, $available_methods ) {
    $default_method = 'per_product';
    if( array_key_exists($method, $available_methods ) )
        return $default_method;
    else
        return $method;
}

这工作正常,直到更新 woo 4.8.0 知道有什么问题吗?

谢谢,

【问题讨论】:

    标签: php wordpress woocommerce taxonomy-terms shipping-method


    【解决方案1】:

    您的代码有一些错误,请尝试以下重新访问和优化的代码:

    add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
    function specific_products_shipping_methods( $rates, $package ){
    
        $terms     = array('cocolis'); // Array of term slugs, names or ids
        $taxonomy  = 'product_tag'; // WooCommerce product tag taxonomy
        $rate_keys = array('oik_weight_zone_shipping_49', 'chrono10', 'chrono13'); // Shipping rates to be hidden
        $has_unset = false; // Initializing
    
        // Loop through cart items for the current shipping package
        foreach( $package['contents'] as $cart_item ) {
            // Targeting specific product tags
            if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ) {
                // Loop through shipping rates to be removed (hidden)
                foreach( $rate_keys as $rate_key ) {
                    if (isset($rates[$rate_key]) ) {
                        unset($rates[$rate_key]);
                        $has_unset = true; // Flag as unset to enable the filter below
                    }
                }
            }
        }
        if ( $has_unset ) {
            add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 3 );
        }
        return $rates;
    }
    
    function reset_default_shipping_method( $default, $rates, $chosen_method ) {
        $targeted_method = 'per_product'; // The shipping method rate to set as chosen one
    
        if( isset($rates[$targeted_method]) ) {
            $default = $targeted_method;
        }
        return $default;
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。在 WooCommerce 4.8+ 中测试并运行。

    不要忘记清空您的购物车以刷新 WooCommerce 运输缓存

    注意:对于 WooCommerce 类别,请将 $taxonomy 变量定义为 product_cat

    【讨论】:

    • 感谢 Loic,但对我不起作用,我使用 Mondial 中继插件进行运输,重要的是我做了未设置的工作。如果我禁用 mondial 中继运输,我的第一个代码可以正常工作。还有你的代码,直到我重新激活 mondial 中继运输......
    • @ilanb 对我来说,它适用于使用 Storefront 主题的最新 WooCommerce 和 Wordpress 版本......所以在你的情况下,还有其他一些问题,比如你的主题、插件或由你。
    猜你喜欢
    • 1970-01-01
    • 2014-07-05
    • 2018-11-01
    • 2020-08-25
    • 2018-07-10
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    相关资源
    最近更新 更多