本文介绍了WooCommerce woocommerce_check_cart_项和购物车总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在woocommerce_check_cart_items挂钩中获取购物车总数时,我得到的是零,而不是实际的购物车总数.知道为什么吗?这是代码.

When I try to get cart total in woocommerce_check_cart_items hook, I get zero instead of the actual cart total. Any idea why? Here is the code.

add_action('woocommerce_check_cart_items', 'check_total');
function check_total() {
    echo WC()->cart->total; // this return zero
    echo WC()->cart->subtotal; // this returns fine, but without shipping costs
}

推荐答案

您还可以使用我以前使用过的其他代码.每次都对我来说很好.

you can also use other code as I have used ever. and it's working fine for me every time.

add_action( 'woocommerce_cart_collaterals', 'display_woocommerce_cart_totals', 10 );

function display_woocommerce_cart_totals(){
     global $woocommerce;
     echo $woocommerce->cart->total; 
     echo $woocommerce->cart->subtotal; 
}

这篇关于WooCommerce woocommerce_check_cart_项和购物车总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 12:48