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 items
Let 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

Subscribe below to get notified of new posts.

Related Posts

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

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

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

On the 15th of the #4WeeksOfShopifyDev challenge, I’ll be talking about what benefits of being a Shopify partner?

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

On the 14th of the #4WeeksOfShopifyDev challenge, I’ll be talking about why developers should invest in learning and becoming a Shopify developer.

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@iliashaddad.com

© Ilias Haddad 2021. All rights reserved.

Made with ❤️ in Morocco 🇲🇦