我们需要根据购物车中的每一个X项运行WooCommerce统一费率运费,例如:
它本质上是每个项目的be 1,但它在be 5的括号中,因此3项将是be 5,而7项将是be 10。
我希望通过开箱即用的运费可以实现这一点,或者说,如果需要的话,我会相对比较满意地编写一个主题函数--但是客户在这方面的预算有限。
我已经尝试寻找更多的参考,我可以使用在先进的运输选项,但似乎找不到一个方法来实现上述的可用的短代码。
发布于 2019-04-11 22:59:28
为此,您需要在woocommerce_package_rates过滤器钩子中使用一个挂钩函数,目标是“固定费率”传送方法。
在flat费率传送方法的传递设置中,您将set的成本为 5。
每5项步骤,运费将从1:·增加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;
}代码在您的活动子主题(或活动主题)的function.php文件中。测试和工作。
Refresh运输缓存:(必需)
发布于 2020-05-07 15:08:07
仍然不能评论,所以我需要张贴这个修复以上可怕的答案。
要使速率计算正常工作,return $rates;行必须位于cart循环之外。
完整的职能应该是:
$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;
}发布于 2019-12-28 19:33:07
要添加特定的类,您必须检查购物车中的每一项,并以这样的方式检查条目对应的类的段塞:
首先添加woocommerce_package_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; } } https://wordpress.stackexchange.com/questions/333554
复制相似问题