[Support request] Display Quantity in 'Cart in Navigation Bar' instead of total costs

Home Forums Support [Support request] Display Quantity in 'Cart in Navigation Bar' instead of total costs

Home Forums Support Display Quantity in 'Cart in Navigation Bar' instead of total costs

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #875089
    Ryan

    Hi,

    I am looking to display the cart quantity (instead of the costs), and I have added a code snippet based on Tom’s previous reply here: https://generatepress.com/forums/topic/show-quantity-in-cart-in-menu-instead-of-total-costs/

    
    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('Enquiry','generate-woocommerce');?> - <?php echo sprintf ( _n( '<span class="number-of-items">%d</span> item', 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;
    }
    	
    	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' );
    }
    

    However, the cart quantity in the navigation bar does not update when I remove an item in the cart on the cart page. Will you be able to help on this? (Currently, the cart quantity in the navigation bar updates only when I add a product to the cart on the product page)

    Also, how can I add custom CSS such that there’s a round circle around the quantity number?

    Thanks!

    GeneratePress 2.2.2
    GP Premium 1.7.8
    #875123
    David
    Staff
    Customer Support

    Hi there,

    GeneratePress Premium 1.8 includes this option in Customizer > Layout > Woocommerce. This version is currently in Release Candidate and can be downloaded from Account > Downloads on this site if you want to give it a test. We are expecting to push it out as a live release this week.

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