Best way to get sale per product

Best way to get sale per product

iO-Rotterdam
Shopify Partner
9 0 5

Hello everyone!

 

We use Shopify as a CMS for our webshop, thus we use the Shopify API to get the product data. We want to start doing sales on the website and for that we need to inform the customer if there is a sale on a product, with a "20% off" label and showing the old and new price.

 

From my research, it doesn't seem to be possible to get any data from the Discount section in the Storefront API. We thought about using the Admin API for this, but given the cost in points of getting it with the Admin API, this doesn't seem viable for us. What would be the best way of doing this?

Replies 5 (5)

TotoCla
New Member
6 0 0

Hello,

 

I think you don't need API. You can set the price and the old price in your back office. Then, you have to custom your code to display your discount tag.

ShopifyDevSup
Shopify Staff
1453 238 511

Hey @iO-Rotterdam - you are correct that at the moment the easiest way to get this info would be through the admin API. A GraphQL API call that might save you some bandwidth could look something like this:

{
   product(id:"gid://shopify/Product/[productIDhere]") {
   id
   variants (first:10) {
     edges {
       node {
         id
         price
         compareAtPrice
       }
     }
   }
 }
}

 

The call itself is pretty compact and small - especially if you know exactly which product you need to serve the data from. By editing the price in your admin (or via the Product API) as @TotoCla mentioned to your discounted price and changing the "compare at" price to the original price, this is what triggers the "discount" formatting on Shopify liquid-based themes/frontends. You could use the "compare at" price to determine if a product is discounted and then take the value of that field as it's outputted in the API response to feed that into your webshop's frontend as well. 

Hope this helps - let us know if we can clarify anything on our end! 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

iO-Rotterdam
Shopify Partner
9 0 5

Thank you for your response!

 

Does this mean it is not possible to get the automatic discount per product, but we have to set all product prices individually to their "compare at" price in order to make a sale rather than having a way to add a sale to all product? If so, what is the use of the "Discount" section when using it as a CMS?

ShopifyDevSup
Shopify Staff
1453 238 511

No worries! Another quick way to get the value of a specific discount code would be to query it using one of our DiscountNode queries, depending on the type of discount code that is being applied to the product. There's a bit of documentation here that goes into this. When querying the discount code, you will want to look for the "CustomerGets" field, which would contain the value (percentage/flat) of the discount.

 

I also have another option that could work and may be faster when it comes to API call bandwidth. There is a "value" field on the PriceRule resource that represents the value of the discount. One workflow that may be useful would be using the value field for a specific PriceRule to trigger the discount notification on your site's frontend. For example, let's say the "value" field's value is "20", for 20 percent. You could serve that data to your frontend from the PriceRule resource to trigger the discount notification on your store. One thing to note is that price rules are the "top tier" of the Shopify discount logic that contain the actual information that is then associated with a store's discount codes (the value of the discount, the products/collections it applies to, etc.)

 

Using the PriceRule resource might be a little easier to simply retrieve the value of the discount so you can serve that data to your shop's frontend to trigger the discount notification, so I wanted to share this as an alternative too. If you have any questions or need any assistance, don't hesitate to post back here and we can help out. Hope this helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

iO-Rotterdam
Shopify Partner
9 0 5

Alright thank you, I think we have enough information for now to come with a solution.

 

I must ask though, why is this not a part of the Storefront API? Discounts are a big part of webshops, so this seems like some vital information to be able to display. The Admin API doesn't seem like the best way of getting this information to me