【问题标题】:Hide specific shipping method when Free Shipping is available in WooCommerce在 WooCommerce 中提供免费送货时隐藏特定的送货方式
【发布时间】:2020-06-22 14:47:41
【问题描述】:

我可以找到大量的代码 sn-ps 来在免费送货时隐藏其他送货选项,或者隐藏除本地取货之外的所有内容。我想隐藏除本地取件和我为升级交付设置的费率之外的所有内容(或者换句话说,我只想隐藏标准交付选项)。我弄乱了一些我找到的代码,但无法让它工作。

下面是我在 Github 上找到的一个代码示例,它隐藏了除免费送货和收集之外的所有内容,而且我知道我还想显示的送货方式 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;
                break;
            }
        }
        return $new_rates;
    }

    return $rates;
}

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

【问题讨论】:

  • 您的问题很困惑,也不是很清楚……您所说的“标准交付选项”是什么,并且在免费送货可用时您想隐藏它?这个“标准交付选项”的$rate->method_id 是什么(它是唯一的)吗?
  • 您好,我的标准配送选项是我设置的统一运费,我的升级配送是使用 Woo Table Rate 运费进行的。如果提供免费送货服务,客户无需查看标准费率。他们只需要查看免费送货、本地取货和升级(餐桌费)。我不介意代码是通过隐藏除这三个之外的所有代码,还是通过简单地隐藏标准统一费率来工作。如果我去我的升级(表率)网址显示 instance_id=9,我不知道这是否是正确的 method_id 或者它是否是唯一的,也不知道我将如何找到。

标签: php woocommerce shipping-method


【解决方案1】:

我知道这是一个迟到的答案,您可能已经找到了解决方案,但您可以这样做:

    $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 && 'your_shipping_method_id' !== $rate->method_id ) {
                $new_rates[ $rate_id ] = $rate;
            }
        }
        return $new_rates;
    }

    return $rates;
}

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2016-11-11
  • 1970-01-01
  • 2018-08-09
  • 2019-05-31
  • 1970-01-01
  • 2021-05-29
  • 2018-03-05
  • 2020-12-03
相关资源
最近更新 更多