问题描述
我正在经营一个woocommerce商店,并使用统一运费 $ 15
.我写了一个公式,为每个其他项目添加 $ 1.25
.
I'm running a woocommerce shop and using a Flat Rate shipping $15
. I have written a formula to add $1.25
for each additional item.
为附加的每个项目使用统一费率设置| $ 1.25
:
Sipping "flat rate settings | $1.25
for Additional Each Item:
但是我想为每3个项目添加此费用 $ 1.25
.我的意思是3、6、9、12等...
But I want to add this cost $1.25
for every 3 items. I mean 3, 6, 9, 12 and so on...
谁能告诉我该怎么做?感谢您的帮助.
Can anyone tell me how to do this? Any help is appreciated.
推荐答案
更新 (2021)
以下代码将为每3件商品(3、6、9…) 增加统一费用运送方式的费用.
The following code will add an additional cost to flat rate shipping method each 3 items (3, 6, 9 …).
您需要用简单的初始费用代替您的公式来更改运输费用.
You will need to change your shipping cost with a simple initial cost instead of your formula.
代码(您将在其中设置额外的运费):
add_filter('woocommerce_package_rates', 'shipping_additional_cost_each_three_items', 10, 2);
function shipping_additional_cost_each_three_items( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE set your additional shipping cost
$additional_cost = 1.25;
$items_count = 0; // Initializing
$each_items = 3; // Number of items (for additional cost)
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ) {
$items_count = += $cart_item['quantity']; // Count cart items for current shipping package
}
if ( $items_count >= $each_items ) {
// Loop through the shipping taxes array
foreach ( $rates as $rate_key => $rate ){
// Targetting "flat rate"
if( 'flat_rate' === $rate->method_id ){
$initial_cost = $new_cost = $rate->cost;
$has_taxes = false; // Initializing
$taxes = array(); // Initializing
// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i+ = $each_items){
$new_cost += $additional_cost;
}
$rates[$rate_key]->cost = $new_cost; // Set the new cost
// Taxes rate cost (if any) - Loop through taxes array (as they can be many)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
// Get the initial tax cost
$initial_tax_cost = $new_tax_cost = $tax;
// 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 changes
}
}
// set array of shipping tax cost
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.
基于您的第二条评论的答案:
您将替换此块:
// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
$new_cost += $additional_cost;
}
通过以下方式:
// Adding to cost an additional fixed cost for the 2nd item
if($items_count >= 2){
$new_cost += 6.21;
}
// Adding to cost the additional cost each 3 items (3, 6, 9 …)
for($i = 0; $i <= $items_count; $i += $each_items){
$new_cost += $additional_cost;
}
这篇关于在Woocommerce中每3件商品的统一费用运送中增加额外的费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!