Woocommerce中特定运输类别的购物车消息和最少的购物车总数 [英] Cart Message for a specific shipping class and a minimal cart total in Woocommerce
本文介绍了Woocommerce中特定运输类别的购物车消息和最少的购物车总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找以下解决方案:我想为购物车中具有特定运输类别的产品显示购物车消息,并且当购物车小计低于75美元时:
I am looking for the following solution: I would like to display a cart message for products that have a specific shipping class are in cart and when the cart subtotal is below $75:
- 运输班:"bezorgservice".
- 最低购物车数量: 75.
- 购物车消息: 再订购20欧元,即可使用我们的送货服务在家里送货.
- Shipping class: "bezorgservice".
- Minimum cart amount: 75.
- cart Message: Order € 20,00 more to deliver your order at home with our delivery service.
感谢您的帮助.
推荐答案
已更新:可以使用此简单的自定义挂钩函数(代码带有注释)轻松完成此操作:
Updated: This can be done easily with this simple custom hooked function (code is commented):
add_action( 'woocommerce_before_calculate_totals', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
if ( is_admin() && ! defined('DOING_AJAX') )
return;
## Your settings below ##
$shipping_class = 'bezorgservice'; // The targeted shipping class
$min_amout = 75; // The minimal amount
$amout_incl_tax = 0; // Initializing variable
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
// Targeting our defined shipping class only
if( $cart_item['data']->get_shipping_class() === $shipping_class ){
// Add each subtotal from our defined shipping class only
$amout_incl_tax += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
}
}
// Display an error notice when quantity count is below the minimum defined on cart page only
if( $amout_incl_tax > 0 && $amout_incl_tax < $min_amout && is_cart() ){
wc_clear_notices(); // Clear all other notices
// Display our custom notice
wc_add_notice( sprintf( 'Order <strong>%s</strong> more to deliver your order at home with our delivery service.',
wc_price($min_amout - $amout_incl_tax) ), 'notice' );
}
}
代码进入活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of the active child theme (or active theme). Tested and works.
这篇关于Woocommerce中特定运输类别的购物车消息和最少的购物车总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文