隐藏仅针对Woocommerce中产品类别的统一费率送货 [英] Hide Flat Rate shipping exclusively for a product category in Woocommerce

查看:87
本文介绍了隐藏仅针对Woocommerce中产品类别的统一费率送货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的扩展:删除WooCommerce 2.6和3+中特定类别的送货统一费率方法。

已将Woo Smart Coupons插件用于礼品卡产品。必须将其设置为Variation,因为我们有多个层次可供选择。 (这排除了虚拟产品)礼品卡具有自己的区分类别。我们设置了两种送货方式:统一费率+当地取件。拥有发送到您的收件箱的礼品卡的运送选项非常可笑,因此我使用了在上面的链接中找到的以下代码段:

Used the Woo Smart Coupons plugin for a Gift Card product. This HAS to be set to Variation, as we have multiple tiers to select from. (this rules out virtual products) The Gift Card has it's own category for distinction. We have two shipping options set up: Flat Rate + Local Pickup. It's pretty silly to have shipping options for a gift card that gets sent to your Inbox, so I used the following snippet found in the link above:

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

工作就像一个吊饰……直到您添加了一个IS N'T从该类别。如果某人决定要礼品卡和普通产品,则需要重新使用常规的运送方式。

Works like a charm... until you add a product that ISN'T from that category. If someone decides that they want a Gift Card AND a normal product, then the regular shipping options need to be back in place.

编辑:选中的答案很完美!如果您想更改上述情况的商品的取件标签,以便它们说下载而不是取件,那么请在IF语句之后添加此行,以检查哪些产品与类别匹配

The checked answer works perfectly! If you want to change the Pickup Label for items like the above situation so they say something like "Download" instead of "Pickup", then add this line after the IF statement that checks which products are matching the categories

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


推荐答案

此处是使它适用于独特且排他的产品类别的正确方法,如果该产品类别中没有其他产品,则将完全隐藏固定费用:

Here is the correct way to make it work for a unique and exclusive product category, that will hide Flat rate exclusively if there is no others products remaining to this product category:

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文件中(或活动主题)。

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

This code is tested and fully functional (it will work if you have correctly set your shipping zones).


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

You might need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in Woocommerce shipping settings.

这篇关于隐藏仅针对Woocommerce中产品类别的统一费率送货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆