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.

Watch the demo

Product page sample

online store product page preview inventory

Preview workflow steps

The workflow simply responds to a web request that will be coming via Javascript and responds with the inventory levels.

workflow steps

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.

Note: this works for Dawn themes but custom themes may require some specific adjustments to this.

<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-test-store/webrequest/66c8f84efa82fcb17905350d/66c8f84efa82fcb17905350f.json?apikey=p234WHl7GU5B9UC5xpGg35JrhXajoW38aWlhKxTI';

  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);
      });
  }

  function showInventoryForVariant(variantId) {
    const outputDiv = document.getElementById('mesa-output');
    outputDiv.style.opacity = 0.5;

    url = WEBHOOK_URL + '&variant_id=' + variantId;
    console.log("MESA fetching inventory levels data: " + url);
    fetchJSON(url).then((response) => {
      let data = response.response;
      console.log("MESA inventory levels data: ", data);

      let levels = data.map((object) => {
        return object.location.name + ': ' + object.quantities[0].available;
      });

      let output = levels.join('<br>');
      outputDiv.style.opacity = 1;
      outputDiv.innerHTML = output;
    });
  }

  window.addEventListener('DOMContentLoaded', () => {
    let variantId = '{{product.selected_or_first_available_variant.id}}';
    showInventoryForVariant(variantId);

    subscribe(PUB_SUB_EVENTS.variantChange, (event) => {
      console.log('Variant change event:', event);
      let variantId = event.data.variant.id;
      showInventoryForVariant(variantId);
    });
  });
</script>

Free workflow

Note: This template runs each time the product page is loaded so we recommend the Unlimited plan.

Questions about this workflow? Contact our partner:

Kalen Jordan

MESA Service Partner

What you should do next

Whenever you’re ready…follow these 4 simple steps to get started:

  1. Add MESA to your Shopify store. The best way to get started is by adding the MESA app from the Shopify App store. Every plan includes the first 7-days free so you can be assured your workflow is running properly.
  2. Import this workflow. Download this .ZIP file then import it into your MESA workflows.
  3. Questions? Contact Kalen Jordan. MESA Service Partners can help personalize workflows or make any automations your business requires.
  4. If you know another merchant who’d enjoy reading this, share it with them via LinkedIn, YouTube, Twitter, or Facebook.

Automate anything and get more from your apps.
For free.

Start free trial
background: abstract lines

Automate everything with MESA

Make any workflow and get more from your existing apps.

Try MESA for free, 7-day trial included.