Skip to content

Instantly share code, notes, and snippets.

@sebastianrothbucher
Created November 4, 2018 17:54
Show Gist options
  • Save sebastianrothbucher/3213d1399816b171d77879d5cab18976 to your computer and use it in GitHub Desktop.
Save sebastianrothbucher/3213d1399816b171d77879d5cab18976 to your computer and use it in GitHub Desktop.
Shoppable content WP - Magento
<!-- wp:paragraph -->
Now this is going to be cool: you can shop for hoodies right here:
<span id="testspan"></span>
<script language="JavaScript">
window.addEventListener('load', function() { // (need magrest defined as proxy); could easily use React et al to create sth and a one-line statement loading it
fetch('/magrest/V1/products-render-info?storeId=1&currencyCode=EUR&searchCriteria[filter_groups][0][filters][0][field]=entity_id&searchCriteria[filter_groups][0][filters][0][value]=2&searchCriteria[filter_groups][0][filters][0][condition_type]=eq').then(res => res.json()).then(res => {
var a_ = document.createElement('a');
a_.target='_blank';
a_.href=res.items[0].url;
var img_ = document.createElement('img');
img_.src = res.items[0].images[0].url;
a_.appendChild(img_);
var t_ = document.createElement('span');
t_.innerText = ' ' + res.items[0].name + ' ';
a_.appendChild(t_);
var p_ = document.createElement('span');
p_.innerHTML = res.items[0].price_info.formatted_prices.regular_price;
a_.appendChild(p_);
document.getElementById('testspan').appendChild(a_);
});
});
</script>
<!-- /wp:paragraph -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment