How to set price rules by a product variant id for a specific quantity (Shopify GraphQl API)

Published Date: December 17, 2020

How to set price rules by a product variant id for a specific quantity (Shopify GraphQl API)

We're now in the second week of the #4WeeksOfShopifyDev challenge.

In the sixth article, As requested, I'll be talking about how to set price rules by a product variant id for a specific quantity using the Shopify GraphQl API

We'll build a discount (80% OFF) that will be applied automatically when the user has a specific product variant with a minimum quantity of 2 itemsLet do it!

I'll be using the Shopify Admin API explorer app

  • You need to install the Shopify Admin API app
  • Paste this code in GraphQl query
mutation discountAutomaticBasicCreate($automaticBasicDiscount: DiscountAutomaticBasicInput!) { discountAutomaticBasicCreate(automaticBasicDiscount: $automaticBasicDiscount) { automaticDiscountNode { id } userErrors { code extraInfo field message } } }

In this code, we have this mutation function to creates a basic automatic discount and it's will return the id of the generated discount

  • Paste this JSON object in the query variables section
{ "automaticBasicDiscount": { "title": "Ilias Challenge", "startsAt": "2020-12-07T11:26:29.519Z", "minimumRequirement": { "quantity": { "greaterThanOrEqualToQuantity": "2" } }, "customerGets": { "value": { "percentage": 0.8 }, "items": { "products": { "productVariantsToAdd": "gid://shopify/ProductVariant/YOUR-PRODUCT-VARIANT-ID" } } } } }

Notes:

Percentage accepts value between 0.00 - 1.00 80% - 0.80

Your Product Variant ID: you can get it when editing a product variant, you'll have in the URL

In this example:

https://ilias-demo.myshopify.com/admin/products/4805542674516/variants/32963135799380

Variant_ID= 32963135799380

You can also checkout this screen cast video of how I create the discount

#4WeeksOfShopifyDevShopify DevShopify APIShopify App Dev

Related Posts

Convert your Shopify store to PWA for better performances (conversion)
#4WeeksOfShopifyDev

Convert your Shopify store to PWA for better performances (conversion)

December 31, 2020

On the 16th of the #4WeeksOfShopifyDev challenge, I’ll be talking about Why you should convert your Shopify store to PWA for better performance?

What benefits of being a Shopify partner and make money using it?
#4WeeksOfShopifyDev

What benefits of being a Shopify partner and make money using it?

December 24, 2020

Why developers should invest in learning the Shopify App or Theme development?
#4WeeksOfShopifyDev

Why developers should invest in learning the Shopify App or Theme development?

December 21, 2020

Let's Build Something Together

Feel free to reach out if you're looking for a developer, have a question, or just want to connect.

Email:contact [at] iliashaddad.com