送货方式 - 隐藏统一费率时,本地取件选项不可用

Shipping methods - Local Pickup option not available when Flat Rate is hidden

,我成功地隐藏了特定产品类别本地交付统一费率 =33=] 选项可用。这工作完美。

问题:本地取件选项对于该特定类别不可用

如何为这个特殊类别提供本地取件选项?

这是我使用的代码:

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

还有一件事:是否可以根据位置显示该特殊类别的本地送货本地取件

目前我的店铺本地取货送货只设置了一个地点。

Advice: ONLY for WooCommerce version 2.6.x (added compatibility for WC 3+)

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

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 循环。

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

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

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.


参考文献: