Site logo

Archived topic

Show Quantity in 'Cart in menu' instead of total costs.

16 replies · Started by Rogier on June 12, 2017

Viewing posts 1–15 of 17

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!

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' );
}

Hi Tom,

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

Cheers!

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

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

Ah yes, adjusted.

Glad it works!

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' );
}

Great stuff Tom, this works like a charm!

Awesome :)

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

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.

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

Thank you very much Tom, it works perfectly!

You're very welcome :)

This archived topic is closed to new replies.