Ad

Add Product In Cart By Using Shopify

- 1 answer

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.

Ad

Answer

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);
}
Ad
source: stackoverflow.com
Ad