Woocommerce 3 中基于购物车总重量的运费 [英] Shipping cost based on cart total weight in Woocommerce 3

查看:34
本文介绍了Woocommerce 3 中基于购物车总重量的运费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Woocommerce 网上商店中,我确实有不同的产品.我想根据购物车商品的总重量计算运费:

In my Woocommerce Webshop I do have different Products. I would like to have shipping cost calculated on total cart items weight:

  • 06公斤的成本是5 €
  • 612 公斤的成本是 9 €
  • from 0 to 6 Kilos the cost is 5 €,
  • from 6 to 12 Kilos the cost is 9 €

实际上,如果我有一个 1 公斤的产品,运费是 5 €,但如果我有这个产品的 10 件商品在我的购物篮中,运费仍然是 5 € (应该是 9 € 而不是).

Actually if I have a Product which is 1 Kilo the shipping cost is 5 €, but if I have 10 items of this product in my basket, the shipping fee is still 5 € (and it should be 9 € instead).

如何根据购物车总重量计算累进运费?

How can I have a progressive shipping cost based on cart total weight?

推荐答案

试试下面的函数,Flat Rate"的费用会根据购物车的总重量改变.

Try the following function which "Flat Rate" cost will be changed based on cart total weight.

使用统一费率"运输方式,您需要使用简单的初始成本而不是任何公式来设置参考运输成本.例如,它可以是 1.此费用将由我的答案代码替换,根据购物车总重量动态变化.

Using "Flate rate" shipping method, you will need to set a reference shipping cost with a simple initial cost instead of any formula. It can be for example 1. This cost will be replaced by my answer code, dynamically based on cart total weight.

您可能需要在运输选项"标签下的一般运输设置中启用调试模式",禁用临时运输缓存.

You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

add_filter('woocommerce_package_rates', 'shipping_cost_based_on_weight', 12, 2);
function shipping_cost_based_on_weight( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    // HERE define the differents costs
    $cost1 = 5; // Up to 6 Kg
    $cost2 = 9; // Above 6 Kg and below 12 kg
    $cost3 = 9; // Above 12 kg

    // The cart total Weight
    $total_weight = WC()->cart->get_cart_contents_weight();

    // Loop through the shipping taxes array
    foreach ( $rates as $rate_key => $rate ){
        $has_taxes = false;
        // Targetting "flat rate"
        if( 'flat_rate' === $rate->method_id ){
            // Get the initial cost
            $initial_cost = $new_cost = $rates[$rate_key]->cost;

            // Calculate new cost
            if( $total_weight <= 6 ) { // Below 6 Kg
                $new_cost = $cost1;
            }
            elseif( $total_weight > 6 && $total_weight <= 12 ) { // Between 6 and 12 Kg
                $new_cost = $cost2;
            }
            else { // Above 12 Kg
                $new_cost = $cost3;
            }

            // Set the new cost
            $rates[$rate_key]->cost = $new_cost;

            // Taxes rate cost (if enabled)
            $taxes = [];
            // Loop through the shipping taxes array (as they can be many)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $rates[$rate_key]->taxes[$key] > 0 ){
                    // Get the initial tax cost
                    $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
                    // Get the tax rate conversion
                    $tax_rate    = $initial_tax_cost / $initial_cost;
                    // Set the new tax cost
                    $taxes[$key] = $new_cost * $tax_rate;
                    $has_taxes   = true; // Enabling tax
                }
            }
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

测试后,不要忘记在运输设置中禁用启用调试模式"选项.

Once tested, don't forget to disable "Enable debug mode" option in shipping settings.

这篇关于Woocommerce 3 中基于购物车总重量的运费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆