Last active
October 18, 2024 15:11
-
-
Save savchukoleksii/5bed3047208176967a7b6961c6a69a1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{%- 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