1

我需要找出一种方法来根据购物车上的商品计算 woocommerce 运费。如果购买 1-2 件商品我需要收取 120 美元,购买 3 件商品需要收取 180 美元。我添加了 4+ 的免费送货选项(基于美元)

我尝试将此添加到统一费率价格中:120+60([qty]-2) 它适用于除 1 项之外的所有情况,因为它收费 60 美元。

有什么想法吗?

4

1 回答 1

2

使用以下代码,您将能够获得此运费:
- 1 或 2 件:120 美元
- 3 件:180 美元
- 4 件或更多:免费送货(隐藏统一费率方法)

1)将以下代码添加到您的活动子主题(活动主题)的function.php文件中:

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

    $items_count  = WC()->cart->get_cart_contents_count();

    if( $items_count < 3 ){
        $cost_rate = 2;
    } else {
        $cost_rate = $items_count;
    }

    foreach ( $rates as $rate_key => $rate ){
        $taxes = [];
        $has_taxes = false;
        // Targeting "flat rate"
        if ( 'flat_rate' === $rate->method_id ) {
            // For 1, 2 or 3 items
            if ( $items_count <= 3 ) {
                $rates[$rate_key]->cost = $rate->cost * $cost_rate;

                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        $has_taxes = true;
                        $taxes[$key] = $tax * $cost_rate;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
            // For more than 3 hide Flat rate
            else {
                // remove flat rate method
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}

并保存……</p>

2) 在您的运输方式设置中,您需要设置60为“统一费率”成本并保存。

您需要保留“免费送货”方法的最低金额。

你完成了。测试和工作。

于 2018-11-13T17:46:40.613 回答