【问题标题】:Disable only specific flat rate shipping method when free shipping is available in WooCommerce当 WooCommerce 中提供免费送货时,仅禁用特定的统一费率送货方式
【发布时间】:2021-03-10 19:34:26
【问题描述】:

我的 WooCommerce 网站上有两个统一费率,我想在启用免费送货时禁用其中一个。我有一个仅适用于所有统一费率的功能。

如何查看运费中的实例 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 ) ) {
        //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 );

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce shipping-method


    【解决方案1】:

    如果您检查购物车或结帐时的送货方式单选按钮,您会看到如下内容:

    <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">
    

    所以在value="flat_rate:20"(这是速率Id)

    • 方法 IDflat_rate
    • 并且实例 ID20

    注意:代码真的可以简化……

    但由于您不提供免费送货和其他统一费率费率 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 );
    

    它应该可以工作。

    【讨论】:

    • 完美!非常感谢您的快速答复!赞赏:)
    【解决方案2】:

    您可以根据其 id 检查是否可以免费送货。如果可用,它会删除运费flat_rate:20

    用您的免费送货 ID 替换 free_shipping:2

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

    代码已经过测试并且可以运行。将其添加到您的活动主题的 functions.php 中。

    【讨论】:

      猜你喜欢
      • 2019-05-31
      • 2018-08-09
      • 1970-01-01
      • 2020-06-22
      • 2018-03-05
      • 2015-08-14
      • 1970-01-01
      • 2016-11-11
      相关资源
      最近更新 更多