在WooCommerce中,我试图在挂钩woocommerce_add_to_cart
时获得购物车总数。这样可行,但返回的购物车总数是最后添加的商品之前的总数。我想要更新的总数,以便能够显示有关运费的通知。
知道如何实现这个目标吗?
我目前的代码:
function oppsalg_add_to_cart() {
global $woocommerce;
// Limit
$minimum_cart_total = 1000;
// Cart value (Not including the last added item)
$total = WC()->cart->subtotal;
// Comparison
if( $total < $minimum_cart_total ) {
// Display notice
wc_add_notice( sprintf( '<strong>Shipping is free above %s.</strong>'
.'<br />Your total is %s. Perhaps you would like to add more items?',
$minimum_cart_total,
$total ),
'notice' );
}
}
add_action('woocommerce_add_to_cart', 'oppsalg_add_to_cart');
答案 0 :(得分:1)
在这里,您只需测试此解决方案,即在没有AJAX的情况下将单页产品添加到购物车。当您通过AJAX更改购物车页面上的购物车数量时,这不会修改您看到的消息。此外,您应该使运费成本动态,以便它不像我在下面所做的那样硬编码,这可能是您修理的功课。
add_filter( 'wc_add_to_cart_message_html', 'modify_wc_add_to_cart_message_html', 10, 2 );
function modify_wc_add_to_cart_message_html( $message, $products ) {
$minimum_cart_total = 100;
$cart_total = WC()->cart->cart_contents_total;
if( $cart_total < $minimum_cart_total ) {
$message = sprintf( '<strong>Shipping is free above %s.</strong>'
.'<br />Your total is %s. Perhaps you would like to add more items?',
wc_price( $minimum_cart_total ),
wc_price( $cart_total )
);
}
return $message;
}