首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅针对购物车项目禁用特定的传送方法,使用特定的传送类ID

仅针对购物车项目禁用特定的传送方法,使用特定的传送类ID
EN

Stack Overflow用户
提问于 2020-09-07 10:38:43
回答 1查看 40关注 0票数 1

从应答代码中,如果购物车中有另一项没有该配送类ID,并希望根据产品配送类再次显示flat_rate:2,该怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-07 21:57:36

您将使用以下方法:

代码语言:javascript
复制
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping_methods', 10, 2 );
function custom_hide_shipping_methods( $rates, $package ) {
    $found = $others = false; // Initializing
    $shipping_class_id  = 513; // <== ID OF YOUR SHIPPING_CLASS
    $shipping_rate_id   = 'flat_rate:2'; // <== Targeted shipping rate ID

    // Checking cart items for current package
    foreach( $package['contents'] as $key => $cart_item ) {
        $product = $cart_item['data']; // The WC_Product Object
        
        if( $product->get_shipping_class_id() == $shipping_class_id ) { 
            $found = true;
        } else {
            $others = true;
        }
    }
    
    if( $found && ! $others && isset($rates[$shipping_rate_id]) ) {
        unset($rates[$shipping_rate_id]); // Removing specific shipping method
    }

    return $rates;
}

代码在您的活动子主题(或活动主题)的functions.php文件中。应该行得通。

刷新运输缓存:

  1. 这段代码已经保存在functions.php文件中。

在传送区域设置中的

  1. ,禁用/保存任何传送方法,然后启用回/保存。

,您已经完成了,您可以测试它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63775928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档