$.ajax({
type: 'POST',
url: '/cart/add.js',
data: {
quantity: 1,
id: form_data
},
success: function(response) {
console.log('in success');
var url = $("#af-btn").attr("href");
window.location.href = $("#af-btn").attr("href");
},
error: function(response) {
console.log(response + "in error");
var url = $("#af-btn").attr("href");
//window.location.href = $("#af-btn").attr("href");
},
complete: function(response) {
console.log(response);
}
});
Above is my AJAX request. Below is the response Object.
Add to cart to shows anonymous behavior, sometimes product gets added and sometimes product is not added. Can’t figure it out. Any ideas?
Below is the form_data
How I retrieve its value,
var form_data = $('form[action="/cart/add"]').find('select[name="id"]').find(":selected").val();
2
Answers
This is how I solved it for using the Shopify Ajax Api in Shopify app whose script was added through Shopify Admin API ScriptTag.
1- Load Shopify jQuery Ajax Api Wrapper
2- Then use it like below :
Resource Link : API jQuery Ajax Shopify Sandbox
The standard AJAX call was throwing errors for me as well in one instance( even thought the product was included in the cart ).
So instead I used their API for the AJAX call. Here is a link to all of the functions: http://mayert-douglas4935.myshopify.com/pages/api
For example adding the items is easy as:
PS:
Have in mind that you will be required to include this script in order to have access to the calls: