Add to cart multiple items?

PHOTO EMBED

Thu Nov 18 2021 08:15:40 GMT+0000 (UTC)

Saved by @JureVidmar #javascript

              function onlyUnique(value, index, self) { 
                  return self.indexOf(value) === index;
              }

              function countOccurances(a,i)
              {
               var result = 0;
               for(var o in a)
                if(a[o] == i)
                 result++;
               return result;
              }

		
            $('#submit-form-button-bundle').click(function(e){   
              e.preventDefault();
              
              var allBeds = [$("#primary_bed").val(), $('#secondary_bed').val(), 		$('#product_3').val(), $('#product_4').val()];
              var uniqueBeds = allBeds.filter( onlyUnique );
              
              //console.log(uniqueOodies);
              //console.log(countOccurances(allOodies, $("#oodie-color").val()));
              
              jsonObj = []
              var arrayLength = uniqueBeds.length;
              for (var i = 0; i < arrayLength; i++) 
              {    
                  item = {}
                  item ["quantity"] 	= countOccurances(allBeds, uniqueBeds[i]);
                  item ["id"] 			= uniqueBeds[i];
                  jsonObj.push(item);
              }
              
              $.ajax({
                url: "/cart/add",
                type: "post",
                data: { items: jsonObj },
                success: function(){
                  
                window.location.href = "/checkout?discount=BLACKFRIDAY-MEGA-BUNDLE-4-PIECE";
                  
                },
                error: function(){
                }
              });
            });

            $('.single-option-selector').change(function(){

              var selected_option = $('.single-option-selector').val();
              
              // adding bed variant ids
              if(selected_option.indexOf("Navy - Small") >= 0){
                $('#primary_bed').val('29012044808226');
              }

              else if(selected_option.indexOf("Grey - Small") >= 0){
                $('#primary_bed').val('29012045103138');
              }

              else if(selected_option.indexOf("Pink - Small") >= 0){
                $('#primary_bed').val('28915153764386');
              }
              
              else if(selected_option.indexOf("Navy - Medium") >= 0){
                $('#primary_bed').val('31683365797911');
              }
              
              else if(selected_option.indexOf("Grey - Medium") >= 0){
                $('#primary_bed').val('31683365765143');
              }
              
              else if(selected_option.indexOf("Pink - Medium") >= 0){
                $('#primary_bed').val('31683365830679');
              }
              
              else if(selected_option.indexOf("Navy - Large") >= 0){
                $('#primary_bed').val('28915153698850');
              }
              else if(selected_option.indexOf("Grey - Large") >= 0){
                $('#primary_bed').val('28915153731618');  
              }
              else if(selected_option.indexOf("Pink - Large") >= 0){
                $('#primary_bed').val('29012060799010');
              }
              else if(selected_option.indexOf("Navy - Extra") >= 0){
                $('#primary_bed').val('31722116251671');
              }
              else if(selected_option.indexOf("Grey - Extra") >= 0){
                $('#primary_bed').val('31722116218903');
              }
              else if(selected_option.indexOf("Pink - Extra") >= 0){
                $('#primary_bed').val('31722116284439');
              }

              else
              {
                console.log('Some thing went wrong please check code for bugs');
              }
              
              if(selected_option.indexOf("Extra") >= 0){
              	$('#price_1').val('189');
              } 
              else if(selected_option.indexOf("Large") >= 0){
              	$('#price_1').val('129');
              }
              else if(selected_option.indexOf("Medium") >= 0){
              	$('#price_1').val('114');
              }
              else if(selected_option.indexOf("Small") >= 0){
              	$('#price_1').val('99');
              }

            });
            $('.single-option-selector-2').change(function(){........}
content_copyCOPY