Skip to content

Instantly share code, notes, and snippets.

@savchukoleksii
Last active May 17, 2024 13:42
Show Gist options
  • Save savchukoleksii/5bed3047208176967a7b6961c6a69a1a to your computer and use it in GitHub Desktop.
Save savchukoleksii/5bed3047208176967a7b6961c6a69a1a to your computer and use it in GitHub Desktop.
async function getProductById(id) {
const handle = (await fetch(`/search/suggest.json?q=id:${id}&resources[type]=product&limit=1`)
.then(response => response.json())
.then(response => response.resources.results.products.shift())).handle;
return await fetch(`/products/${handle}.js`).then(response => response.json());
}
const product = await getProductById(2083844620358);
console.log(product);
async function getProductsByIds(ids = []) {
const search = encodeURI(ids.map(id => {
return `id:${id}`;
}).join(" OR "));
const response = (await axios.get(`/search?q=${search}&type=product&view=products-json`)).data;
const html = document.createElement("div");
html.innerHTML = response;
return JSON.parse(html.querySelector('[data-json-response]').innerHTML);
}
{%- layout none -%}
{%- assign result = "" -%}
{%- paginate search.results by 50 -%}
{%- for item in search.results -%}
{%- if item.object_type != 'product' -%}
{%- continue -%}
{%- endif -%}
{%- if result != "" -%}
{%- assign result = result | append: "," -%}
{%- endif -%}
{%- capture result_item -%}
{{- item | json -}}
{%- endcapture -%}
{%- assign result = result | append: result_item -%}
{%- endfor -%}
{%- endpaginate -%}
<script data-json-response type="application/json">[{{- result -}}]</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment