[Resolved] Show Quantity in 'Cart in menu' instead of total costs.

Home Forums Support [Resolved] Show Quantity in 'Cart in menu' instead of total costs.

Home Forums Support Show Quantity in 'Cart in menu' instead of total costs.

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #332321
    Rogier

    Hi Tom,

    I Love your recent WooCommerce support for GeneratePress. You never let us loyal users down! Keep it up. Some questions:

    1. Is there a simple function to show Quantity in the ‘Cart in menu’ instead of total costs?Otherwise, which template could I overwrite in my GeneratePress Child theme?

    2. Also, is there another function to get the WordPress (translatable) word for ‘Cart’ instead of an icon?

    Thanks again!

    #332691
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Glad you like it!

    Both of these are possible, but would require some coding.

    For example, you’d have to build a couple new functions:

    function tu_custom_wc_cart_link() {
        ob_start();
        ?>
        <a href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" class="cart-contents" title="<?php esc_attr_e( 'View your shopping cart','generate-woocommerce' ); ?>">
            <?php _e( 'Cart','generate-woocommerce' );?> - <?php echo sprintf ( _n( '<span class="number-of-items">%d</span> item', '<span class="number-of-items">%d</span> items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?>
        </a>
        <?php
        return ob_get_clean();
    }
    
    function tu_custom_wc_menu_cart( $nav, $args ) {
        if ( $args->theme_location == 'primary' && generatepress_wc_get_setting( 'cart_menu_item' ) ) {
            return sprintf( 
                '%1$s 
                <li class="wc-menu-item %4$s" title="%2$s">
                    %3$s
                </li>',
                $nav,
                esc_attr__( 'View your shopping cart','generate-woocommerce' ),
                tu_custom_wc_cart_link(),
                is_cart() ? 'current-menu-item' : ''
            );
        }
    	
        // Our primary menu isn't set, return the regular nav
        return $nav;
    }
    
    function tu_custom_wc_mobile_cart_link() {
    	if ( function_exists( 'generatepress_wc_get_setting' ) && ! generatepress_wc_get_setting( 'cart_menu_item' ) ) {
    		return;
    	}
    	?>
    	<div class="mobile-bar-items wc-mobile-cart-items">
    		<?php do_action( 'generate_mobile_cart_items' ); ?>
    		<?php echo tu_custom_wc_cart_link(); ?>
    	</div><!-- .mobile-bar-items -->
    	<?php
    
    }
    
    add_filter( 'woocommerce_add_to_cart_fragments', 'tu_wc_cart_link_fragment' );
    function tu_wc_cart_link_fragment( $fragments ) {
    	global $woocommerce;
    	$fragments['.cart-contents span.number-of-items'] = ( WC()->cart->get_cart_contents_count() > 0 ) ? '<span class="number-of-items">' . wp_kses_data( WC()->cart->get_cart_contents_count() ) . '</span>' : '<span class="number-of-items"></span>';
    	return $fragments;
    }

    Then you’d have to remove the current icon and add your new one:

    add_action( 'after_setup_theme','tu_remove_wc_cart_item' );
    function tu_remove_wc_cart_item() {
        remove_filter( 'wp_nav_menu_items','generatepress_wc_menu_cart', 10, 2 );
        add_filter( 'wp_nav_menu_items','tu_custom_wc_menu_cart', 10, 2 );
    
        remove_action( 'generate_inside_navigation','generatepress_wc_mobile_cart_link' );
        remove_action( 'generate_inside_mobile_header','generatepress_wc_mobile_cart_link' );
    
        add_action( 'generate_inside_navigation','tu_custom_wc_mobile_cart_link' );
        add_action( 'generate_inside_mobile_header','tu_custom_wc_mobile_cart_link' );
    }
    #332768
    Rogier

    Hi Tom,

    This works perfect. Great stuff as always!
    Hopefully it will be usefull to anyone else searching for this solution.

    Cheers!

    #332770
    Rogier

    The answer to my second question, Change ‘Cart’ in Tom’s code to:

    <?php _e( 'Cart', 'woocommerce' ); ?>

    #332986
    Tom
    Lead Developer
    Lead Developer

    Ah yes, adjusted.

    Glad it works!

    #336781
    Rogier

    Hi Tom, the above solution works perfect. But now I realize it will only change the desktop cart. How can I do this same function to the Cart in the Mobile header?

    function custom_wc_cart_link() {
        ob_start();
        ?>
        <a href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" class="cart-contents" title="<?php esc_attr_e( 'View your shopping cart','generate-woocommerce' ); ?>">
            <i class="fa fa-shopping-cart" aria-hidden="true"></i> <small><?php echo sprintf ( _n( '%d', '%d', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?></small>
        </a>
        <?php
        return ob_get_clean();
    }
    
    function custom_wc_menu_cart( $nav, $args ) {
        if ( $args->theme_location == 'primary' && generatepress_wc_get_setting( 'cart_menu_item' ) ) {
            return sprintf( 
                '%1$s 
                <li class="wc-menu-item %4$s" title="%2$s">
                    %3$s
                </li>',
                $nav,
                esc_attr__( 'View your shopping cart','generate-woocommerce' ),
                custom_wc_cart_link(),
                is_cart() ? 'current-menu-item' : ''
            );
        }
    	
        // Our primary menu isn't set, return the regular nav
        return $nav;
    }
    
    add_action( 'after_setup_theme','remove_wc_cart_item' );
    function remove_wc_cart_item() {
        remove_filter( 'wp_nav_menu_items','generatepress_wc_menu_cart', 10, 2 );
        add_filter( 'wp_nav_menu_items','custom_wc_menu_cart', 10, 2 );
        add_filter( 'woocommerce_add_to_cart_fragments', 'custom_wc_cart_link' );
    }
    #336960
    Tom
    Lead Developer
    Lead Developer
    #337130
    Rogier

    Great stuff Tom, this works like a charm!

    #337196
    Tom
    Lead Developer
    Lead Developer

    Awesome ๐Ÿ™‚

    #448881
    alberto arredondo

    Hello Tom,

    I used the solution you posted but I can’t get updated the quantity of products showed in the icon(shopping cart) either when I add from the shop or when I update the items from the shopping cart. It only works when I change the page, for example, when I move from the shop to the shopping cart or from the shopping cart to payment. Is there a function to update it without the need to refresh / change the page?

    Thank you,

    Alberto

    #449122
    Tom
    Lead Developer
    Lead Developer

    Hmm good point. I believe you would need some javascript.

    The javascript for the price is built into WooCommerce, but I’m not sure if it has code for the item count.

    I just added a line to the code above which might do it.

    #449624
    alberto arredondo

    Hi Tom, thanks for your quick reply.

    I used the new code and unfortunately the number of products still does not update. Do you have any other idea that could work?

    Thanks

    #450495
    Tom
    Lead Developer
    Lead Developer
    #451210
    alberto arredondo

    Thank you very much Tom, it works perfectly!

    #451224
    Tom
    Lead Developer
    Lead Developer

    You’re very welcome ๐Ÿ™‚

Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.