2014-04-13 72 views
1

我正在使用woocommerce和wordpress作爲電子商務網站。我希望能夠在頁面上的任何位置顯示用戶購物車中的物品數量和總價格。如何在Wordpress/WooCommerce中顯示購物車信息

通常情況下 - 如果您使用其中一個woo主題,則會在菜單導航欄中顯示。但是,我使用的是幾乎完全空白的主題,我不知道如何獲取物品/價格總計信息並將其顯示在html中。他們的文檔給出了這樣的片段:
http://docs.woothemes.com/document/show-cart-contents-total/

<?php global $woocommerce; ?> 
<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>`<br> 

但我不明白是什麼HTML標籤或類我會用作出這樣的顯示。我需要使用什麼類型的元素和類ID來使其顯示?

回答

1

請務必在您的主題的functions.php文件中包含以下代碼。

而且這也會使您的購物車在沒有重新加載頁面的情況下自動更新。

// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php) 
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment'); 

function woocommerce_header_add_to_cart_fragment($fragments) { 
    global $woocommerce; 

    ob_start(); 

    ?> 
    <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a> 
    <?php 

    $fragments['a.cart-contents'] = ob_get_clean(); 

    return $fragments; 

} 
相關問題