Get Created Automatic Discounts object on Product & Collection Pages

We have created an Automatic Discount in our store, which works on the Cart page. However, we would like to automatically display prices with the discount on Product & Collection pages.

The following solution works, but only when the cart is not empty and a cart object exists in the liquid code:

{% if cart.discount_applications.size > 0 %}
  {% assign discount_application = cart.discount_applications[0] %}
  {% if discount_application.value_type == 'percentage' %}
    {% assign discount_percent_off = discount_application.value | divided_by: 100 %}
  {% endif %}
{% endif %} 

This is example is for a specific percentage discount with index [0] as only 1 is created.

Then use the following code to calculate and display the discount prices:

{% if discount_percent_off %}
  {% assign product_price = product.price %}
  {% assign product_calc_amount = product_price %}
  {% assign discount_amount = product_calc_amount | times: discount_percent_off %}
  {% assign discount_price = product_calc_amount | minus: discount_amount %}
  <span id="price" class="single-product-price discount-price" data-product-price>
   {{ discount_price | money_with_currency }}
  </span>
   {% if product_price > discount_price %}
      <div class="regular-price-display">
        <span class="visually-hidden" data-compare-text>{{ 'products.product.regular_price' | t }}</span>
        <s id="compare-at-price-update" data-compare-price>{{ product_price | money_with_currency }}</s>
      </div>
   {% endif %}
{% else %}
  {% comment %} display normal pricing code here {% endcomment %}
{% endif %}

Again, this works only when the cart object exists.

Looking for a way to access the Stores created Automatic Discounts (JSON data or object) when the cart is empty.

Thanks :slight_smile:

2 Likes

The following solution works, but only when the cart is not empty

Automatic Discounts are applied automatically when a customer adds them to their cart.

[W]e would like to automatically display prices with the discount on Product & Collection pages.

If the customer doesn’t need to add the product to their cart, what do they need to do to get the discount?

If the customer doesn’t need to add the product to their cart, what do they need to do to get the discount?

are you slow? why the heck are you even answering to this? topic starter made it clear what they need to do and it makes perfect sense to me.

Is there an answer to this? Perhaps in another thread?

I ended up saving automatic discount data as a stringified json on shop.metafields which is available on all pages

Just to clarify, do you mean that you can now create different automatic discounts in your admin section, then by using metafields, you are able to display the discounted price on your product and collection pages?

Not different, just one automatic discount at a time. Yes, I made a custom app that has this feature. It checks if there is active automatic discount and saves it data in shop metafield.