【问题标题】:Hide Flat Rate shipping exclusively for a product category in Woocommerce专门为 Woocommerce 中的产品类别隐藏统一运费
【发布时间】:2018-11-01 16:45:48
【问题描述】:

这是这个问题的延伸:Remove shipping Flat Rate method for particular Category in WooCommerce 2.6 and 3+

将 Woo Smart Coupons 插件用于礼品卡产品。这必须设置为 Variation,因为我们有多个层可供选择。 (这排除了虚拟产品)礼品卡有自己的区分类别。我们设置了两种运输选项:统一费率 + 本地取货。为发送到您的收件箱的礼品卡提供运输选项非常愚蠢,因此我使用了上面链接中的以下 sn-p:

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 = 'coupons-gift-cards';
$prod_cat = false;

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;
}

像魅力一样工作...直到您添加不属于该类别的产品。如果有人决定他们想要礼品卡和普通产品,则需要恢复常规运输选项。

编辑:选中的答案完美无缺!如果您想更改类似上述情况的商品的取货标签,以便他们说“下载”而不是“取货”,然后在检查哪些商品与类别匹配的 IF 语句之后添加此行

foreach( $rates as $rate_key => $rate ) {
//change local_pickup:1 to your shipping method
    if ( 'local_pickup:1' == $rate_key){
//set the text for the label
        $rates[$rate_key]->label = __( 'Download', 'woocommerce' );
    }
}

【问题讨论】:

    标签: php wordpress woocommerce shipping custom-taxonomy


    【解决方案1】:

    这是使其适用于独特和独家产品类别的正确方法,如果该产品类别中没有其他产品,它将专门隐藏统一费率:

    add_filter( 'woocommerce_package_rates', 'hide_shipping_flat_rate_conditionaly', 90, 2 );
    function hide_shipping_flat_rate_conditionaly( $rates, $package ){
    
        // HERE Define your product category (ID, slug or name or an array of values)
        $term   = 'coupons-gift-cards';
        $others = $found = false;
    
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product_id = $cart_item['product_id'];
            if ( has_term( $term, 'product_cat', $cart_item['product_id'] ) )
                $found = true;
            else
                $others = true;
        }
    
        if ( $found && ! $others ) {
            foreach($rates as $rate_id => $rate) {
                if ('flat_rate' === $rate->method_id )
                    unset($rates[ $rate_id ]);
            }
        }
        return $rates;
    }
    

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

    此代码已经过测试并且功能齐全(如果您正确设置了运输区域,它将起作用)

    您可能需要刷新运输缓存数据:在 Woocommerce 运输设置中禁用、保存和启用、保存当前运输区域的相关运输方式。

    【讨论】:

    • 嗯,由于某种原因,它不适用于“其他”产品添加。我重新保存了所有的运输设置,清除了我的临时缓存,并且我还在分配了“提货”和“统一费率”的运输区域内运行了我的运输计算器,所以我认为我的设置不是问题。 ..我有什么遗漏吗?
    • @Nalakira 好的,找到了罪魁祸首……我只是忘记在某处设置一个重命名的变量……对不起。代码已更新。请再试一次。
    • 完美!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2019-09-04
    • 2018-07-18
    • 2022-12-10
    • 2017-01-19
    相关资源
    最近更新 更多