Anchor to section titled 'undefined'

priceRuleCreate
mutation
deprecated

Requires write_price_rules access scope.

Create a price rule using the input. Use discountCodeBasicCreate instead.


The input fields to create a price rule.

The input fields to create a discount code for the price rule.


Was this section helpful?

The newly created price rule.

The newly created discount code.

The list of errors that occurred from executing the mutation.

The list of errors that occurred from executing the mutation. Use priceRuleUserErrors instead.


Was this section helpful?

Examples

Hide code
DescriptionCopy
mutation priceRuleCreate($priceRule: PriceRuleInput!, $priceRuleDiscountCode: PriceRuleDiscountCodeInput) {
  priceRuleCreate(priceRule: $priceRule, priceRuleDiscountCode: $priceRuleDiscountCode) {
    priceRule {
      id
      title
      validityPeriod {
        start
      }
      allocationMethod
      itemEntitlements {
        products(first: 3) {
          edges {
            node {
              id
            }
          }
        }
        targetAllLineItems
      }
      valueV2 {
        ... on MoneyV2 {
          amount
          currencyCode
        }
      }
      customerSelection {
        forAllCustomers
      }
      oncePerCustomer
      usageLimit
      target
    }
    priceRuleDiscountCode {
      id
      code
    }
    priceRuleUserErrors {
      code
      field
      message
    }
  }
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/2024-01/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation priceRuleCreate($priceRule: PriceRuleInput!, $priceRuleDiscountCode: PriceRuleDiscountCodeInput) { priceRuleCreate(priceRule: $priceRule, priceRuleDiscountCode: $priceRuleDiscountCode) { priceRule { id title validityPeriod { start } allocationMethod itemEntitlements { products(first: 3) { edges { node { id } } } targetAllLineItems } valueV2 { ... on MoneyV2 { amount currencyCode } } customerSelection { forAllCustomers } oncePerCustomer usageLimit target } priceRuleDiscountCode { id code } priceRuleUserErrors { code field message } } }",
 "variables": {
    "priceRule": {
      "title": "Boots Discount",
      "allocationMethod": "ACROSS",
      "customerSelection": {
        "forAllCustomers": true
      },
      "itemEntitlements": {
        "targetAllLineItems": false,
        "productIds": [
          "gid://shopify/Product/121709582"
        ]
      },
      "target": "LINE_ITEM",
      "validityPeriod": {
        "start": "2016-08-29T12:00:00-04:00"
      },
      "value": {
        "fixedAmountValue": "-10.0"
      }
    }
  }
}'
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(
  `#graphql
  mutation priceRuleCreate($priceRule: PriceRuleInput!, $priceRuleDiscountCode: PriceRuleDiscountCodeInput) {
    priceRuleCreate(priceRule: $priceRule, priceRuleDiscountCode: $priceRuleDiscountCode) {
      priceRule {
        id
        title
        validityPeriod {
          start
        }
        allocationMethod
        itemEntitlements {
          products(first: 3) {
            edges {
              node {
                id
              }
            }
          }
          targetAllLineItems
        }
        valueV2 {
          ... on MoneyV2 {
            amount
            currencyCode
          }
        }
        customerSelection {
          forAllCustomers
        }
        oncePerCustomer
        usageLimit
        target
      }
      priceRuleDiscountCode {
        id
        code
      }
      priceRuleUserErrors {
        code
        field
        message
      }
    }
  }`,
  {
    variables: {
      "priceRule": {
        "title": "Boots Discount",
        "allocationMethod": "ACROSS",
        "customerSelection": {
          "forAllCustomers": true
        },
        "itemEntitlements": {
          "targetAllLineItems": false,
          "productIds": [
            "gid://shopify/Product/121709582"
          ]
        },
        "target": "LINE_ITEM",
        "validityPeriod": {
          "start": "2016-08-29T12:00:00-04:00"
        },
        "value": {
          "fixedAmountValue": "-10.0"
        }
      }
    },
  },
);

const data = await response.json();
session = ShopifyAPI::Auth::Session.new(
  shop: "your-development-store.myshopify.com",
  access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
  session: session
)

query = <<~QUERY
  mutation priceRuleCreate($priceRule: PriceRuleInput!, $priceRuleDiscountCode: PriceRuleDiscountCodeInput) {
    priceRuleCreate(priceRule: $priceRule, priceRuleDiscountCode: $priceRuleDiscountCode) {
      priceRule {
        id
        title
        validityPeriod {
          start
        }
        allocationMethod
        itemEntitlements {
          products(first: 3) {
            edges {
              node {
                id
              }
            }
          }
          targetAllLineItems
        }
        valueV2 {
          ... on MoneyV2 {
            amount
            currencyCode
          }
        }
        customerSelection {
          forAllCustomers
        }
        oncePerCustomer
        usageLimit
        target
      }
      priceRuleDiscountCode {
        id
        code
      }
      priceRuleUserErrors {
        code
        field
        message
      }
    }
  }
QUERY

variables = {
  "priceRule": {
    "title": "Boots Discount",
    "allocationMethod": "ACROSS",
    "customerSelection": {
      "forAllCustomers": true
    },
    "itemEntitlements": {
      "targetAllLineItems": false,
      "productIds": ["gid://shopify/Product/121709582"]
    },
    "target": "LINE_ITEM",
    "validityPeriod": {
      "start": "2016-08-29T12:00:00-04:00"
    },
    "value": {
      "fixedAmountValue": "-10.0"
    }
  }
}

response = client.query(query: query, variables: variables)
const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: {
    "query": `mutation priceRuleCreate($priceRule: PriceRuleInput!, $priceRuleDiscountCode: PriceRuleDiscountCodeInput) {
      priceRuleCreate(priceRule: $priceRule, priceRuleDiscountCode: $priceRuleDiscountCode) {
        priceRule {
          id
          title
          validityPeriod {
            start
          }
          allocationMethod
          itemEntitlements {
            products(first: 3) {
              edges {
                node {
                  id
                }
              }
            }
            targetAllLineItems
          }
          valueV2 {
            ... on MoneyV2 {
              amount
              currencyCode
            }
          }
          customerSelection {
            forAllCustomers
          }
          oncePerCustomer
          usageLimit
          target
        }
        priceRuleDiscountCode {
          id
          code
        }
        priceRuleUserErrors {
          code
          field
          message
        }
      }
    }`,
    "variables": {
      "priceRule": {
        "title": "Boots Discount",
        "allocationMethod": "ACROSS",
        "customerSelection": {
          "forAllCustomers": true
        },
        "itemEntitlements": {
          "targetAllLineItems": false,
          "productIds": [
            "gid://shopify/Product/121709582"
          ]
        },
        "target": "LINE_ITEM",
        "validityPeriod": {
          "start": "2016-08-29T12:00:00-04:00"
        },
        "value": {
          "fixedAmountValue": "-10.0"
        }
      }
    },
  },
});
use Shopify\Clients\Graphql;

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
$query = <<<QUERY
  mutation priceRuleCreate($priceRule: PriceRuleInput!, $priceRuleDiscountCode: PriceRuleDiscountCodeInput) {
    priceRuleCreate(priceRule: $priceRule, priceRuleDiscountCode: $priceRuleDiscountCode) {
      priceRule {
        id
        title
        validityPeriod {
          start
        }
        allocationMethod
        itemEntitlements {
          products(first: 3) {
            edges {
              node {
                id
              }
            }
          }
          targetAllLineItems
        }
        valueV2 {
          ... on MoneyV2 {
            amount
            currencyCode
          }
        }
        customerSelection {
          forAllCustomers
        }
        oncePerCustomer
        usageLimit
        target
      }
      priceRuleDiscountCode {
        id
        code
      }
      priceRuleUserErrors {
        code
        field
        message
      }
    }
  }
QUERY;

$variables = [
  "priceRule" => [
    "title" => "Boots Discount",
    "allocationMethod" => "ACROSS",
    "customerSelection" => [
      "forAllCustomers" => true,
    ],
    "itemEntitlements" => [
      "targetAllLineItems" => false,
      "productIds" => ["gid://shopify/Product/121709582"],
    ],
    "target" => "LINE_ITEM",
    "validityPeriod" => [
      "start" => "2016-08-29T12:00:00-04:00",
    ],
    "value" => [
      "fixedAmountValue" => "-10.0",
    ],
  ],
];

$response = $client->query(["query" => $query, "variables" => $variables]);
Hide code
Input variables
Copy
{
  "priceRule": {
    "title": "Boots Discount",
    "allocationMethod": "ACROSS",
    "customerSelection": {
      "forAllCustomers": true
    },
    "itemEntitlements": {
      "targetAllLineItems": false,
      "productIds": [
        "gid://shopify/Product/121709582"
      ]
    },
    "target": "LINE_ITEM",
    "validityPeriod": {
      "start": "2016-08-29T12:00:00-04:00"
    },
    "value": {
      "fixedAmountValue": "-10.0"
    }
  }
}
Hide code
Response
JSON
{
  "priceRuleCreate": {
    "priceRule": {
      "id": "gid://shopify/PriceRule/1057371212",
      "title": "Boots Discount",
      "validityPeriod": {
        "start": "2016-08-29T16:00:00Z"
      },
      "allocationMethod": "ACROSS",
      "itemEntitlements": {
        "products": {
          "edges": [
            {
              "node": {
                "id": "gid://shopify/Product/121709582"
              }
            }
          ]
        },
        "targetAllLineItems": false
      },
      "valueV2": {
        "amount": "-10.0",
        "currencyCode": "USD"
      },
      "customerSelection": {
        "forAllCustomers": true
      },
      "oncePerCustomer": false,
      "usageLimit": null,
      "target": "LINE_ITEM"
    },
    "priceRuleDiscountCode": null,
    "priceRuleUserErrors": []
  }
}