Site logo

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

Viewing posts 1–15 of 22


I wanted to know if I could,Adding a cart icon with number of items and total cost in nav menu.

Thanks. Leo
I just add this code, its work.
But the icon did not change, it's still CART
After I press CART it does not open a window and shows me the stuff.

No. I still have the title Rewriting Cart. It does not change it to the icon.
I want it to look like this site template: https://www.templatemonster.com/demo/60096.html

When I click on the icon, I open a small window that has the View Cart / Checkout option.
And it shows me the products I've put into the basket

Hi. Tom Thanks for the replay
But it still does not look like a website template I put here.
And I replaced what you told me to change. And still do not have the icon (product basket)

At this point that would require a custom plugin, although we do have something like that planned in a future version.

I believe one of our users created a custom plugin to achieve this sort of layout (without any styling). If you join our Facebook group (GeneratePress Community) and ask about it, someone might be able to point you to that plugin.

Thanks Tom
I think I'll stick with the code you gave me here.
But my problem with this code, which I can not seem to translate into my language.
(View your shopping cart) this title to my language

That text is on the line above where you switched out the cart text for the icon - you should be able to edit it directly.

Can I see your code? Paste it into the editor, highlight it and then click the "code" button in the editor toolbox :)

<?php
/**
 * GeneratePress.
 *
 * Please do not make any edits to this file. All edits should be done in a child theme.
 *
 * @package GeneratePress
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// Set our theme version.
define( 'GENERATE_VERSION', '2.0.1' );

if ( ! function_exists( 'generate_setup' ) ) {
	add_action( 'after_setup_theme', 'generate_setup' );
	/**
	 * Sets up theme defaults and registers support for various WordPress features.
	 *
	 * @since 0.1
	 */
	function generate_setup() {
		// Make theme available for translation.
		load_theme_textdomain( 'generatepress' );

		// Add theme support for various features.
		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'post-thumbnails' );
		add_theme_support( 'post-formats', array( 'aside', 'image', 'video', 'quote', 'link', 'status' ) );
		add_theme_support( 'woocommerce' );
		add_theme_support( 'title-tag' );
		add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ) );
		add_theme_support( 'customize-selective-refresh-widgets' );

		add_theme_support( 'custom-logo', array(
			'height' => 70,
			'width' => 350,
			'flex-height' => true,
			'flex-width' => true
		) );

		// Register primary menu.
		register_nav_menus( array(
			'primary' => __( 'Primary Menu', 'generatepress' ),
		) );

		/**
		 * Set the content width to something large
		 * We set a more accurate width in generate_smart_content_width()
		 */
		global $content_width;
		if ( ! isset( $content_width ) ) {
			$content_width = 1200; /* pixels */
		}

		// This theme styles the visual editor to resemble the theme style.
		add_editor_style( 'css/admin/editor-style.css' );
	}
}

/**
 * Get all necessary theme files
 */
require get_template_directory() . '/inc/theme-functions.php';
require get_template_directory() . '/inc/defaults.php';
require get_template_directory() . '/inc/class-css.php';
require get_template_directory() . '/inc/css-output.php';
require get_template_directory() . '/inc/general.php';
require get_template_directory() . '/inc/customizer.php';
require get_template_directory() . '/inc/markup.php';
require get_template_directory() . '/inc/element-classes.php';
require get_template_directory() . '/inc/typography.php';
require get_template_directory() . '/inc/plugin-compat.php';
require get_template_directory() . '/inc/migrate.php';
require get_template_directory() . '/inc/deprecated.php';

if ( is_admin() ) {
	require get_template_directory() . '/inc/meta-box.php';
	require get_template_directory() . '/inc/dashboard.php';
}

/**
 * Load our theme structure
 */
require get_template_directory() . '/inc/structure/archives.php';
require get_template_directory() . '/inc/structure/comments.php';
require get_template_directory() . '/inc/structure/featured-images.php';
require get_template_directory() . '/inc/structure/footer.php';
require get_template_directory() . '/inc/structure/header.php';
require get_template_directory() . '/inc/structure/navigation.php';
require get_template_directory() . '/inc/structure/post-meta.php';
require get_template_directory() . '/inc/structure/sidebars.php';

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

}
This archived topic is closed to new replies.