Site logo

[Resolved] Adding a cart icon with number of items and total cost in nav menu

Home Forums Support [Resolved] Adding a cart icon with number of items and total cost in nav menu

Home Forums Support Adding a cart icon with number of items and total cost in nav menu

Viewing 7 posts - 16 through 22 (of 22 total)
  • Author
    Posts
  • #468535
    Tom
    Lead Developer
    Lead Developer

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

    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

    #468909
    Tom
    Lead Developer
    Lead Developer

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

    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

    #469743
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #469851
    Jacob

    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

    #470087
    Tom
    Lead Developer
    Lead Developer

    Thank you! 🙂

Viewing 7 posts - 16 through 22 (of 22 total)
  • You must be logged in to reply to this topic.