WooCommerce 中特定运输 Class 的购物车消息
Cart Message for a Specific Shipping Class in WooCommerce
我正在尝试为特定发货添加购物车消息 class。改编自 Cart Message for a specific shipping class and a minimal cart total in 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;
$shipping_class = '150';
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_shipping_class() === $shipping_class ){
wc_clear_notices();
wc_add_notice( sprintf( 'Beers can only be shipped via <a title="Pallet Shiping" href="/shipping-delivery/#Pallet_Shipping" target="_blank" rel="noopener noreferrer">Pallet Shiping</a>. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.'), 'notice' );
}
}
}
但它似乎不起作用。有什么想法吗?
在您的情况下,您似乎已经在问题代码中定义了 a shipping class Id,因此您需要使用 WC_Product
get_shipping_class_id()
方法如下:
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
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
// Check cart items for specific shipping class, displaying a notice
if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
wc_clear_notices();
wc_add_notice( sprintf(
__('Beers can only be shipped via %s. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
'<a title="Pallet Shipping" href="/shipping-delivery/#Pallet_Shipping" target="_blank" rel="noopener noreferrer">' . __("Pallet Shipping", "woocommerce") . '</a>'
), 'notice' );
break;
}
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。它应该有效。
添加:要仅在购物车页面上显示通知,请替换以下代码:
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
通过这个:
if ( ! is_cart() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
return;
我正在尝试为特定发货添加购物车消息 class。改编自 Cart Message for a specific shipping class and a minimal cart total in 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;
$shipping_class = '150';
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_shipping_class() === $shipping_class ){
wc_clear_notices();
wc_add_notice( sprintf( 'Beers can only be shipped via <a title="Pallet Shiping" href="/shipping-delivery/#Pallet_Shipping" target="_blank" rel="noopener noreferrer">Pallet Shiping</a>. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.'), 'notice' );
}
}
}
但它似乎不起作用。有什么想法吗?
在您的情况下,您似乎已经在问题代码中定义了 a shipping class Id,因此您需要使用 WC_Product
get_shipping_class_id()
方法如下:
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
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
// Check cart items for specific shipping class, displaying a notice
if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
wc_clear_notices();
wc_add_notice( sprintf(
__('Beers can only be shipped via %s. To reveal much cheaper UPS Shipping Rates, remove Beers from the Cart.', 'woocommerce'),
'<a title="Pallet Shipping" href="/shipping-delivery/#Pallet_Shipping" target="_blank" rel="noopener noreferrer">' . __("Pallet Shipping", "woocommerce") . '</a>'
), 'notice' );
break;
}
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。它应该有效。
添加:要仅在购物车页面上显示通知,请替换以下代码:
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
通过这个:
if ( ! is_cart() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
return;