• Resolved userwhosme

    (@userwhosme)


    Hi,
    I want to add an item counter to the wishlist page link in the top bar menu of my site. I tried a few solutions with adding a [yith_wcwl_items_count] shortcode, but I still do not get a counter in the menu link. Could someone please help me add an item counter to the link? Thanks in advance! ??

    The code I’m currently using in my functions.php of my child theme:

    if ( function_exists( 'YITH_WCWL' ) ) {
    	if ( ! function_exists( 'yith_wcwl_add_counter_shortcode' )  ) {
    		function yith_wcwl_add_counter_shortcode() {
    			add_shortcode( 'yith_wcwl_items_count', 'yith_wcwl_print_counter_shortcode' );
    		}
    	}
    
    	if ( ! function_exists( 'yith_wcwl_print_counter_shortcode' )  ) {
    		function yith_wcwl_print_counter_shortcode() {
    			?>
    			    				<?php echo "test"; ?>
    				<?php echo esc_html( yith_wcwl_count_all_products() ); ?>
    			<?php
    		}
    	}
    	add_action( 'init', 'yith_wcwl_add_counter_shortcode' ); 

    Sincerely,
    userwhosme

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author YITHEMES

    (@yithemes)

    Hi userwhosme

    I checked the provided code and it is a bit strange (I can see an echo $test that makes little sense, and most of all, shortcode callback should return shortcode template, instead of printing it)

    Maybe you can give a look at this complete article, that will give you tools to print a counter in your page using shortcode, and even update it via AJAX once productare added/removed from wishlist

    Hope this helps

    Thread Starter userwhosme

    (@userwhosme)

    @yithemes Hi, you’ve seemingly replied to this topic a little more than an hour and 30 minutes ago (“Last reply from: YITHEMES / Last activity: 1 hour, 35 minutes ago”) but unfortunately, I cannot see your reply ??

    Plugin Author YITHEMES

    (@yithemes)

    Hi userwhosme

    Our comment is currently held for moderation, and a moderator will soon -hopefully- unlock it
    Unfortunately I feel like this comment too will be held for moderation too, so it won’t be of any help, but I hope it will all make sense eventually ??

    Thread Starter userwhosme

    (@userwhosme)

    @yithemes Oh, I can see the replies now! I didn’t know about the moderation, oops, I’m new here! ?? Sorry about the confusion.

    I tried the code in the article and it worked, but only in the body of the page, not in the top bar menu link, which is where I want the counter to appear. I put the shortcode in the menu item’s Navigation Label like this: Wishlist <?php echo do_shortcode('[yith_wcwl_items_count]'); ?>, am I doing something wrong?

    Sincerely,
    userwhosme

    • This reply was modified 4 years ago by userwhosme.
    Plugin Author YITHEMES

    (@yithemes)

    It should work everywhere; what do you see as output in your topbar?
    Could you please share an url where I can look at a live example of your implementation?

    Thread Starter userwhosme

    (@userwhosme)

    @yithemes I just see the text “Wishlist” instead of “Wishlist” followed by the number of items. Here is the live implementation.

    Plugin Author YITHEMES

    (@yithemes)

    I’m afraid you added PHP code in a place where it isn’t allowed (here a screen that should better explain the problem)

    How did you add that item to your topbar? It seems like you’re entering a menu item for that specific location
    In this case I suggest you to do what follows:

    1. Place this as label for your menu item: Wishlist [yith_wcwl_items_count]
    2. Add this snippet of code at the end of functions.php file of your theme or child theme

    add_filter( 'wp_nav_menu_items', 'yith_wcwl_menu_items_counter' );
    
    if ( ! function_exists( 'yith_wcwl_menu_items_counter' ) ) {
    	function yith_wcwl_menu_items_counter( $items ) {
    		$items = str_replace( '[yith_wcwl_items_count]', do_shortcode( '[yith_wcwl_items_count]' ), $items );
    		return $items;
    	}
    }
    

    Please, let me know if this works as expected

    Thread Starter userwhosme

    (@userwhosme)

    @yithemes It’s working now!!! Thank you so much for all the help! ??

    Plugin Author YITHEMES

    (@yithemes)

    You’re welcome!

    We are doing our best to improve our plugins. Our target is to develop and release the best free plugins for WooCommerce, but to achieve this we need your help. Please leave a good review to support us and help our growth ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Wishlist item counter in top bar menu of website’ is closed to new replies.