php - 每 X 步增加统一运费,最少。基本费用-Woocommerce
问题描述
在 WooCommerce 商店中,我有各种产品可以根据数量断点确定运费。
IE:
Class1_1
:收取基础费率1,并为每增加一个项目增加一个基础费率;Class1_2
: 对第一项收取基本费率 1,然后在数量等于 2 的倍数时再次收取基本费率。
到目前为止,我有这个,但它并没有为每个项目添加。它将返回它找到的第一个类的结果,然后停止。
因此,如果有一个Class1_1
项目和一个Class2_1
项目,它只会为第一个项目做方程式。
我需要做什么来制作这个循环和所有产品线项目?
add_filter( 'woocommerce_package_rates', 'different_rates_based_on_quantity_class1_3', 10, 2 );
function different_rates_based_on_quantity_class1_3 ( $rates, $package ){
$item_count = 0;
// Checking in cart items
foreach( WC()->cart->get_cart() as $cart_item ){
$slug_class_shipping = $cart_item['data']->get_shipping_class();
switch ($slug_class_shipping) {
case "class1_3":
$step_change = 3;
break;
case "class1_2":
$step_change = 2;
break;
case "class3_1":
$step_change = 1;
break;
default:
$step_change = 1;
}
// 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; } }
编辑:第一次粘贴错误的代码
解决方案
推荐阅读
- angular - 动态地将唯一 id 设置为手风琴到角度的数据表 td
- mongodb - mongodb 错误:子进程失败,以“错误号 62”退出
- ajax - 如何进行递归发布请求?
- nuget - 无法找到版本 (>= ...) 的软件包 - 但我已经安装了该软件包?
- c# - 未调用枚举上的扩展方法
- excel - Excel VBA 宏会意外减速,通常在 DoEvents
- python - 在数据框中组合/映射行
- javascript - 形成 laravel 和 vueJS
- javascript - 如何在不重新出现用户先前输入的情况下完全重置表单?
- c++ - push_back 在调用析构函数时使对象中的指针无效