• Hello,guys
    I custom a shop theme by woocommerce plugin, now I don’t know to how add a cart icon in head ,it could display the count and price of products in the cart at mount

    I download 2 woocommerce shop themes:artificer & wootique form woo’s website, they all have cart icon in the head (also have checkout button beside it), but I couldn’t find how could they get it in the header.php. There is nothing info about it.

    Can anybody help me?
    Thank you very much!

    Vincent

    https://www.ads-software.com/plugins/woocommerce/

Viewing 10 replies - 1 through 10 (of 10 total)
  • You could do something like this in your header.php file:

    <?php
    global $woocommerce;
    
    // get cart quantity
    $qty = $woocommerce->cart->get_cart_contents_count();
    
    // get cart total
    $total = $woocommerce->cart->get_cart_total();
    
    // get cart url
    $cart_url = $woocommerce->cart->get_cart_url();
    
    // if multiple products in cart
    if($qty>1)
          echo '<a href="'.$cart_url.'">'.$qty.' products | '.$total.'</a>';
    
    // if single product in cart
    if($qty==1)
          echo '<a href="'.$cart_url.'">1 product | '.$total.'</a>';
    
    ?>

    Adding the cart icon should be relatively easy then. Other cart methods can be found here: https://docs.woothemes.com/wc-apidocs/class-WC_Cart.html

    Thread Starter icevincent

    (@icevincent)

    lemu,
    Thank you very much, the code is great!

    hello sir i have used zebra themes in this theme only display the count and price of products but I don’t know to how add a cart icon in head

    hey guys ??

    have a question.

    how can i add those cart info inside the Heading in unique div?. in a way that its not inside the nav.ul.li
    like
    <heading>
    <brand> <nav.ul.li> <cart>
    </heading>

    // addd cat in header
    
    add_filter('wp_nav_menu_items','add_cart_in_menu', 10, 2);
    
    function add_cart_in_menu( $menu, $args ) {
    
        if( $args->theme_location == 'primary')  {global $woocommerce;
    
    	// get cart quantity
    	$qty = $woocommerce->cart->get_cart_contents_count();
    
    	// get cart total
    	$total = $woocommerce->cart->get_cart_total();
    
    	// get cart url
    	$cart_url = $woocommerce->cart->get_cart_url();
    
    	// if multiple products in cart
    	if($qty>1)
    	      echo '<a class="cart_is_in_menu" href="'.$cart_url.'">'.$qty.' products | '.$total.'</a>';
    
    	// if single product in cart
    	if($qty==1)
    	      echo '<a class="cart_is_in_menu"  href="'.$cart_url.'">1 product | '.$total.'</a>';
    
        }
        return $menu;
    
    }

    is this bit of code correct?

    <?php
    //* Do NOT include the opening php tag
    //* Make Font Awesome available
    add_action( 'wp_enqueue_scripts', 'enqueue_font_awesome' );
    function enqueue_font_awesome() {
        wp_enqueue_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css' );
    }
    /**
    * Place a cart icon with number of items and total cost in the menu bar.
    *
    * Source: https://www.ads-software.com/plugins/woocommerce-menu-bar-cart/
    */
    add_filter('wp_nav_menu_items','sk_wcmenucart', 10, 2);
    function sk_wcmenucart($menu, $args) {
        // Check if WooCommerce is active and add a new item to a menu assigned to Primary Navigation Menu location
        if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'primary' !== $args->theme_location )
            return $menu;
        ob_start();
            global $woocommerce;
            $viewing_cart = __('View your shopping cart', 'your-theme-slug');
            $start_shopping = __('Start shopping', 'your-theme-slug');
            $cart_url = $woocommerce->cart->get_cart_url();
            $shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
            $cart_contents_count = $woocommerce->cart->cart_contents_count;
            $cart_contents = sprintf(_n('%d item', '%d items', $cart_contents_count, 'your-theme-slug'), $cart_contents_count);
            $cart_total = $woocommerce->cart->get_cart_total();
            // Uncomment the line below to hide nav menu cart item when there are no items in the cart
            // if ( $cart_contents_count > 0 ) {
                if ($cart_contents_count == 0) {
                    $menu_item = '<li class="right"><a class="wcmenucart-contents" href="'. $shop_page_url .'" title="'. $start_shopping .'">';
                } else {
                    $menu_item = '<li class="right"><a class="wcmenucart-contents" href="'. $cart_url .'" title="'. $viewing_cart .'">';
                }
                $menu_item .= '<i class="fa fa-shopping-cart"></i> ';
                $menu_item .= $cart_contents.' - '. $cart_total;
                $menu_item .= '</a></li>';
            // Uncomment the line below to hide nav menu cart item when there are no items in the cart
            // }
            echo $menu_item;
        $social = ob_get_clean();
        return $menu . $social;
    }

    can so 1 help me to show this code in my .my-extra-widget

    yavarkhan

    you want the cart to go into a extra.widget area. that is located in the header?

    yes excatly my extra widget is in the top right corner

    im a total loser with coding > php

    but maybe what we want could be done like this:

    1- create a widget area

    2- position widget area into theme > heading

    3- insert data inside the widget area. the data = load woocommerce variables and echo it.

    —-

    1 to 2 = https://www.themesandco.com/snippet/add-widget-area-header/

    3 = we should find a way to know how to: add content into a widget area (with ‘id’ => ‘customname’ ) using functions hooks.

    and in that function we could add woocommerce variables

    global $woocommerce;
    
    	// get cart quantity
    	$qty = $woocommerce->cart->get_cart_contents_count();
    
    	// get cart total
    	$total = $woocommerce->cart->get_cart_total();
    
    	// get cart url
    	$cart_url = $woocommerce->cart->get_cart_url();
    
    	// if multiple products in cart
    	if($qty>1)
    	      echo '<a id="cart_in_menu_11" class="cart_is_in_menu" href="'.$cart_url.'">'.$qty.' products | '.$total.'</a>';
    
    	// if single product in cart
    	if($qty==1)
    	      echo '<a id="cart_in_menu_1" class="cart_is_in_menu"  href="'.$cart_url.'">1 product | '.$total.'</a>';
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘how to add cart icon in head’ is closed to new replies.