# Add Wishlist counter \[yith] JS

**Goals:**

* **You can create wishlist counter on your e-commerce website (especially on our theme).**

**Requirement:**

* **Install plugin YITH WooCommerce Wishlist**

Tutorial:

1. After you already install the plugin, you can adjust the settings in the plugin as needed. \
   you can see an example below:

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2FiSzSQcU9EmXjqnxSDqfv%2Fimage.png?alt=media&#x26;token=699f3c87-6cd7-461c-9b8b-e51f5f435a74" alt=""><figcaption></figcaption></figure>

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2F0goldapIxLoZHUhqaITK%2Fimage.png?alt=media&#x26;token=a8f7fa2d-15ae-4fd7-9a58-9506054b7d5d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2FI2eVizlb1f3T3psAjliv%2Fimage.png?alt=media&#x26;token=078d9bd4-645e-47ef-b21f-319557588f61" alt=""><figcaption></figcaption></figure>

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2FSnAnAJREita5Do2LJhSq%2Fimage.png?alt=media&#x26;token=976bddf0-c038-448a-95e1-9f1741b2465c" alt=""><figcaption></figcaption></figure>

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2F5T97VUB2mtX67b4aCZwg%2Fimage.png?alt=media&#x26;token=86958ead-b4a8-4b99-a18f-13e9cbdb29eb" alt=""><figcaption></figcaption></figure>

2. after that, we will create a shortcode, open your theme on vs code then you can go to **your\_thems > includes,** you can add new folder like the picture below:

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2FEZls4cKqEK7cU9M67oKH%2Fimage.png?alt=media&#x26;token=aab2d170-e531-4fe3-9529-34680e63f063" alt=""><figcaption></figcaption></figure>

Here the code **class-WishlistIcon.php,** you can cop&#x79;**:**

```php
<?php

/**
 * The template for Create Custom Post Type - Laten Beleggen used in WP-admin
 *
 * Author: Andi Gustanto and Muhammad Rizki
 *
 * @package HelloElementor
 */

defined('ABSPATH') || die("Can't access directly");

class WishlistIcon
{
    public function __construct()
    {
        if (defined('YITH_WCWL') && !function_exists('yith_wcwl_get_items_count')) {
            add_shortcode('yith_wcwl_items_count', [$this, 'yith_wcwl_get_items_count_func']);
        }
        if (defined('YITH_WCWL') && !function_exists('yith_wcwl_ajax_update_count')) {
            add_action('wp_ajax_yith_wcwl_update_wishlist_count', [$this, 'yith_wcwl_ajax_update_count']);
            add_action('wp_ajax_nopriv_yith_wcwl_update_wishlist_count', [$this, 'yith_wcwl_ajax_update_count']);
        }
        if (defined('YITH_WCWL') && !function_exists('yith_wcwl_enqueue_custom_script')) {
            add_action('wp_enqueue_scripts', [$this, 'yith_wcwl_enqueue_custom_script'], 20);
        }
    }

    public function yith_wcwl_get_items_count_func()
    {
        ob_start();

        require __DIR__ . '/template-item-count.php';
        return ob_get_clean();
    }

    public function yith_wcwl_ajax_update_count()
    {
        wp_send_json(array(
            'count' => yith_wcwl_count_all_products()
        ));
    }

    public function yith_wcwl_enqueue_custom_script()
    {
        wp_add_inline_script(
            'jquery-yith-wcwl',
            "
              jQuery( function( $ ) {
                $( document ).on( 'added_to_wishlist removed_from_wishlist', function() {
                  $.get( yith_wcwl_l10n.ajax_url, {
                    action: 'yith_wcwl_update_wishlist_count'
                  }, function( data ) {
                    $('.yith-wcwl-items-count').children('span').html( data.count );
                  } );
                } );
                $(document).on('added_to_cart', () => {
                  $.get( yith_wcwl_l10n.ajax_url, {
                    action: 'yith_wcwl_update_wishlist_count'
                  }, function( data ) {
                    $('.yith-wcwl-items-count').children('span').html( data.count );
                  } );
                });
              });
            "
        );
    }
}

new WishlistIcon();
```

Here the code **template-item-count.php,** you can cop&#x79;**:**

```html
<a href="<?php echo esc_url(YITH_WCWL()->get_wishlist_url()); ?>">
    <span class="yith-wcwl-items-count wishlish-icon-wrapper">
        <span class="wishlish-icon-wrapper__count"><?php echo esc_html(yith_wcwl_count_all_products()); ?></span>
        <i class="yith-wcwl-icon fa fa-heart wishlish-icon-wrapper__heart"></i>
    </span>
</a>
```

Here the code **autoload.php,** you can cop&#x79;**:**

```php
<?php

/**
 * Used for ....
 *
 * @package HelloElementor
 */

defined('ABSPATH') || die("Can't access directly");
require_once __DIR__ . '/wishlist-icon/class-WishlistIcon.php';

```

3. You can add your shortcode on your website **\[yith\_wcwl\_items\_count]**

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2FKEPMdot7o7q8W8HoSxE9%2Fimage.png?alt=media&#x26;token=13930b7b-c3dd-4525-8fe5-b8d8d0bd8bad" alt=""><figcaption></figcaption></figure>

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2FXYhlFwBwd0RZ6QEjXITY%2Fimage.png?alt=media&#x26;token=2d1a4e3e-7291-4b83-8e60-2ecc1d213ad8" alt=""><figcaption></figcaption></figure>

4. You can add code for style on folder scss, you can see image below:

<figure><img src="https://115803331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F705N8OeBYBxKGzhvaDVF%2Fuploads%2F9Drv6Cpt1a3aqlDN8ilU%2Fimage.png?alt=media&#x26;token=8df52d9f-ddc8-4b42-8170-05f494efa47a" alt=""><figcaption></figcaption></figure>

here the code **wishlist.scss,** for styling the icon hear&#x74;**:**

```scss
.wishlish-icon-wrapper {
	position: relative;
	margin-right: 7px;

	&__count {
		background: $accent;
		color: $white;
		position: absolute;
		right: -7px;
		top: -7px;
		min-width: 1.6em;
		height: 1.6em;
		font-size: 10px;
		font-weight: 600;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	&__heart {
		font-size: 18px;
		margin-top: 3px;
		color: $white;
	}

	&:hover {
		.wishlish-icon-wrapper__heart {
			color: $accent !important;
		}
	}
}
```

Don't forget to update your **plugin.scss,** add this cod&#x65;**:**

```
@import "./wishlist/wishlist";
```

Notes: for styling you can change it according to the color of your website.<br>

here the reference link: <https://support.yithemes.com/hc/en-us/articles/115001372967-Wishlist-How-to-count-number-of-products-wishlist-in-ajax>

\
**Good Luck.**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://madeindonesia.gitbook.io/mi-guide/plugins/woocommerce/add-wishlist-counter-yith-js.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
