php - 基于购物车项目数量的 WooCommerce 中特定运输类别的购物车消息
问题描述
这改编自WooCommerce答案中特定运输类别的购物车消息,并根据特定运输类别答案的购物车项目数量显示或隐藏运输方法。
这是我的代码尝试:
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;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$shipping_class_id = '150'; // Your shipping class Id
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$related_total_qty = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
wc_clear_notices();
wc_add_notice( sprintf('Beers can only be shipped via pallet shipping. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
'notice' );
break;
}
}
}
但这似乎不起作用。有任何想法吗?
更新:我让它工作了。这是代码:
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;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$shipping_class_id = '150'; // Your shipping class Id
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$related_total_qty = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
wc_clear_notices();
wc_add_notice( sprintf('Beers can only be shipped via pallet shipping. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
'notice' );
}
}
解决方案
工作得很好。
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;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$shipping_class_id = '150'; // Your shipping class Id
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$related_total_qty = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
wc_clear_notices();
wc_add_notice( sprintf('Beers can only be shipped via pallet shipping. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
'notice' );
}
}
推荐阅读
- javascript - 图像未使用带有输入和 ngFor 的数组显示 - Angular
- python - “numpy.ndarray”对象不可调用文件未正确选择
- python - Outlook 根据日期、发件人和主题行使用 MAPI python 下载附件
- java - 如何从导航组件中的子片段访问父片段的视图
- html - 表格 CSS 根本没有出现
- elasticsearch - ElasticSearch - 跨多个字段的部分匹配
- c# - VB 的二维字符串数组到 C# ASP.NET
- scala - Scalafmt 包名检查器
- elasticsearch - 显示 Kibana 中的所有可用路线
- postfix-mta - 不需要标准的后缀配置