送货方式-隐藏统一费用后本地提货选项不可用 [英] Shipping methods - Local Pickup option not available when Flat Rate is hidden

查看:83
本文介绍了送货方式-隐藏统一费用后本地提货选项不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此回答(下面的代码) ,我可以成功地从特定的产品类别本地交付中隐藏统一费率 >选项可用。

Based on this answer (code below), I successfully can hide flat rate from particular product category and local delivery option is available. This is working perfect.

问题:该特定类别的本地拾音器选项不可用。

The problem: local pickup option is NOT available for that particular category.

如何使该特殊类别的本地取件选项可用?

How can I make the local pickup option available to this special category?

这是我的代码使用:

function custom_shipping_methods( $rates ){

    // Define/replace here your correct category slug (!)
    $cat_slug = 'your_category_slug';
    $prod_cat = false;

    // Going through each item in cart to see if there is anyone of your category        
    foreach ( WC()->cart->get_cart() as $values ) {
        $item = $values['data'];

        if ( has_term( $cat_slug, 'product_cat', $item->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;
}
add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 100);

另一件事:是否可以显示本地投放

One more thing: Is it possible to show local delivery and local pickup for that special category depending on location?

当前在我的商店中是本地取件还是送货? strong>仅针对一个位置设置。

Currently in my store local Pickup or Delivery is setup only for one location.

推荐答案


建议: 仅适用于WooCommerce 2.6.x版(增加了对WC 3+的兼容性)

经过多次测试……您将需要更改代码中的2个小东西:

After many tests… You will need to change 2 little things in your code:

add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 100, 2 );
function custom_shipping_methods( $rates, $package ){

    // Define/replace here your correct category slug (!)
    $cat_slug = 'posters';
    $prod_cat = false;

    // Going through each item in cart to see if there is anyone of your category
    foreach ( WC()->cart->get_cart() as $values ) {
        $product = $values['data'];

        // compatibility with WC +3
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        if ( has_term( $cat_slug, 'product_cat', $product_id ) )
            $prod_cat = true;
    }

    $rates_arr = array();

    if ( $prod_cat ) {
        foreach($rates as $rate_id => $rate) { // <== There was a mistake here

            if ('free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id || 'local_delivery' === $rate->method_id) {
                $rates_arr[ $rate_id ] = $rate;
                // break; // <========= Removed this to avoid stoping the loop
            }
        }
    }
    return !empty( $rates_arr ) ? $rates_arr : $rates;
}

有2个错误:


  • 在foreach循环中有一个变量名被我替换了。

  • 已删除 break; 避免在一个条件匹配时停止foreach循环。

  • One in the foreach loop with a bad variable name that I have replace it.
  • Removed break; avoiding stoping the foreach loop when one condition match.

此代码位于您活动的子主题或主题的function.php文件上。

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


您需要刷新运送的缓存数据:在woocommerce运送设置中禁用,保存和启用并保存当前运送区域的相关运送方法。

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






引用:


References:

  • Hide other shipping methods when FREE SHIPPING is available
  • WooCommerce - Hide other shipping methods when FREE SHIPPING is available

这篇关于送货方式-隐藏统一费用后本地提货选项不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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