在 Woocommerce 中为每 3 件商品的统一运费增加额外费用
Add an additional cost to flat rate shipping each 3 items in Woocommerce
我是 运行 一家 woocommerce 商店,使用统一运费 </code>。我写了一个公式来为每个附加项目添加 <code>.25
。
13.50 + ( 1.25 * [qty])
啜饮“固定费率设置 | .25
额外的每个项目:
但我想为每 3 件商品添加这笔费用 .25
。我的意思是 3、6、9、12 等等...
谁能告诉我该怎么做?感谢任何帮助。
已更新 (2021)
以下代码将为每 3 件商品的固定费率运输方式增加额外费用 (3, 6, 9 …).
您需要使用简单的初始成本而不是您的公式来更改运费。
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_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 文件。已测试并有效。
Don't forget to disable "Enable debug mode" option in shipping settings.
根据您的第二条评论回答:
您将替换此块:
// 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;
}
<?php
$number = 13;
$all_number = 1;
for($i=1;$i<=13;$i++){
if ($i % 3 == 0){
$all_number = $all_number + 1;
}
}
$price_new_data = $old_price*$all_number;
$price = 13.50+($price_new_data*$product_quenty);
?>
我是 运行 一家 woocommerce 商店,使用统一运费 </code>。我写了一个公式来为每个附加项目添加 <code>.25
。
13.50 + ( 1.25 * [qty])
啜饮“固定费率设置 | .25
额外的每个项目:
但我想为每 3 件商品添加这笔费用 .25
。我的意思是 3、6、9、12 等等...
谁能告诉我该怎么做?感谢任何帮助。
已更新 (2021)
以下代码将为每 3 件商品的固定费率运输方式增加额外费用 (3, 6, 9 …).
您需要使用简单的初始成本而不是您的公式来更改运费。
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_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 文件。已测试并有效。
Don't forget to disable "Enable debug mode" option in shipping settings.
根据您的第二条评论回答:
您将替换此块:
// 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;
}
<?php
$number = 13;
$all_number = 1;
for($i=1;$i<=13;$i++){
if ($i % 3 == 0){
$all_number = $all_number + 1;
}
}
$price_new_data = $old_price*$all_number;
$price = 13.50+($price_new_data*$product_quenty);
?>