【问题标题】:How do I disable shipping in some of my woocommerce products如何在我的某些 woocommerce 产品中禁用运输
【发布时间】:2016-12-03 15:07:47
【问题描述】:

我有一家 WooCommerce 在线商店,可为大多数产品提供送货服务。部分产品供本地取货。我尝试在成本为零的运输区域上设置一个类别,并在产品上分配类别。但到目前为止,结帐仍然显示运费。有什么方法可以让部分产品免运费?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    如果您正在寻找插件解决方案,请尝试WooCommerce Conditional Shipping and Payments。通过使用此插件,您可以对某些产品或产品类别添加限制。

    【讨论】:

    • 谢谢,我去看看
    【解决方案2】:

    您可能想要查看woocommerce_package_rates 过滤器,它允许您过滤客户可用的一组运输选项。一个例子是这样的:

    <?php
    // add this snippet to functions.php:
    add_filter( 'woocommerce_package_rates', function ( $rates, $package ) {
        // examine $package for products. this could be a whitelist of specific 
        // products that you wish to be treated in a special manner...
        $special_ids             = array( 1, 2, 3, 4, 5 );
        $special_product_present = false;
        foreach ( $package['contents'] as $line_item ) {
            if ( in_array( $line_item['product_id'], $special_ids ) ) {
                $special_product_present = true;
            }
        }
    
        $rates = array_filter( $rates, function ( $r ) use ( $special_product_present ) {
            // do some logic here to return true (for rates that you wish to be displayed), or false.
            // example: only allow shipping methods that start with "local"
            if ( $special_product_present ) {
                return preg_match( '/^local/', strtolower( $r->label ) );
            } else {
                return true;
            }
        } );
    
        return $rates;
    }, 10, 2 );
    

    blog post here 展示了使用此钩子对该想法的一些变体,包括如何根据购物车价值、客户所在国家/地区、购物车中的商品数量等自定义可用费率。这是源代码:@987654322 @

    【讨论】:

    • 谢谢,我会考虑你的建议。
    猜你喜欢
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2018-07-25
    • 2021-10-26
    相关资源
    最近更新 更多