基于购物车项目数的WooCommerce运费 [英] WooCommerce shipping cost based on cart item count

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

问题描述

我想根据购物车上添加的产品数量来计算运输成本,

I want to count shipping cost based on number of products add on cart like,

如果我购买一部手机,那么它将计算运输成本为2.5,并且以后而不是我购买的两两部手机,则运费为5.0

If I purchase one mobile then it will count shipping cost as 2.5 and after more than two or two mobile I purchased then shipping cost will be 5.0

 <?php

      $qty(1) * 2.5 = 2.5

      $qty(2) * 2.5 = 5.0

      $qty(3) * 2.5 = 5.0

  ?>

那么有什么想法或建议如何根据产品数量计算运输成本?

So is there any idea or suggestion how to count the shipping cost based on number of products ?

推荐答案


已更新:

由于您的问题尚不清楚,您只需在固定费率中添加 [qty] * 2.5 运送方式费用(针对每个运送区域)(在wooCommerce运送设置中)。

As your question is a bit unclear, you could just need to add [qty]*2.5 in the Flat rate shipping method cost (for each shipping zone) in your wooCommerce shipping settings.

但如果您的购物车中有2个不同的商品,例如: item1(数量1)+ item2(数量1)

But it will not work if you have 2 different items in cart like: item1 (qty 1) + item2 (qty 1)

因此,此答案将在所有情况下都适用:

1 )首先,您需要为每个送货区域设置统一费率 送货方式,其费用将设置为 2.5 (在您的WooCommerce送货设置中)。

1) First you will need to set a "Flat rate" shipping method for each Shipping Zones which cost will be set to 2.5 (in your WooCommerce shipping settings).

2)添加此代码tha t将为每个购物车项目(基于项目总数)计算新的更新运输成本:

2) Adding this code that will calculate for each cart items (based on the total quantity of items) the new updated shipping cost:

add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{
    // The cart count (total items in cart)
    $cart_count = WC()->cart->get_cart_contents_count();
    $taxes = array();

    // If there is more than 1 cart item
    if( $cart_count > 1 ){
        // Iterating through each shipping rate
        foreach($rates as $rate_key => $rate_values){
            // Targeting "Flat Rate" shipping method
            if ( 'flat_rate' === $rate_values->method_id ) {
                // Set the new calculated rate cost
                $rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cumulated_active_quantity, 2);
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_id]->taxes as $key => $tax){
                    if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
                        $taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cumulated_active_quantity, 2 );
                        $has_taxes = true;
                    } else {
                        $has_taxes = false;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_id]->taxes = $taxes; 
            }
        }
    }
    return $rates;
}

代码包含在您活动的子主题的function.php文件中(

此代码已在WooCommerce 3+上进行了测试,并且可以正常工作

This code is tested on WooCommerce 3+ and works


您将需要刷新运输区域缓存:,禁用统一费率,然后保存。并启用该速率并保存。

You will need to refresh shipping zones caches: disabling the Flat rate, then save. And enabling back this rate and save.

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

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