我在这个答案中找到了解决问题的半功能解决方案WooCommerce - Flat rate shipping based on X quantity steps? 但这会计算购物车项目的总数量,因为如果客户添加了一个可以打包在一起的小项目,并且该项目使购物车项目的总数超过阈值,则会导致发货量翻倍。仅当特定类别(即大型装运类别)超过阈值时,我需要将装运加倍。这里有一些我需要的例子。
现场2类产品
衬衫(一个包装中最多只能发送3件)-分类为“大发货类别”(可以在一个包装中发送多件)因为如果客户购买了这两种产品的组合,我可以将这些戒指添加到衬衫包装中,所以我设置了运费,以收取最大的发货类别(大发货)。
这里有一些订单样本
3件T恤=8美元运费3件T恤+6枚戒指=8美元运费4件T恤=16美元运费(每3秒翻一倍)4件T恤+6枚戒指=16美元(忽略戒指作为购物车数量)下面的代码就是这样做的
3件T恤=8美元运费(好)3件T恤+1枚戒指=16美元运费(不好)6件T恤=16美元运费(好)
- 看到我的困境了吗?我想修改下面的代码,只计算特定装运类别中的项目,而不是购物车数量。
这是密码
add_filter( \'woocommerce_package_rates\',
\'change_shipping_method_rate_based_on_shipping_class_2\', 11, 2 );
function change_shipping_method_rate_based_on_shipping_class_2( $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;
}
非常感谢您的帮助