Products without variants not being added to cart

Carlos138
New Member
1 0 0

Hey guys,

 

I am having an issue with a clients site, where if a product does not have a color variant it will not be added to the cart.

I know that that is the issue as I have tested it. But the developer is not currently available to make a change to the code.

 

Here is the js.liquid:

$(document).ready(function(){
 
  let selectedVariantColor;
  let selectedVariantId;
 
  function addToCart(){
    let quantityN = Number($('.dn_quantity').val());
    selectedVariantColor = $('.tt-options-swatch').find('li.active a').attr('data-value');
    $('div#secret_variants div').each(function(){
      let dataId = $(this).attr('data-id');
      let dataTitle = $(this).attr('data-title');
      if(dataTitle == selectedVariantColor){
        selectedVariantId = dataId
      }
    })
    console.log('variant and quantity', selectedVariantId,quantityN);
   
    $.post('/cart/add.js', {
      items:[
        {id: selectedVariantId,
         quantity: quantityN
        }
      ]
    }).always(function(data){
        window.location.href='/cart';
    })
    //     window.location.href='/cart';
 
  }
 
  // add to cart handler
 
  $('.addToCartBtn').on('click',()=> addToCart())
 
})
 
I appreciate any and all help
 
Replies 2 (2)
IttantaTech
Shopify Partner
510 55 92

@Carlos138 

Replace this code :- 

      if(dataTitle == selectedVariantColor){
        selectedVariantId = dataId
      }
 
With this code:- 
  selectedVariantId = dataId.
 
So, basically just remove the if condition
Thanks,
Ittanta Technologies Pvt. Ltd. | Shopify Expert
If reply is helpful, please Like and Accept Solution.
To hire us, contact us at info@ittanta.com
LitCommerce
Trailblazer
295 73 73

Hi @Carlos138,

Please send your site and if your site is password protected, please send me the password. or you can send me the theme name.

I will check it for you.

LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool.
Effortlessly sell on biggest marketplaces like Amazon, Etsy, eBay, Facebook etc with bulk listing tool, real-time sync & smart order management. Use LitCommerce free for 1-year now!