Variant metafield help - Trying to display a back in stock message

Hoping someone might have some insight to share…

I’m trying to display a custom back-in-stock timing message on my site when a variant has an inventory count <= 0. I’m using the Boost theme with the Inventory Notice turned on and my variants are set to NOT ‘continue selling when out of stock’. I want to use variant metafields to toggle and display a special message when the variant is out of stock. I would prefer the messaging to be variant-specific, but I can make do with product messaging if trying to do this at the variant level will require extra coding.

I’ve created 2 variant metafields: one (single line text) for the message and one (True/False) metafield to simplify the administration – a quick On/Off toggle. It isn’t working. I thought it might be due to a javascript issue with the variant selection. So, I tried using product metafields, but that’s not working either. (And yes, I am testing this on a product with <= 0 inventory and the data in the metafields.)

Here’s a code snippet (my insert is commented near the end):

{%- if current_variant.inventory_management != blank and block.settings.show_inventory_notice != ‘never’ -%}

{% if inventory_qty > 0 %} {%- unless block.settings.show_inventory_notice == 'low' and inventory_qty > inventory_threshold -%} {{ inventory_status }} {% unless block.settings.show_inventory_count == 'never' %} {% unless block.settings.show_inventory_count == 'low' and inventory_qty > inventory_threshold %} {% if inventory_qty == 1 %} - {{ 'products.labels.single_unit_available' | t }} {% else %} - {{ 'products.labels.x_units_available' | t: quantity: inventory_qty }} {% endif %} {% endunless %} {% endunless %} {% endunless %} {%- elsif inventory_qty <= 0 %} {%- if variant.metafields.custom.back_order_on == True -%} {{ variant.metafields.custom.back_order_message }} {% endif %} {% endif %}

Any ideas?

Hi @melis-morgan

as the code you post you only have current_variant, variant is empty. I’m not sure if you have define variant or those code all in loop.

current_variant.metafields.custom.back_order_on

It would be best if you DM me your store url and accept me as collaborator.

Hi @Kani , thanks for looking at this. I’ve tried using current_variant.metafields… as well, but I’m not sure: is that even possible? Online information about metafields is sorely lacking…

This is all within the Buy Buttons loop where the form has assigned inventory_qty to current_variant.

Here’s the full code from the form, now with current_variant.metafields (which still doesn’t work):

{%- when ‘buy_buttons’ -%}
{%- form ‘product’, product, id: product_form_id, class: form_class -%}
{%- liquid
assign inventory_class = ‘’
assign inventory_threshold = block.settings.inventory_notice_threshold
assign inventory_qty = current_variant.inventory_quantity

if inventory_qty > 0
if inventory_qty <= inventory_threshold
assign inventory_class = ’ product-inventory–low’
assign inventory_status = ‘products.labels.low_stock’ | t
else
if block.settings.show_inventory_notice == ‘low’
assign inventory_class = ’ product-inventory–none’
else
assign inventory_status = ‘products.labels.in_stock’ | t
endif
endif
else
assign inventory_class = ’ product-inventory–none’
endif
-%}

{%- if current_variant.inventory_management != blank and block.settings.show_inventory_notice != ‘never’ -%}

{% if inventory_qty > 0 %} {%- unless block.settings.show_inventory_notice == 'low' and inventory_qty > inventory_threshold -%} {{ inventory_status }} {% unless block.settings.show_inventory_count == 'never' %} {% unless block.settings.show_inventory_count == 'low' and inventory_qty > inventory_threshold %} {% if inventory_qty == 1 %} - {{ 'products.labels.single_unit_available' | t }} {% else %} - {{ 'products.labels.x_units_available' | t: quantity: inventory_qty }} {% endif %} {% endunless %} {% endunless %} {% endunless %} {%- elsif inventory_qty <= 0 %} {%- if current_variant.metafields.custom.back_order_on == True -%} {{ current_variant.metafields.custom.back_order_message }} {% endif %}

{% endif %}

{%- endif -%} {% if product.available and block.settings.show_backorder_text == true %} {% render 'backorder', product: product, variant: current_variant %} {% endif %}

{%- if block.settings.show_quantity_selector -%}
{% render ‘quantity-selector’, variant: current_variant %}
{%- endif -%}

{%- if current_variant.available -%} {{ 'products.product.add_to_cart' | t }} {%- else -%} {{ 'products.product.sold_out' | t }} {%- endif -%}

{% if block.settings.enable_payment_button and product.selling_plan_groups == empty %}
{{ form | payment_button }}
{% endif %}

{%- if block.settings.show_pickup_availability -%}
{% render ‘store-availability’, product: product, current_variant: current_variant %}
{%- endif -%}

{%- endform -%}