Thank you both for pointing me in the right direction. I feel like I’m so close, but I’ve been getting unexpected results. Here’s what I’ve done…
I set up an alternative template for collections called collections.output.liquid
. So “output” is what I use in the “view” url parameter: &view=output
. The alternative template page has layout none
at the top, and I use liquid to process the filtered collection.products
that is already available without needing to do much. So I just serialize the filtered collection.products
object and echo it. When I manually point a browser tab to the url with the filter and sort parameters:
/collections/all-paintings?filter.v.availability=1&sort_by=title-ascending&view=output
just to test it, I get exactly what I expect: a string matching the serialized object I echoed. It just fills the browser window. That seemed like a good start. All the products and their details are there. Great… except that when I use the same URL in an AJAX request, I get an object containing some data about a single collection, no products. It’s not even possible for my liquid code to produce this output. So something odd is happening here.
Here’s the rest of the details on how I set everything up:
On my regular collections.json
template is the main-collection-product-grid.liquid
section. I have inserted a style tag linking to a custom JS asset (deferred), and in that document is the following code:
function AA_fetchFilteredProducts()
{
const pathname = window.location.pathname;
let queryStr = window.location.href.split('?')[1];
if ( queryStr )
{
queryStr += ( queryStr.includes('view=output') ) ? '' : '&view=output'
$.ajax(
{
url: `${pathname}?${queryStr}`,
method: 'GET',
dataType: 'JSON',
cache: false,
success: function(productsFetched){
console.log(productsFetched);
}
}
);
}
}
$(document).ready( function() {
AA_fetchFilteredProducts();
});
My AA_fetchFilteredProducts
function runs on page load, and I modified the Dawn theme’s facets.js
document so that my function is also called when any filters on the product grid are changed. It all works except that the results of the AJAX fetch is not even a batch of products, it’s an object with data about a single collection, and not even all the available data.
Why would Shopify be returning a completely different result when the same URL is used in a browser window, versus in an AJAX call?