2015-10-29 43 views
3

我已將我網站的標題中的鏈接創建爲具有當前總計的購物車。Woocommerce:購物車總計顯示零個零售商品

它應該顯示當前總的車的和包含在車中的商品的數量,以這種方式:

[ICON] 1.20€ - 1文章

但是,值是隻有在購物車頁面中才能正確計算。在主頁上,例如,它只會顯示如下:

[圖標] 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()->cart->get_cart_total(); ,這將顯示不含稅的價格,即使我強制價格包括稅。

如何在整個網站上始終如一地獲得正確的值?

回答

3
global $woocommerce; 

$amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total; 

您還可以按照您的要求將浮動價值中的$ amount轉換。

1

我有同樣的問題,所以我經歷了WooCommerce代碼。在功能WC()->cart->calculate_totals()在類WC_Cart,因爲前一段時間有一個以下條件

// 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()->cart->calculate_totals();

然後得到購物車總計使用WC()->cart->totalwc_cart_totals_order_total_html()(貨幣符號)

相關問題