For the complete documentation index, see llms.txt. This page is also available as Markdown.

Adding Metafields to Disable Add to Cart for OOS Products

Want to keep inventory available for products in their subscription without having to remove them from all sales channels? Well, control the add to cart button's availability using custom metafields.

Objective

This guide outlines how to replicate "Out of Stock" behavior on product display pages (PDPs) while still allowing subscription orders to be processed.

Background

Currently, for subscriptions to process orders that include products with negative inventory, the "Continue selling when out of stock" option must be enabled. However, enabling this setting also allows new, one-time purchases of these out-of-stock items, which may result in overselling.

Purpose

To address this, the following guide provides a method to simulate an "Out of Stock" state for one-time purchases—restricting new customers from buying unavailable items—while still permitting existing, recurring subscription orders to process as usual.

How To Implement

  1. Create the Metafield

  2. Go to Settings > Custom data > Products in Shopify admin.

Screenshot of the "Product metafield definitions" in Shopify. Click the "Add definition" button next to "View unstructured metafields" to add a custom metafield.
  1. Add a new definition

    • Name: Force Out of Stock (feel free to adjust as needed)

    • Key: force_out_of_stock (this will autopopulate from the name)

    • Type: Boolean (True/False)

When adding a metafield, you will need to give it a name and select its type. In this screenshot, the "Boolean" type is selected. So, this metafield will only accept 1 of 2 values: true or false.
  1. Set the Metafield for Specific Products

    • Navigate to a product that you want to “set out of stock” visually on the frontend.

    • Scroll to the metafields section at the bottom.

    • Set Force Out of Stock to true.

Go to the product that you're setting this metafield for, scroll down to the metafields section on the PDP, locate the newly added metafield and set the value accordingly.
  1. Modify Your Theme Code

    • Go to Online Store > Themes > Edit Code

    • Find your product.liquid, main-product.liquid, or product.json template, depending on your theme.

    • Locate your purchase button (i.e. add to cart button or buy button).

    • Modify the display of the purchase button based on the Force Out of Stock metafield.

      • When this metafield is enabled, a disabled version of the "Buy" button should be shown to simulate an out-of-stock state.

      • If the metafield is not enabled, the standard "Buy" button should be displayed as usual.

Example Templates

Example 1

Example 2 (in product-form.liquid)

Last updated