Foxden Creative

Bypassing Shopify's Variant Inventory Limit: A Custom Code Solution

Shopify is a powerful platform for e-commerce, but sometimes its built-in features don’t perfectly align with unique business needs. A common challenge arises when selling products with variants, like apparel with different sizes, but you need to manage inventory based on a total stock count across all variants, rather than individually per size. This can be particularly tricky for limited-run collections where you have a fixed number of items regardless of the size breakdown.

By default, Shopify’s inventory management is tied to each specific variant. If you have a t-shirt available in Small, Medium, and Large, you’re required to enter the stock level for Small, Medium, and Large independently. This works well for traditional inventory where you might have 10 Small, 15 Medium, and 5 Large t-shirts. However, if you have a special collection limited to just 10 t-shirts total across all sizes, Shopify’s default setup forces you to artificially allocate that stock per size, which doesn’t reflect the true total limit and can lead to overselling if one size is unexpectedly popular.

This was the exact hurdle faced recently when setting up a special collection. The goal was to offer 10 unique apparel items, available in various sizes, but with a strict total limit of 10 orders. Simply creating size variants and setting inventory to 10 for each size wouldn’t work; it would allow for potentially 10 Small, 10 Medium, and 10 Large orders, far exceeding the desired total.

The solution? A custom code workaround leveraging Shopify’s ability to carry custom data through the “add to cart” process. While not immediately obvious, any input field within the <form> tags of the “add to cart” form that includes a name attribute will have its data passed along as a “line item property” when the product is added to the cart. This data then persists through to the checkout and the final order details.

This crucial, yet somewhat understated, Shopify feature allows us to bypass the standard variant inventory tracking for size selection and instead handle it through a custom input.

Here’s how this can be implemented:

The Workaround: Custom Size Selection and Line Item Properties

Instead of relying on Shopify’s built-in variant dropdown for size selection (which is tied to the restrictive inventory tracking), we can create our own custom size selector using HTML. When a user selects a size from this custom dropdown and adds the item to the cart, the chosen size is passed as a line item property associated with the product.

The actual inventory management of the total 10 items would then need to happen outside of Shopify’s automatic system. This could involve manually monitoring orders as they come in or using a third-party inventory management tool that can aggregate stock across “virtual” variants based on these line item properties. While manual monitoring is feasible for a very limited run, for ongoing collections, a more robust external system would be necessary. The key here is that the size information is successfully captured and available for this external tracking.

Implementation Steps: Adding the Custom Size Selector

This solution requires modifying your Shopify theme’s Liquid files. You’ll need to apply this to both the product grid item (if you want to allow adding to cart directly from a collection or homepage) and the product detail page.

1. Modifying the Product Grid Item (for homepage/collection display):

Locate the Liquid file responsible for rendering product grid items (often something like product-card.liquid or within a section like featured-collection.liquid).

Inside the <form> tag for adding the product to the cart, you’ll typically find code related to variants. You’ll need to replace or hide the default variant selection for the specific products in your special collection and add your custom size dropdown.

Here’s a simplified example of what you might add:

Code snippet

{% if product.collections contains collection_with_special_inventory %}
  <div class="custom-size-selector">
    <label for="size-{{ product.id }}">Select Size:</label>
    <select name="properties[Selected Size]" id="size-{{ product.id }}">
      <option value="Small">Small</option>
      <option value="Medium">Medium</option>
      <option value="Large">Large</option>
      </select>
  </div>
{% endif %}

<input type="hidden" name="id" value="{{ product.selected_or_first_available_variant.id }}">
<button type="submit" name="add">Add to cart</button>

Replace collection_with_special_inventory with the handle of your special collection.

  • The crucial part is the <select> element with the name="properties[Selected Size]". The properties part tells Shopify this is a line item property, and [Selected Size] is the name that will appear in the cart and order details.
  • You still need a hidden input with name="id" and the variant ID. This is necessary for Shopify to know which product is being added, even though we’re handling the size separately for inventory tracking purposes. You can use the selected_or_first_available_variant.id as a placeholder; the actual size information is in the line item property.

2. Modifying the Product Detail Page:

Locate the main product template file (often main-product.liquid or product-template.liquid). Similar to the product grid, you’ll need to find the “add to cart” form and replace or hide the default variant selection for your special collection products with your custom dropdown.

The code will be very similar to the grid item example:

Code snippet

{% if product.collections contains collection_with_special_inventory %}
  <div class="custom-size-selector">
    <label for="size-{{ product.id }}">Select Size:</label>
    <select name="properties[Selected Size]" id="size-{{ product.id }}">
      <option value="Small">Small</option>
      <option value="Medium">Medium</option>
      <option value="Large">Large</option>
      </select>
  </div>
{% else %}
  {# Display default variant selection for other products #}
{% endif %}

<input type="hidden" name="id" value="{{ product.selected_or_first_available_variant.id }}">
<button type="submit" name="add">Add to cart</button>
  • Again, replace collection_with_special_inventory with your collection handle.
  • Include the conditional logic ({% if product.collections contains ... %}) to ensure this custom selector only appears for the relevant products.

3. Displaying the Selected Size in Cart:

To show the selected size in the cart, you need to modify your cart template file (often main-cart-items.liquid or cart-template.liquid). Inside the loop that iterates through cart items, you can access the line item properties.

Code snippet

{% for item in cart.items %}
  {{ item.product.title }}
  {% comment %} Display line item properties {% endcomment %}
  {% for property in item.properties %}
    {% unless property.last == blank %}
      {{ property.first }}: {{ property.last }}<br>
    {% endunless %}
  {% endfor %}
  Quantity: {{ item.quantity }}
  Price: {{ item.line_price | money }}
{% endfor %}

This code snippet iterates through the properties of each item in the cart and displays them. In this case, it would show “Selected Size: Small” (or whichever size was chosen).

Benefits and Considerations

Benefits:

  • Accurate Total Stock Management: This workaround allows you to sell a limited total quantity of a product across all variants, aligning with the true inventory constraint.
  • Improved Customer Experience for Limited Runs: Customers see the available sizes but the underlying system enforces the overall limit, preventing the frustration of ordering a size that appears in stock but is part of an already depleted total.
  • Developer Credibility: Successfully implementing such a solution demonstrates a strong understanding of Shopify’s Liquid templating and its underlying data handling, showcasing problem-solving skills to potential clients.

Considerations:

  • Manual or External Inventory Tracking: The primary drawback is the need to manage the total inventory count outside of Shopify’s automatic system. This requires careful monitoring or integration with an external inventory management solution.
  • No Automatic “Sold Out” per Size: Since Shopify’s variant inventory isn’t being used for sizes in this case, individual sizes won’t automatically show as “sold out” based on their own stock level. The product would only show as sold out when the total quantity across all orders reaches your limit, which you’d need to manage externally.
  • Theme Updates: Modifications to theme files can be overwritten during theme updates. It’s crucial to keep track of your customizations and reapply them after updating your theme or consider using a development theme for such changes.
  • Complexity: This solution involves custom coding and is best suited for those comfortable working with Shopify’s Liquid templates.

Conclusion

While Shopify’s default inventory management is excellent for many use cases, it can present challenges when you need to manage a total stock limit across product variants. By understanding how Shopify handles line item properties, you can implement custom solutions like a tailored size selector that passes the necessary information through the order process. This not only solves a specific inventory problem but also highlights your ability as a developer to create flexible and effective e-commerce experiences on the Shopify platform.

If you’re facing similar inventory challenges on Shopify and need a custom approach, this technique of using line item properties for crucial data like size can be a valuable tool in your development arsenal.

Lastly, if you need professional help with your shop, get in contact. Let’s talk how Foxden Creative can support your business