Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drabbytux/d97dda76b524c7ad1ab1840daad408d2 to your computer and use it in GitHub Desktop.
Save drabbytux/d97dda76b524c7ad1ab1840daad408d2 to your computer and use it in GitHub Desktop.
Get custom information for product javascript for theme.js
jQuery(function($) {
$('form[action="/cart/add"]').submit(function() {
var formIsValid = true;
var message = "Please fill this out and you will be able to add the item to your cart.";
$(this).find('[name^="properties"]').filter('.required, [required="required"]').each(function() {
$(this).removeClass('error');
if (formIsValid && $(this).val() == '') {
formIsValid = false;
message = $(this).attr('data-error') || message;
$(this).addClass('error');
}
});
if (formIsValid){
return true;
}
else {
alert(message);
return false;
}
}).find('input, select, textarea').focus(function() {
$(this).removeClass('error');
});
});
@glaxies
Copy link

glaxies commented Jun 2, 2017

Hi i followed your guide on shopify https://help.shopify.com/themes/customization/products/get-customization-information-for-products to make customization form required. However after adding in this jquery code to the bottom of my theme.js file but it still does not make the customization text compulsory. is there anywhere on this code that i need to edit?

I tried changing

find('[name^="properties"]

to my code

find('[name^="properties[Company Stamp Text]"]

@bjt1t2t3
Copy link

I have the same issue as @glaxies posted above.

Any ideas on why this code isn't making the customization field required?

Thanks!

@ajcsilva
Copy link

@glaxies @bjt1t2t3
Are you using one of the free Shopify themes? I'm using the "Simple" theme and was having a problem with this. Here's how I got it to work:

Change:
$('form[action="/cart/add"]').submit(function() {
to:
$('#AddToCart').on("click", function() {

There seems to be a name conflict with the .submit() method and the form's submit button name="submit" attribute. SO reference here.

@bjt1t2t3
Copy link

Thanks for your post @ajisilva. I am using this with the Simple theme and will try your change. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment