简体   繁体   中英

How to display shopping cart information in Wordpress / WooCommerce

I am using woocommerce and wordpress for an e-commerce website. I want to be able to display the number of items and total price in a user's cart anywhere on the page.

Normally -- and if you use one of the woo themes -- this shows up in the menu navigation bar. However, I am using an almost completely blank theme, and I do not know how I can get the item / price total information and display it in html. Their documentation gives this snippet:
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>

But I do not understand what HTML tag or class I would use to make that display. What sort of element and class id would I need to use to make this appear?

Make sure to include the codes below for your theme's functions.php file.

And also this will ajaxify your cart to be updated automatically without reloading the page.

// 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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM