Archived topic
Adding a cart icon with number of items and total cost in nav menu
21 replies · Started by Jacob on January 8, 2018
First thing you want to do is remove everything from your child theme functions.php file, your file should look like this:
<?php
function custom_wc_cart_link() {
ob_start();
?>
<a href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" class="cart-contents" title="עגלה שלי">
<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' );
}
Hi Tom
Sorry but I do not understand .. What should I remove. And where it is found what needs to be removed.
Sorry for the ignorance and thank you for your patience
Instead of using the functions.php file, use this plugin: https://en-ca.wordpress.org/plugins/code-snippets/
And add this code to a new snippet:
function custom_wc_cart_link() {
ob_start();
?>
<a href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" class="cart-contents" title="עגלה שלי">
<?php echo sprintf ( _n( '%d', '%d', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?>
</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' );
}
Thanks Tom
It works perfect!
Now I just need a drop-down menu with an optional cart / checkout
And the products I added to my shopping cart
Thanks for the help
Currently GP can't do that by default, and the amount of code is too much for the forum.
We're hoping to get it added to the WC module in GPP soon :)
Thanks Tom.
I appreciate your help, and your patience.
I'm very glad I bought a template where the support is excellent.
Hopefully in the future will be this model.
Go up and succeed
Thanks Kobi
Thank you! :)