Liquid, JavaScript, themes, sales channels
To continue receiving payouts, you need to secure your account by turning on two-step authentication. If two-step authentication is not turned on your payouts will be paused. Learn more
Clicking the Add button cause this issue:
Uncaught ReferenceError: routes is not defined
at ProductForm.onSubmitHandler (product-form.js?v=106714731521289003461653532105:33:16)
Current code that it takes me to shows the error lies in the following:
fetch(`${routes.cart_add_url}`, config)
.then((response) => response.json())
.then((response) => {
if (response.status) {
this.handleErrorMessage(response.description);
I haven't changed any of the coding at all so I'm not sure what has happened.
Solved! Go to the solution
This is an accepted solution.
Not sure if making a difference but can you try this code instead of the routes.cart_add_url
fetch(window.Shopify.routes.root + 'cart/add.js', config)
Hi @hnt88,
Follow the example code provided by shopify. You need to add the variantID (or productID if no variant), and the quantity. Refer to documentation here.
let formData = {
'items': [{
'id': 36110175633573,
'quantity': 2
}]
};
fetch(window.Shopify.routes.root + 'cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
})
.then(response => {
return response.json();
})
.catch((error) => {
console.error('Error:', error);
});
Oh by bad. I thought you are writing a fetch function. You might have miss some comma or semi-colon. If you did not change much on the product-form.js. I recommend, to add a new Dawn theme. Copy the product-form.js contents and replace the product-form.js contents in your old theme.
@made4Uo All good. I've just tried to do a exact copy of the product-form new Dawn theme and replaced it on my old theme and nothing had changed. So I'm not sure what else could've happened.
This is an accepted solution.
Not sure if making a difference but can you try this code instead of the routes.cart_add_url
fetch(window.Shopify.routes.root + 'cart/add.js', config)
@made4Uo you absolute legend! Seems like the template had incorrect coding from the start. Your solution fixed it!
Glad to know
Actually, this routes were set in the theme.liquid.
Perfect solution! Worked like a charm. 😎
NB: I'm using Sense theme.
Learn these 5 things I had to learn the hard way with starting and running my own business
By Kitana Jan 27, 2023Would you love to unleash the unbridled power of the Google Shopping Channel into your sho...
By Gabe Jan 6, 2023How can you turn a hobby into a career? That’s what Emmanuel did while working as a wa...
By Skye Dec 30, 2022