subscriptions.original_to_hidden_variant_map metafield not showing on all products

prathmesh_veda
Visitor
1 0 0

I am trying to show the discounted price of products on my subscription page using metafields. But the subscriptions.original_to_hidden_variant_map is not available on all products. How can I get that metafields on all products.

My liquid code looks like:-

 

{% for product in collections.all.products %}
    {% for variant in product.variants %}

        {{ variant.metafields.subscriptions.discount_variant_price }}

    {% endfor %}

{% endfor %}

 

Reply 1 (1)

Jonathan_Cui
Shopify Staff (Retired)
1 0 0

Hi Prathmesh! 

Thank you for writing to us. Shopify does not officially use metafields to store/display information about subscriptions and subscriptions discounts. I would recommend you take a look at this tutorial regarding displaying Pricing/Payments for Subscriptions. The selling plan allocation object is the officially supported method of querying subscriptions discounts for products. 

 

In the case that you would like to continue with using metafields (which is not officially supported by Shopify), my best guess is that the information in each variant.metafield is populated individually. It would make sense that only subscriptions products will have the variant.metafield.subscriptions field populated because your subscriptions app injects that information. If you are continuing with metafields, a possible solution is to use an if statement to check if the variant has subscriptions information before displaying the discounted subscription price:

{% for product in collections.all.products %}
    {% for variant in product.variants %}
        {% if variant.metafields.subscriptions.subscription_id %}
            {{ variant.metafields.subscriptions.discount_variant_price }}
            {% assign variant_map = variant.metafields.subscriptions.original_to_hidden_variant_map | default: false %}
            ... use variant_map
        {% endif %}
    {% endfor %}
{% endfor %}

 

I will recommend that you check out the tutorial regarding displaying Pricing/Payments for Subscriptions. 

 

Does this unblock you?

To learn more visit the Shopify Help Center or the Community Blog.