Archived topic
Cart icon customize
28 replies · Started by ZZZ on September 6, 2018
Hey Tom,
didnt work :/ The cart doesn't show the "quantity" when it is empty. It only shows quantity when items are inside the cart, 1 item and above.
When it is empty, it doesn't show zero.
Just adjusted the function, can you try now?: https://generatepress.com/forums/topic/cart-icon-customize/#post-673917
Hey Tom,
Still doesn't work. Here are the images with 1 item in cart, and empty cart.
Grr, WooCommerce not making it easy on us.
Another try: https://generatepress.com/forums/topic/cart-icon-customize/#post-673917
Hey Tom,
I'm not sure what went wrong.. it's still not working. However on initial load it works. 1second later it'll jump and then disappears. This does not happen for 1 item and above.
Please see screenshots.
Aha, replace this function as well:
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">0</span>';
return $fragments;
}
still no luck Tom... it's jumps again after 1 second.
I just tested it and it's working on my end.
These are the functions I used:
function tu_custom_wc_cart_link() {
ob_start();
$count = WC()->cart->get_cart_contents_count();
?>
<a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="cart-contents" title="<?php esc_attr_e( 'View your shopping cart','generate-woocommerce' ); ?>">
<?php _e( 'Enquiry','generate-woocommerce' );?>
<?php
if ( $count > 0 ) {
echo sprintf ( _n( '<span class="number-of-items">%d</span>', '<span class="number-of-items">%d</span>', $count ), $count );
} else {
echo '<span class="number-of-items">0</span>';
}
?>
</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">0</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' );
}
hey Tom, yes it is. I disabled all plugins and WP rocket seems to be the one affecting it! I'll go ask them.
Btw, is there a possibility of using GP secondary menu in Woocommerce?
What would you want to do with the secondary menu? Move the cart icon into it?
Hi Tom,
I would like to put it into the sidebar. But it doesn't allow this option for woocommerce pages. So just wondering if there's a way to do it because GP menu is smoother and cleaner as compared to Elementor nav menu :)
The Secondary Nav has sidebar locations in the Navigation Location option. Have you tried those yet?
Let me know :)
Hey Tom,
That's alright, Im using woocommerce with elementor, they might not allow the theme menu insertion in it.
Thanks for the the whole topic on the cart thingy! Cheers!
No problem! :)