首页 > 解决方案 > WooCommerce - 如果金额大于 x,则将每个运费设置为 ​​0(我有 90% 的工作片段但无法修改)

问题描述

我找到了一个几乎完美的片段,我觉得很傻,但无法修改它以满足我的需要。

它是什么:

if price is above 10--> shipping cost 4.5
if price is under 10--> shipping cost 2.5

我需要的:

if price is above 10000--> shipping cost 0 (and display 0 next to each shipping method if it's not too hard)
if price is under 10000 --> keep original pricing set in WooCommerce settings

如果你能帮助我,非常感谢!有一个非常愉快的一天!

https://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/

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

    $new_cost = ( WC()->cart->subtotal < 10 ) ? 4.5 : 2.5;

    foreach($rates as $key => $rate ) {
        $rates[$key]->cost = $new_cost;
    }

    return $rates;
}

标签: phpwordpresswoocommerce

解决方案


推荐阅读