【问题标题】:Woocommerce : cart total shows zero for 1 itemWoocommerce:购物车总数显示 1 件商品为零
【发布时间】:2017-01-30 08:40:01
【问题描述】:

我已经在我的网站标题中创建了一个指向当前总数的购物车的链接。

它应该以这种方式显示购物车的当前总数和购物车中包含的商品数量:

[icon] 1.20 € - 1 篇文章

但是,只有在购物车页面中才能正确计算该值。例如在主页上,它只会这样显示:

[icon] 0.00 € - 1 篇文章

这就是我访问值的方式:

$count = WC()->cart->cart_contents_count;
WC()->cart->calculate_totals();
if($count > 0)
{
    print "<a class='cart-contents' href='" . WC()->cart->get_cart_url();
    print "' title='Voir votre panier'>";
    print WC()->cart->get_total();
    print " - " . sprintf(_n('%d article', '%d articles', $count, 'woothemes'), $count);
    print "</a>";
}

如果我打电话给WC()-&gt;cart-&gt;get_cart_total();,这将显示不含税的价格,即使我强制价格包含税。

我怎样才能在整个网站上始终如一地获得正确的值?

【问题讨论】:

    标签: wordpress woocommerce cart


    【解决方案1】:
    global $woocommerce;
    
    $amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;
    

    您还可以根据需要将 $amount 转换为浮点值。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,所以我查看了 WooCommerce 代码。在WC_Cart类中的函数WC()-&gt;cart-&gt;calculate_totals()中,因为前段时间有以下条件

      // Only calculate the grand total + shipping if on the cart/checkout
      if ( is_checkout() || is_cart() || 
           defined('WOOCOMMERCE_CHECKOUT') || defined('WOOCOMMERCE_CART') )
      

      这个条件适用于专门计算total,我不知道为什么。但这为您提供了正确的解决方法: 在需要显示总数的页面上,包括以下任一代码行:

      define( 'WOOCOMMERCE_CART', true );
      

      define( 'WOOCOMMERCE_CHECKOUT', true );
      

      然后执行函数WC()-&gt;cart-&gt;calculate_totals();

      然后使用WC()-&gt;cart-&gt;totalwc_cart_totals_order_total_html()(用于货币符号)获取购物车总数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-22
        • 2020-07-13
        • 2019-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多