0
votes

In my customized Shopify theme, a "Final Sale" message displays for any product that is 70% off using the following code:

{% assign finalSale = product.metafields.details.final_sale | upcase %}
{% if finalSale == 'TRUE' %}
    <p style="color: #B21F1F;">
        This item is final sale -- no returns or exchanges are accepted.
    </p>
{% endif %}

I am trying to change this so it displays the Final Sale message when the product is 60% off. My theme is using the Metafields 2 app and I do see the active metafield called "final_sale" under the "Configure Product Metafields" page but I don't see where I can adjust the percentage that is assigned to this custom metafield...Does anyone know where I can find this?

1
Are you using the "Compare at price" field to store the original price? help.shopify.com/manual/products/promoting-marketing/…Josh Brown
@JoshBrown I am using the Compare at Price to store the original price. Do you think that's playing into this?bhood

1 Answers

1
votes

The 60% figure isn't anywhere in this code, and isn't part of the theme at all. The percentage comparison is being done when the metafield gets assigned to TRUE. It's only possible to read metafields in liquid, though—you can't write to them there. You'd definitely need to look for code that sets the metafield elsewhere; perhaps as part of a custom app.

I don't think you need the metafield for this case at all, though. You could set up the logic right in liquid using product.price attributes and the math filters.

It would look something like this in liquid:

if compare_at_price | minus price | divided_by compare_at_price >= 0.6

One thing to pay attention to with this approach is Shopify's math filters rounding to the nearest integer at times, but you can fix that by multiplying by 100 before you start.

assign sale_amount = compare_at_price | minus price | times 100
assign adjusted_compare_price = compare_at_price | times 100
if sale_amount | divided_by adjusted_compare_price >= 60

The actual implementation might need to be a little more complex than this depending on how you use variants and variant pricing, plus this example can't handle the case where "price" is the original and "compare at" is the sale amount. Those can also be handled with liquid's logic. Here are the relevant liquid references to help you build this out further if needed:

https://help.shopify.com/themes/liquid/objects/product

https://help.shopify.com/themes/liquid/filters/math-filters