Developers

Hide purchase buttons when local pickup is not available

A Local code tweak that hides the buy buttons when the product is not available at the store the customer selected.

Krown support does not provide help with custom code. Local's pickup availability lines are information only: they tell customers whether a product is in stock at the store they chose. This page is for developers who want to go one step further and hide the buy buttons when the product is not available at that store, or when no store is chosen yet.

What the tweak does

  • On product cards and product pages that show a Compact pickup line, the Quick buy button and the product page buy buttons are hidden when the product is out of stock at the customer's store.
  • They are also hidden while the customer has not chosen a store.
  • Products available at the chosen store keep their buttons.

Customers can still buy the product. The real pickup and delivery choice happens at Shopify checkout, which the theme does not control. A customer who changes their preferred store, or adds the product to the cart another way, can still check out.

Before you start

The tweak only works where a Compact pickup line is on the page. Set these up first, then add the code:

Set up the store selector

Create your stores as Shopify locations with local pickup, and add one Store block per location in the Store selector section, with matching names. See Store selector section.

Add the Compact line to product pages

In the theme editor, open Products → Default product → Product page, select Add block → Pickup availability and set Style to Compact. Repeat on every product template.

Add the Local availability block to product grids

Open every section with product cards (Collections → Default collection → Product grid, Featured collection, Search results and the others) and select Add block → Local availability.

Theme editor home page with the Local availability block selected in the Featured collection section; the product cards show Available for pickup at Saint Germain or Out of stock at Saint Germain lines, highlighted.

Add the code

The code depends on your Local version. Your theme version is shown under the theme name in Online Store → Themes. Duplicate the theme before you edit it, and keep a copy of both changes: a theme update replaces assets/component-pickup-availability.js, so you make the change again after each update.

Replace the pickup availability script

In Online Store → Themes → … → Edit code, open assets/component-pickup-availability.js, replace its whole content with this code and select Save. It is the theme's own file with a few lines added that mark each product card or product page as available, unavailable or waiting for a store.

if ( typeof PickupAvailabilityCompact !== 'function' ) {

  class PickupAvailabilityCompact extends HTMLElement {

    constructor(){

      super();
      this.classList.add('active');

      this.storeSelector = document.querySelector('store-selector[data-main-selector]');
      if ( this.storeSelector ) {
        const STORE_CHANGED_EVENT = () => {
          this.#fetchAvailability(this.dataset.variantId, true);
        }
        this.storeSelector.addEventListener('storechanged', STORE_CHANGED_EVENT);
				const observer = new MutationObserver(()=>{
          this.storeSelector.removeEventListener('storechanged', STORE_CHANGED_EVENT);
					observer.disconnect();
				});
				observer.observe(this.parentElement, {
					attributes: false, childList: true, subtree: false
				});
      }

      if ( this.hasAttribute('data-static') ) {
        this.#fetchAvailability(this.dataset.variantId);
      } else {
        const widgetIntersection = (entries, observer) => {
          if (!entries[0].isIntersecting) return;
          observer.unobserve(this);
          this.#fetchAvailability(this.dataset.variantId);
        }
        new IntersectionObserver(widgetIntersection.bind(this), {rootMargin: '0px 0px 50px 0px'}).observe(this);
      }
      
    }

    #fetchAvailability(variantId, forceFetch = false) {

      const pickupAvailabilityData = this.querySelector('[data-js-pickup-availability-data]');

      if ( pickupAvailabilityData && ! forceFetch ) {
        this.#renderPreview(pickupAvailabilityData);
      } else {

        const variantSectionUrl = `${this.dataset.baseUrl.endsWith('/')?this.dataset.baseUrl:`${this.dataset.baseUrl}/`}variants/${variantId}/?section_id=helper-pickup-availability-compact`;

        fetch(variantSectionUrl)
          .then(response => response.text())
          .then(text => {
            const sectionInnerHTML = new DOMParser()
              .parseFromString(text, 'text/html')
              .querySelector('.shopify-section');
            this.#renderPreview(sectionInnerHTML);
          })
          .catch(e => {
            console.log(e);
          });

      }

    }
    
    #renderPreview(sectionInnerHTML) {
      
      const selectedStore = localStorage.getItem('selected-store');
      const product = this.closest('.product-item') || this.closest('product-page');

      if ( product ) {
        product.classList.remove('product-pickup--available', 'product-pickup--unavailable', 'product-pickup--unselected');
      }

      if ( this.storeSelector && selectedStore && this.storeSelector.storesList[selectedStore] ) {
        // search based on selected store 
        let storeFound = false;
        if ( selectedStore ) {
          sectionInnerHTML.querySelectorAll('.pickup-availability-alert').forEach(elm=>{
            if ( selectedStore === elm.dataset.store ) {
              this.innerHTML = elm.innerHTML.replace('{{ store }}', this.storeSelector.storesList[selectedStore]);
              storeFound = true;
              if ( product ) {
                product.classList.add(elm.querySelector('.alert--success') ? 'product-pickup--available' : 'product-pickup--unavailable');
              }
            }
          })
        }
        if ( !storeFound ) {
          this.innerHTML = sectionInnerHTML.querySelector('.pickup-availability-alert[data-default-unavailable]').innerHTML.replace('{{ store }}', this.storeSelector.storesList[selectedStore]);
          storeFound = true;
          if ( product ) {
            product.classList.add('product-pickup--unavailable');
          }
        }
        if ( !selectedStore || !storeFound ) {
          this.innerHTML = '';
        }
      } else {
        // search based on default store
        this.innerHTML = sectionInnerHTML.querySelector('.pickup-availability-alert[data-default-store]').innerHTML;
        if ( product ) {
          product.classList.add('product-pickup--unselected');
        }
      }

    }
    
  }

  if ( typeof customElements.get('pickup-availability-compact') === 'undefined' ) {
    customElements.define('pickup-availability-compact', PickupAvailabilityCompact);
	}

}

Add the CSS

Open Theme settings → Custom CSS, paste this and save. Custom CSS is kept when you update Local.

/* Hide purchase buttons when the product is not available at the selected store */
.product-pickup--unavailable .product-item__quick-buy,
.product-pickup--unavailable .product-form {
  display: none !important;
}

/* Hide purchase buttons while no store is selected */
.product-pickup--unselected .product-item__quick-buy,
.product-pickup--unselected .product-form {
  display: none !important;
}

The product page keeps its price; only the buy buttons are hidden.

Can customers still buy the product?

Yes. Hiding the buttons changes only what customers see in the theme. Checkout is controlled by Shopify, not by Local, and customers choose pickup or delivery there. If you must stop orders for products a store does not have, use Shopify's inventory and local pickup settings, or an app.

Can Local hide the buy buttons without code?

No. Local does not support hiding purchase buttons by store availability with theme settings. Without code, the pickup lines inform customers, and checkout handles the rest.

On this page