How to add the Cart button on top of the page in Woocommerce?

I am developing a site using WordPress+Woocommerce

I am having a problem here, how to add the cart on top of the page that will dynamically change the number of products and price, each time a user adds a product to cart?

Read More

Currently I am trying with this code, but no luck yet:

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;

}

Related posts

Leave a Reply

1 comment

  1. Try this code at top of your page header.

    you have to declar this first :
    <?php global $woocommerce; ?> //required !!!!

    And then put this wherever you want the total number of items to appear :

    <?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?>
    

    if someone want to print the total ( $ ) like me you can put this code :

    <?php echo $woocommerce->cart->get_cart_total(); ?>
    

    and here is the cart url too :

    <?php echo $woocommerce->cart->get_cart_url(); ?>
    

    That’s all .