Show Inventory Levels By Location On Your Product Page
This workflow and code snippet for your product page will pull inventory levels by location on the fly and show them on your product page.
Topics:
Watch the demo
Product page sample
![online store product page preview inventory](https://blog.getmesa.com/wp-content/uploads/Screenshot-2024-04-16-at-8.29.25 AM-1024x434.png)
Preview workflow steps
The workflow simply responds to a web request that will be coming via Javascript and responds with the inventory levels.
![](https://blog.getmesa.com/wp-content/uploads/Screenshot-2024-04-16-at-8.15.57 AM-1024x478.png)
Liquid code for theme
You’ll want to copy and paste this code into a Custom Liquid section in the Theme Editor for your Product Page.
<fieldset class="js product-form__input">
<legend class="form__label">Available Inventory</legend>
<div id='mesa-output'></div>
</fieldset>
<script type="text/javascript">
const WEBHOOK_URL = 'https://webhooks.getmesa.com/v1/kalen-jordan-dev/webrequest/661e710bfd648171430b5c1e/661e710e20fe32389a0e0652.json?apikey=pobbFlA9BN73tE4gkbUsb5DLnZnwfIKi8g8UtM7U';
function fetchJSON(url) {
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
}
let variantId = '{{product.selected_or_first_available_variant.id}}';
url = WEBHOOK_URL + '&variant_id=' + variantId;
fetchJSON(url)
.then(response => {
let data = response.response;
console.log(data);
let levels = data.map(object => {
return object.location.name + ": " + object.quantities[0].available
});
let output = levels.join('<br>');
console.log(output);
const outputDiv = document.getElementById('mesa-output');
outputDiv.innerHTML = output;
});
</script>