• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Woocommerce Cart Count Needs Repositioning

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Woocommerce Cart Count Needs Repositioning

This topic is: not resolved

Tagged: cart_contents_count, WooCommerce

  • This topic has 8 replies, 3 voices, and was last updated 9 years, 2 months ago by Ben Siegfried.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • December 4, 2016 at 7:19 pm #197026
    Ben Siegfried
    Participant

    In my local dev version I have this Woocommerce cart count working but I need it repositioned into the div class ".before-header" as a list item (li) if that's possible. It's not installed right now and I'm not sure if it needs to be if you know how to reposition it. I was only able to get to be positioned at "genesis_before_header," but that's not precisely where it needs to go.

    I don't know how to make that adjustment in PHP, interested in helping out?

    Here's the gist: Woocommerce Cart Count with Icon

    Here's the code found at the gist:

    //* Add Cart icon and count to header if WC is active
    add_action( 'genesis_header', 'jmd_wc_cart_count' );
    function jmd_wc_cart_count() {
     
        if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
     
            $count = WC()->cart->cart_contents_count;
            ?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php if ( $count > 0 ) echo '(' . $count . ')'; ?></a><?php
        
        }
     
    }
    //* Ensure cart contents update when products are added to the cart via AJAX
    add_filter( 'woocommerce_add_to_cart_fragments', 'jmd_header_add_to_cart_fragment' );
    function jmd_header_add_to_cart_fragment( $fragments ) {
     
        ob_start();
        $count = WC()->cart->cart_contents_count;
        ?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php if ( $count > 0 ) echo '(' . $count . ')'; ?></a><?php
     
        $fragments['a.cart-contents'] = ob_get_clean();
         
        return $fragments;
    		
    }

    The CSS for the gist for helpful reference:

    .cart-contents {
      background-color: #f5f5f5;
      color: #333;
      font-size: 16px;
      padding: 16px 18px 17px;
      position: absolute;
      right: 60px;
      top: 0;
      z-index: 9999;
    }
    
    .cart-contents:before{
      font-family: WooCommerce;
      color: #333;
      content: "\e01d";
      font-size: 16px;
      margin-top: 10px;
      font-style: normal;
      font-weight: 400;
      padding-right: 5px;
      vertical-align: bottom;
    }
    
    .cart-contents:hover {
      background-color: #333;
      color: #fff;
      text-decoration: none;
    }
    
    .cart-contents:hover:before {
      color: #fff;
    }

    Below is the website. I would like it to appear to the right of the menu items Checkout and My Account where ".before-header" is found.

    https://coronadosurfdesigns.com/
    December 5, 2016 at 5:54 am #197039
    Brad Dalton
    Participant

    Looks like you're using the custom menu widget in the before header widget area?


    Tutorials for StudioPress Themes.

    December 5, 2016 at 10:38 am #197060
    Ben Siegfried
    Participant

    Yes, that is a widget area that the theme developer installed, it is titled Before Header.

    December 5, 2016 at 10:54 am #197063
    Brad Dalton
    Participant

    You can change the hook to genesis_before_header and position using CSS.

    Or

    If you want to add it to the nav menu items, you'll need to filter the nav menu


    Tutorials for StudioPress Themes.

    December 5, 2016 at 11:43 am #197069
    Ben Siegfried
    Participant

    I was able to add it to genesis_before_header, but I need to get it to become a list-item (li), that would be the ideal position. Are you saying filtering the nav menu for that would be get me there? I'm not skilled with filtering, I have a vague understanding of it.

    December 5, 2016 at 11:50 am #197070
    Brad Dalton
    Participant

    I spent a few hours working this out for my members if you need the complete solution and want to save some time.


    Tutorials for StudioPress Themes.

    December 5, 2016 at 12:02 pm #197072
    Ben Siegfried
    Participant

    Thanks, I saw Sridhar's tutorial for this one, which looks similar. The thing I don't like about it is that the values don't update with changes in the Cart via AJAX. This one have in my post here does, however it does not display the price. I'm more interested in seeing one with AJAX though, and ideally, it shows the item count and accumulated price changing via AJAX, and in my specific issue be able to put it into a menu.

    With Sridhar's or yours, I'm pretty sure you both have a way to choose a menu target of primary or secondary. With my scenario I don't think that applies as you've pointed out that I need to filter it.

    December 5, 2016 at 12:24 pm #197075
    Victor Font
    Moderator

    This is a function I use to update items added to my menu cart through Ajax. You'll need to modify it for your specific needs, but it should help get you started.

    // Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
    add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
    function woocommerce_header_add_to_cart_fragment( $fragments ) {
    	ob_start();
    	    global $woocommerce;
            $viewing_cart = __('View your shopping cart', 'shdplans');
            $start_shopping = __('Start shopping', 'shdplans');
            $cart_url = $woocommerce->cart->get_cart_url();
            $cart_contents_count = $woocommerce->cart->cart_contents_count;
    		$cart_contents = sprintf(_n('%d item', '%d items', $cart_contents_count, 'shdplans'), $cart_contents_count);
    		$cart_total = $woocommerce->cart->get_cart_total();
    	?>
    	<a class="houseplanscart-contents" href="<?php echo $cart_url; ?>" title="<?php $viewing_cart; ?>" itemprop="url"><?php echo $cart_contents; ?> - <?php echo $cart_total; ?></a>
    	<?php
    
    	$fragments['a.houseplanscart-contents'] = ob_get_clean();
    
    	return $fragments;
    }
    

    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    December 5, 2016 at 1:11 pm #197079
    Ben Siegfried
    Participant

    Thanks Victor.

  • Author
    Posts
Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble