简体   繁体   English

当 WooCommerce 中提供免费送货时,仅禁用特定的统一费率送货方式

[英]Disable only specific flat rate shipping method when free shipping is available in WooCommerce

I have two flat rates on my WooCommerce site and I want to disable one of them, when free shipping is enabled.我的 WooCommerce 网站上有两个统一费率,当启用免费送货时,我想禁用其中一个。 I have a function which is working for all flat rates only.我有一个 function 仅适用于所有统一费率。

How do I check for an instance id on a shipping rate?如何检查运费中的实例 ID? Can anyone help me understand how to check for the instance id?谁能帮我理解如何检查实例ID?

I Was trying to echo shipping data out to make sense of what values were available but no echo was showing on the front end either so a tip on why would be great.我试图回显运输数据以了解可用的值,但前端也没有显示回显,因此提示为什么会很棒。

Here is my code attempt:这是我的代码尝试:

function hide_shipping_when_free_is_available( $rates, $package ) {
    $new_rates = array();

    foreach ( $rates as $rate_id => $rate ) {
        // Only modify rates if free_shipping is present.
        if ( 'free_shipping' === $rate->method_id ) {
            $new_rates[ $rate_id ] = $rate;
            break;
        }
    }

    if ( ! empty( $new_rates ) ) {
        //Save local pickup if it's present.
        foreach ( $rates as $rate_id => $rate ) {
            if ('local_pickup' === $rate->method_id ) {
                $new_rates[ $rate_id ] = $rate;
            }
            if ( 'flat_rate:20' === $rate->instance_id )
                $new_rates[ $rate_id ] = $rate; 
            }
        return $new_rates;
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

If you inspect the shipping methods radio buttons on cart or in checkout you will see something like:如果您检查购物车或结帐时的运输方式单选按钮,您将看到如下内容:

<input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate20" value="flat_rate:20" class="shipping_method" checked="checked">

So in value="flat_rate:20" (which is the rate Id) :所以在value="flat_rate:20" (这是速率 ID)

  • the method Id is flat_rate ,方法 IDflat_rate
  • and instance ID is 20 .实例 ID20

Note: The code could be really simplified…注意:代码可以真正简化......

But as you don't provide the free shipping and other flat rate rates IDs (or instances Ids) , I Keep your code making some changes to it, this way:但是由于您不提供免费送货和其他统一费率 ID (或实例 ID) ,我会保留您的代码对其进行一些更改,这样:

function hide_shipping_when_free_is_available( $rates, $package ) {
    $new_rates = array();

    foreach ( $rates as $rate_id => $rate ) {
        // Only modify rates if free_shipping is present.
        if ( 'free_shipping' === $rate->method_id ) {
            $new_rates[ $rate_id ] = $rate;
            break;
        }
    }

    if ( ! empty( $new_rates ) ) {
        foreach ( $rates as $rate_id => $rate ) {
            //Save local pickup if it's present.
            if ('local_pickup' === $rate->method_id ) {
                $new_rates[ $rate_id ] = $rate;
            }
            if ( 20 == $rate->instance_id )
                $new_rates[ $rate_id ] = $rate;
            }
        }
        return $new_rates;
    }
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

It should work.它应该工作。

You can check if free shipping is available based on its id.您可以根据其 ID 检查是否可以免费送货。 If it is available, it removes the shipping rate flat_rate:20 .如果可用,它会删除运费flat_rate:20

Replace free_shipping:2 with your free shipping id.free_shipping:2替换为您的免费送货 ID。

// if free shipping is available, disable a specific shipping rate
add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_rate_if_shipping_free_is_available', 10, 2 );
function hide_specific_shipping_rate_if_shipping_free_is_available( $rates, $package ) {
    if ( isset( $rates['free_shipping:2'] ) ) {
        unset( $rates['flat_rate:20'] );
    }
    return $rates;
}

The code has been tested and works.该代码已经过测试并且可以工作。 Add it to your active theme's functions.php.将其添加到您的活动主题的功能中。php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 Woocommerce 提供免费送货时,仅禁用统一费率送货方式 - Disable only flat rate shipping method when free shipping is available in Woocommerce 当 Woocommerce 提供免费送货服务时,隐藏运费统一费率” - Hide shipping Flat rate" when free shipping is available in Woocommerce 当 WooCommerce 提供免费送货时隐藏特定的送货方式 - Hide specific shipping method when Free Shipping is available in WooCommerce 在 Woocommerce 中为所选产品禁用特定的统一费率运输方式 - Disable a specific of flat rate shipping method for a selected product in Woocommerce 当 WooCommerce 3 中提供免费送货时,隐藏细节统一费率 - Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3 在woocommerce中将“统一费率”运输方式设置为默认值 - Set “flat rate” shipping method as default in woocommerce 在 WooCommerce 中为“统一费率”运输方式添加工具提示 - Add tooltip to “flat rate” shipping method in WooCommerce 隐藏WooCommerce中特定运输类别的所有统一费率运输方法 - Hide all Flat rate Shipping methods for specific shipping classes in WooCommerce 在 WooCommerce 中禁用特定优惠券代码的免费送货 - Disable free shipping for specific coupon codes in WooCommerce WooCommerce - 当免费送货可用时隐藏其他送货方式 - WooCommerce - Hide other shipping methods when FREE SHIPPING is available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM