jquery - How to update the WooCommerce cart Icon to show new products added with JavaScript

I created some custom links to add new products to the cart using the "href" attribute, my code looks like thi

I created some custom links to add new products to the cart using the "href" attribute, my code looks like this.

<a class="line1" id="btn2" href="?add-to-cart=5207">Example</a>

But I didn't like the fact that every time I added a product it refreshed the whole page so I started looking for a different approach, and I found a way to do it with some jQuery code, It's adding the product but as the page is not reloading the cart icon doesn't update until I reload the page manually. The code looks like this.

jQuery(document).ready(function( $ ){   
    $('#buy').click(function(e) {
       console.log('Working!!')
       e.preventDefault();
       prodId = $(this).attr("data-prod-id");
       addToCart(prodId);
       return false;
    });    

    function addToCart(p_id) {
       $.get('?add-to-cart=' + p_id, function() {
           // call back
       });
     }
});

Is there a way to add some code to my jQuery function in order to update only the cart icon so that it shows the new products added?

I created some custom links to add new products to the cart using the "href" attribute, my code looks like this.

<a class="line1" id="btn2" href="?add-to-cart=5207">Example</a>

But I didn't like the fact that every time I added a product it refreshed the whole page so I started looking for a different approach, and I found a way to do it with some jQuery code, It's adding the product but as the page is not reloading the cart icon doesn't update until I reload the page manually. The code looks like this.

jQuery(document).ready(function( $ ){   
    $('#buy').click(function(e) {
       console.log('Working!!')
       e.preventDefault();
       prodId = $(this).attr("data-prod-id");
       addToCart(prodId);
       return false;
    });    

    function addToCart(p_id) {
       $.get('?add-to-cart=' + p_id, function() {
           // call back
       });
     }
});

Is there a way to add some code to my jQuery function in order to update only the cart icon so that it shows the new products added?

Share Improve this question asked Jul 13, 2020 at 20:28 Jonattan SalcedoJonattan Salcedo 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Heres a nice article describing how to do this, but a very different approach to yours : https://aceplugins/ajax-add-to-cart-button-on-the-product-page-woocommerce/

Since you know how many items are being added to the cart (1 based on your link) you could just fake some addition and upate the cart icon value

Assuming the cart icon number has an id 'cart-icon' Eg :

 function addToCart(p_id) {
   $.get('?add-to-cart=' + p_id, function() {
      // SO IF YOU ALREADY HAD 3 IN CART IT WILL NOW BE 4
       $('#cart-icon').text( $('#cart-icon').text() + 1);

   });
 }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742264168a4411466.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信