- This topic has 16 replies, 5 voices, and was last updated 5 years, 1 month ago by
David.
-
AuthorPosts
-
June 12, 2017 at 3:02 am #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!
June 12, 2017 at 7:07 pm #332691Tom
Lead DeveloperLead DeveloperHi 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' ); }
June 13, 2017 at 12:45 am #332768Rogier
Hi Tom,
This works perfect. Great stuff as always!
Hopefully it will be usefull to anyone else searching for this solution.Cheers!
June 13, 2017 at 12:47 am #332770Rogier
The answer to my second question, Change ‘Cart’ in Tom’s code to:
<?php _e( 'Cart', 'woocommerce' ); ?>
June 13, 2017 at 8:55 am #332986Tom
Lead DeveloperLead DeveloperAh yes, adjusted.
Glad it works!
June 21, 2017 at 5:10 am #336781Rogier
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' ); }
June 21, 2017 at 9:51 am #336960Tom
Lead DeveloperLead DeveloperAh, just edited the code above: https://generatepress.com/forums/topic/show-quantity-in-cart-in-menu-instead-of-total-costs/#post-332691
June 21, 2017 at 2:43 pm #337130Rogier
Great stuff Tom, this works like a charm!
June 21, 2017 at 7:23 pm #337196Tom
Lead DeveloperLead DeveloperAwesome ๐
December 13, 2017 at 12:55 pm #448881alberto 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
December 13, 2017 at 10:12 pm #449122Tom
Lead DeveloperLead DeveloperHmm 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.
December 14, 2017 at 11:04 am #449624alberto 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
December 15, 2017 at 2:03 pm #450495Tom
Lead DeveloperLead DeveloperCode above tested and working: https://generatepress.com/forums/topic/show-quantity-in-cart-in-menu-instead-of-total-costs/#post-332691
December 16, 2017 at 9:48 pm #451210alberto arredondo
Thank you very much Tom, it works perfectly!
December 16, 2017 at 11:11 pm #451224Tom
Lead DeveloperLead DeveloperYou’re very welcome ๐
-
AuthorPosts
- You must be logged in to reply to this topic.