Validating Required Field when clicking "Buy It Now" dynamic button

Hello,

On all of our product pages, we have a Delivery Date picker that is required. I was able to adjust to code to make that work when people select the “add to cart” button. However, when they select the “Buy it Now” button it will take them to checkout even if the delivery date field is blank. I have read that there is some additional code needed to do this. Can anyone help me with this?

Thanks!

1 Like

Hello @frontporchgift ,

  1. On initial view make ‘Buy now’ button hidden. Now create a JS function which will detect change in date input field as any change happened change ‘buy now’ to display block.

  2. Once page loaded add disabled attribute to ‘buy now’ button and as there is change happened in date picker field remove attribute disabled.

Hope these steps will give you idea to write code.

Thanks

Hi @frontporchgift ,

Add below script to achieve the same

$("form[action$=\'/cart/add\'] button[type=submit]").click(function(evt){
   // Run Valdiation											  
   evt.preventDefault();										  
   evt.stopPropagation();
   if(true){										 
       $("form[action$=\'/cart/add\']").submit();											  
   }else{
       return;
   }
});

Hope it will help.

Hi Hari1_prasad,
I’m also interested in this solution but it doesn’t seem to work. Maybe I didn’t paste the code in the right section. Where should I add this code?
Kind regards,
Guido

where to add this code?

Can you please provide detailed code and screenshots and also where exactly to add the code