我们需要根据购物车中的每X个项目运行WooCommerce统一费率运费,例如:
1-5项:交货=5英镑,6-10项:交货=10英镑,因此3项为5英镑,7项为10英镑,例如。
我希望通过开箱即用的运费可以做到这一点,或者,如果需要的话,我可以相对轻松地编写一个主题函数,但客户在这方面的预算有限。
我试图寻找更多关于可用快捷码的参考,我可以在高级装运选项中使用这些快捷码,但似乎找不到实现上述目标的方法。
我们需要根据购物车中的每X个项目运行WooCommerce统一费率运费,例如:
1-5项:交货=5英镑,6-10项:交货=10英镑,因此3项为5英镑,7项为10英镑,例如。
我希望通过开箱即用的运费可以做到这一点,或者,如果需要的话,我可以相对轻松地编写一个主题函数,但客户在这方面的预算有限。
我试图寻找更多关于可用快捷码的参考,我可以在高级装运选项中使用这些快捷码,但似乎找不到实现上述目标的方法。
为此,需要在woocommerce_package_rates
过滤器挂钩,针对“固定费率”的运输方式。
在装运设置中flat rate 您将采用的运输方式set a cost of 5
.
每5项step, 运输成本将增加5:
从1项增加到5项:交货=5
•6至10项:交货=10
•从11到15项:交货=15
<等等
代码如下:
add_filter( \'woocommerce_package_rates\', \'different_rates_based_on_quantity_steps\', 10, 2 );
function different_rates_based_on_quantity_steps( $rates, $package ){
$items_count = WC()->cart->get_cart_contents_count(); // Cart item count
$items_change = 5; // number of items needed to increase the cost each time
$rate_operand = ceil( $items_count / $items_change ); // Operand increase each 5 items here
foreach ( $rates as $rate_key => $rate ){
// Targetting "Flat rate"
if( \'flat_rate\' === $rate->method_id ) {
$has_taxes = false;
// Set the new cost
$rates[$rate_key]->cost = $rate->cost * $rate_operand;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
// New tax calculated cost
$taxes[$key] = $tax * $rate_operand;
$has_taxes = true;
}
}
// Set new taxes cost
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
代码进入功能。活动子主题(或活动主题)的php文件。已测试并正常工作。Refresh the shipping caches: (必需)
此代码已保存在活动主题的函数中。php文件
仍然无法发表评论,因此我需要将此修复发布到上述令人敬畏的答案。
为了使费率计算正常工作return $rates;
行必须位于购物车循环之外。
完整功能应为:
$step_change = 5; // Steps for item
$item_count = 0;
// Checking in cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// If we find the shipping class
if( $cart_item[\'data\']->get_shipping_class() == $slug_class_shipping ){
$item_count += $cart_item[\'quantity\']; // Cart item count
$rate_operand = ceil( $item_count / $step_change ); // Operand increase each # items here
foreach ( $rates as $rate_key => $rate ){
// Targetting "Flat rate"
if( \'flat_rate\' === $rate->method_id ) {
$has_taxes = false;
// Set the new cost
$rates[$rate_key]->cost = $rate->cost * $rate_operand;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
// New tax calculated cost
$taxes[$key] = $tax * $rate_operand;
$has_taxes = true;
}
}
// Set new taxes cost
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
要添加特定的类,您必须检查购物车中的每个项目,并用类的slug检查哪些项目对应,方法如下:
首先添加woocommerce\\u package\\u rates筛选器
add_filter( \'woocommerce_package_rates\', \'different_rates_based_on_quantity_steps\', 10, 2 );
然后插入函数function different_rates_based_on_quantity_steps ( $rates, $package ){
$slug_class_shipping = \'books\'; // Slug of the shipping class
$step_change = 5; // Steps for item
$item_count = 0;
// Checking in cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// If we find the shipping class
if( $cart_item[\'data\']->get_shipping_class() == $slug_class_shipping ){
$item_count += $cart_item[\'quantity\']; // Cart item count
$rate_operand = ceil( $item_count / $step_change ); // Operand increase each # items here
foreach ( $rates as $rate_key => $rate ){
// Targetting "Flat rate"
if( \'flat_rate\' === $rate->method_id ) {
$has_taxes = false;
// Set the new cost
$rates[$rate_key]->cost = $rate->cost * $rate_operand;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
// New tax calculated cost
$taxes[$key] = $tax * $rate_operand;
$has_taxes = true;
}
}
// Set new taxes cost
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
} return $rates;
} break; } }