首页 > 解决方案 > Woocommerce 数量范围运费

问题描述

我需要像这样设置运费:

1 - 5 个,60 美元 6 - 10 个,90 美元

我怎样才能做到这一点?因此,如果有人从产品中购买 3 件,则收取 60 件运费,如果 6 件或更多件,则收取 90 件运费。

woocommerce 提供的基本 [qty] 占位符无法做到这一点,也没有找到任何可以做到这一点的插件。

标签: wordpresswoocommerce

解决方案


也许是这样的?在您的活动主题 functions.php 文件中添加以下功能

add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 );
function custom_shipping_costs( $rates, $package ) {
    global $woocommerce;
    $qty =  $woocommerce->cart->cart_contents_count;
    //error_log($qty);
    foreach( $rates as $rate_key => $rate ){
        // Excluding free shipping methods
        if( $rate->method_id != 'free_shipping'){
            // Between 1 and 5
            if($qty > 0 && $qty <= 5):
                $rates[$rate_key]->cost = '60';
                // Between 6 and 10
            elseif($qty > 5 && $qty <= 10):
                $rates[$rate_key]->cost = '90';
            else:
                // For over 10
                $rates[$rate_key]->cost = '60';
            endif;
        }
    }
    return $rates;
}

推荐阅读