admin管理员组文章数量:1321044
We need to run WooCommerce flat rate shipping rate based on every X items in the cart, for example:
- 1-5 Items: Delivery = £5
- 6-10 Items: Delivery = £10
- 11-15 Items: Delivery = £15
- and so on…
It's essentially £1 per item, but it's in brackets of £5, so 3 items would be £5, and 7 items would be £10, for example.
I'm hoping this is achievable with the out of the box shipping rates, or alternatively, I'd be relatively comfortable writing a theme function, if required - but the client has a limited budget on this.
I've tried looking for more reference on the available shortcodes that I can use in the advanced shipping options, but can't seem to find a way to achieve the above.
We need to run WooCommerce flat rate shipping rate based on every X items in the cart, for example:
- 1-5 Items: Delivery = £5
- 6-10 Items: Delivery = £10
- 11-15 Items: Delivery = £15
- and so on…
It's essentially £1 per item, but it's in brackets of £5, so 3 items would be £5, and 7 items would be £10, for example.
I'm hoping this is achievable with the out of the box shipping rates, or alternatively, I'd be relatively comfortable writing a theme function, if required - but the client has a limited budget on this.
I've tried looking for more reference on the available shortcodes that I can use in the advanced shipping options, but can't seem to find a way to achieve the above.
Share Improve this question edited Apr 12, 2019 at 0:13 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Apr 5, 2019 at 8:33 fish_rfish_r 1231 silver badge9 bronze badges 03 Answers
Reset to default 3For that, you need to use a hooked function in woocommerce_package_rates
filter hook, targeting "Flat rate" shipping method.
In shipping settings for flat rate shipping method you will set a cost of 5
.
Each 5 items step, the shipping cost will be increased by 5:
• From 1 to 5 Items: Delivery = 5
• From 6 to 10 Items: Delivery = 10
• From 11 to 15 Items: Delivery = 15
• And so on…
Here is the code:
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;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
Refresh the shipping caches: (required)
- This code is already saved on your active theme's function.php file.
- The cart is empty
- In a shipping zone settings, disable / save any shipping method, then enable back / save.
Still can't comment so I need to post this fix to the above awesome answer.
For the rate calc to work properly, the return $rates;
line must be outside the cart loop.
The complete function should be:
$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;
}
To add a specific class you must check each item in the cart, and check with the slug of the class which items correspond, in this way:
First add the woocommerce_package_rates filter
add_filter( 'woocommerce_package_rates', 'different_rates_based_on_quantity_steps', 10, 2 );
Then insert the function
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; } }
本文标签: WooCommerceFlat rate shipping based on X quantity steps
版权声明:本文标题:WooCommerce - Flat rate shipping based on X quantity steps? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742094344a2420455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论