Purpose: Display certain products ( either by tags, by title or by recommendation ) in the Checkout Page.
I tried to create a list of products in the checkout page using the product object but it looks like it's not recognized in the checkout.liquid. So I tried the Ajax APIs which does not need authentication. I was able to fetch data from cart.js using this code:
jQuery.ajax({ url: '/cart.js', dataType: 'json' })
and while these codes are present inside the <head></head>:
{{ '//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js' | script_tag }}
{{ 'api.jquery.js' | shopify_asset_url | script_tag }}
Issue: Now, I tried fetching from Products by using the codes found here: https://shopify.dev/docs/themes/ajax-api/reference/product-recommendations , however I can't make it work. I even replaced the product_id value to one of our products' in the fetch url code but still it's not fetching:
jQuery.getJSON("/recommendations/products.json?product_id={my product's id}&limit=4", function( response ) {
var recommendedProducts = response.products;
if (recommendedProducts.length > 0) {
var firstRecommendedProduct = recommendedProducts[0];
alert(
"The title of the first recommended product is: " +
firstRecommendedProduct.title
);
}
});
Thank you for your help.
That should work. Are you replacing {my product's id} with an actual id of a product? Your code should look like the code below, with a numeric product id instead of {my product's id}.
jQuery.getJSON("/recommendations/products.json?product_id=334342160424&limit=4", function( response ) {
var recommendedProducts = response.products;
if (recommendedProducts.length > 0) {
var firstRecommendedProduct = recommendedProducts[0];
alert(
"The title of the first recommended product is: " +
firstRecommendedProduct.title
);
}
});
Replace 334342160424 with one of your shop's product's id.
Hi @kenput3r , I found the issue. I'm was using the wrong product id. The code was working but it's not fetching the data because the product id that I was using was actually the cart item's id ( data.items[0].id ). After using the actual product id ( data.items[0].product_id ) I am now able to receive API responses from the recommendation JSON. Thank you for your help.
User | Count |
---|---|
16 | |
12 | |
7 | |
5 | |
4 |