Products without variants not being added to cart

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 :slightly_smiling_face:

@Carlos138

Replace this code :-

if(dataTitle == selectedVariantColor){
selectedVariantId = dataId
}

With this code:-
selectedVariantId = dataId.

So, basically just remove the if condition

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.