WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] want to show cart icon with number of items and total cost in .my-extra-widget (5 posts)

  1. yavarkhan
    Member
    Posted 1 year ago #

    want to show cart icon with number of items and total cost in .my-extra-widget
    pls help me change the code so i could show my cart on the top right corner of the page

    <?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: http://wordpress.org/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;
    
    }

    https://wordpress.org/plugins/woocommerce/

  2. yavarkhan
    Member
    Posted 1 year ago #

    Place a cart icon with number of items and total cost in the extra widget area.

  3. Jeremiah
    Member
    Posted 1 year ago #

    Hey there!

    If you create a custom menu widget and add it to that widget area you can assign the menu cart item to display there, no code required. Also, the pro version of the menu cart plugin (http://wordpress.org/plugins/woocommerce-menu-bar-cart/) includes a shortcode that you can add pretty much anywhere on your site.

    Jeremiah

  4. yavarkhan
    Member
    Posted 1 year ago #

    thx

  5. Jeremiah
    Member
    Posted 1 year ago #

    Welcome! Post back whether that works :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.