Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add product in cart by using Shopify

Tags:

shopify

I have a product based Website build in WordPress, i have used shopify for add to cart, but having some issue to add multipal products.

i am using this script for add to cart

 function buyButtonClickHandler(evt) {
 evt.preventDefault();
 evt.stopImmediatePropagation();
 var id = product.selectedVariant.id; 
 var id = evt.target.id; 
 var quantity;
 var cartLineItem = findCartItemByVariantId(id);
 quantity = cartLineItem ? cartLineItem.quantity + 1 : 1;
 addOrUpdateVariant(product.selectedVariant, quantity);
 setPreviousFocusItem(evt.target);
 $('#checkout').focus();


 }

this use for update quantity for products

 function updateQuantity(fn, variantId) {
    var variant = product.variants.filter(function (variant) {
        return (variant.id === variantId);
    })[0];
    var quantity;
    var cartLineItem = findCartItemByVariantId(variant.id);
    if (cartLineItem) {
        quantity = fn(cartLineItem.quantity);
        updateVariantInCart(cartLineItem, quantity);
    }
}

Any buddy have an idea please help.

like image 659
Husain Ahmed Avatar asked Aug 27 '16 11:08

Husain Ahmed


1 Answers

I have found solution of these issue

to add single or multi-pal product on 'add to cart' we have use this

    $('.cart').on('click', '.quantity-increment', function(event) {
        var variantId = $(this).data('variant-id');
        console.log(variantId);
        incrementQuantity(variantId);
        event.preventDefault();
    });

   
    $('.cart').on('click', '.quantity-decrement', function(event) {
        var variantId = $(this).data('variant-id');
        decrementQuantity(variantId);
        event.preventDefault();

    });

   function decrementQuantity(variantId) {
      updateQuantity(function(quantity) {
        return quantity - 1;
      }, variantId);
   }

  function incrementQuantity(variantId) {
    updateQuantity(function(quantity) {
        return quantity + 1;
    }, variantId);
}
like image 175
Husain Ahmed Avatar answered Jan 02 '23 12:01

Husain Ahmed