Add multiple products to cart- check if same item

PHOTO EMBED

Mon Feb 28 2022 13:45:09 GMT+0000 (Coordinated Universal Time)

Saved by @PIzmAR #javascript

function familyPack() {
  $('.btn-custom1').click(function(){
    jQuery.post('/cart/clear.js');
    var attr = $(this).attr('data-discount')
    console.log(attr)
    var prod1
    var prod2
    var prod3
    var prod4
    var newArr = []
    var items = []
    if (attr == 'family-pack') {
      prod1 = $('#familySelector1 option:selected').val()
      prod2 = $('#familySelector2 option:selected').val()
      prod3 = $('#familySelector3 option:selected').val()
      prod4 = $('#familySelector4 option:selected').val()
      newArr.push (prod1, prod2, prod3, prod4)
    }
    if (attr == 'couple-pack') {
      prod1 = $('#coupleSelector1 option:selected').val()
      prod2 = $('#coupleSelector2 option:selected').val()
      newArr.push (prod1, prod2)
    }
    if (attr == 'single-pack') {
      prod1 = $('#singleSelector1 option:selected').val()
      newArr.push (prod1)
    }

    
    var counter = 0
    for (let i = 0; i < newArr.length; i++) {
      var prod = newArr[i]
      var qty = 1
      if (i == 0 ) {
        items.push({id:prod, quantity: qty})
      }
      var itemlength = items.length
      if (i >= 1) {
        counter 
        if (items[counter].id == newArr[i]) {
          items[counter].quantity = items[counter].quantity+1
        }
        else {
          items.push({id:prod, quantity: qty})
          counter++
        }
      }
    }
    console.log(items)
    setTimeout(() => {
		jQuery.post('/cart/add.js', {items});
    }, 400)
  	var getDiscount = $(this).attr('data-discount')
    var discount 
    if (getDiscount == 'family-pack') {
    	discount = "FAMILY"
    }
    if (getDiscount == 'couple-pack') {
    	discount = "COUPLE"
    }
    if (getDiscount == 'couple-pack' || getDiscount == 'family-pack') {
      setTimeout(() => {
          window.location = '/checkout?discount='+discount
      }, 1300)
    }
    if (getDiscount == 'single-pack' ) {
      setTimeout(() => {
          window.location = '/checkout'
      }, 1300)
    }
  })

}
familyPack()
content_copyCOPY

I made this on Sleepwell landing page

https://sleepwelltech.co/pages/advertorial