Shopify Ajax API fails when product is not published

Shopify Partner
115 0 7

I am using the Shopify Ajax API to retrieve product data in the product.liquid template. The issue is that when previewing an unpublished product the Ajax request fails.

Is there a way to fix this?

I don't really want to print the entire product data json into a script tag, but I guess if there is no way to access unpublished products through the api I will have to go this route.

Here is an example of what my script looks like:

$.getJSON('/products/'+ handle +'.js')
	.done(function (data) {
		deferred.resolve(data);
	})
	.fail(function (data) {
		var err = new Error('ERROR: The data request could not be fulfilled');
		deferred.reject(err);
		console.log(err);
	});

 

0 Likes
Shopify Expert
3939 16 328

It kinda makes sense. If a product is unpublished, AKA hidden, being able to show it on the front-end would be a major bonehead experience. So the query fails. Why not teach the query to just gracefully fail and move on? Since your algorithm depends on a server generated handle anyway, you could also just not populate your front-end calls with handles to hidden products. How are you doing that anyway?

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Shopify Partner
115 0 7

It would make sense if you were requesting an array of products, but I don't see how it would be an issue if you are requesting a specific product by it's handle. I can't think of any instance where you would request a hidden product other than when previewing that product as an admin.

Since your algorithm depends on a server generated handle anyway, you could also just not populate your front-end calls with handles to hidden products.

I think you might be misunderstanding me. This is not a request for multiple products, one of which happens to be hidden. I am specifically requesting data for the product who's page I am currently viewing(i.e. '/products/my-product'). So when an admin previews a hidden product the ajax request for that product's data fails.

If I were to skip this ajax request ffor hidden products then there would be some features that would only be available on published product pages.

How are you doing that anyway?

By using {{ product.handle }}

0 Likes
Shopify Expert
3939 16 328

I am lost. If you are an admin, just look at the product in the admin. Heck, add a .json to it and inspect the JSON. Now I have to ask you why you are on the front-end expecting hidden data you cannot access to be accessible? Makes no sense.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Shopify Partner
115 0 7

Now I have to ask you why you are on the front-end expecting hidden data you cannot access to be accessible?

I'm not. When you are logged in as an admin you can open up a product to edit it. On that page you can click "Preview"(or "View" if it is published) to view what the product page will look like once it is published. But because the product is not yet published(you are just previewing it) any features that rely on that AJAXed data will be broken.

 

I have no need for the hidden product data to be available on the front end for regular users. But when you are previewing a product page of a hidden product it is kind of important that all the features be working.

0 Likes
Shopify Expert
3939 16 328

If your theme relies on Ajax to get a product by handle, but you are actually rendering the product, are you not aware of just using the Liquid constuct:

var product = {{ product | json }};

Why bother going to the server to get what you already have?

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Highlighted
Shopify Partner
115 0 7

Preference. I prefer to keep my javascript out of the markup whenever possible and that particular request is cheap enough that I'm not worried about a performance hit.

The original question was basically can I make this work without changing my code base or do I have to switch to declaring the the data in the markup(like in your example). I suspected that there was no way around it, but I figured it was worth asking in case there was something in place to handle this.

0 Likes
Shopify Expert
3939 16 328

What is this markup thing you speak of? Why bother with an AJAX call asking for product handles crap when you already have that? Since you cannot view a hidden product on the front-end, your question is moot. If you want to preview a hidden product on the front-end, unhide it. 

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes
Shopify Partner
115 0 7

I don't think this question was particularly unresonable. Oh well, thanks for the responses anyways.

0 Likes
Shopify Expert
3939 16 328

It's not unreasonable. I think it makes sense. You are asking how to get hidden product info on the front end. You cannot. Now I understand your request. The answer I hope is clear. 

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes