Need to create a discount code to a specific cutomer using graphql

Solved

Need to create a discount code to a specific cutomer using graphql

Rajith
Visitor
3 0 1
  $query = <<<'QUERY'
            mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
              discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
                codeDiscountNode {
                  codeDiscount {
                    ... on DiscountCodeBasic {
                      title
                      codes(first: 10) {
                        nodes {
                          code
                        }
                      }
                      startsAt
                      endsAt
                      customerSelection {
                        ... on DiscountCustomers {
                          customers
                      }
                      customerGets {
                        value {
                          ... on DiscountPercentage {
                            percentage
                          }
                        }
                        items {
                          ... on AllDiscountItems {
                            allItems
                          }
                        }
                      }
                      appliesOncePerCustomer
                    }
                  }
                }
                userErrors {
                  field
                  code
                  message
                }
              }
            }
          QUERY;

        $variables = [
            "basicCodeDiscount" => [
                "title" => "20% Rajith Sahampathh",
                "code" => "SUMMER20",
                "startsAt" => "2022-06-21T00:00:00Z",
                "endsAt" => "2022-09-21T00:00:00Z",
                "customerSelection" => [
                    "customers" => [
                        81044372851254
                    ],
                ],
                "combinesWith" => [
                    "orderDiscounts" => true
                ],
                "customerGets" => [
                    "value" => [
                        "percentage" => 0.2,
                    ],
                    "items" => [
                        "all" => true,
                    ],
                ],
                "appliesOncePerCustomer" => true,
            ],
        ];
 
What is the issue of this. When add for all customers works fine.
Accepted Solution (1)

Liam
Community Manager
3108 340 873

This is an accepted solution.

Hi Rajith,

 

I was able to get something very similar to work with this mutation - you should be able to adjust it for your purpose:

 

mutation CreateDiscountCode {
  discountCodeBasicCreate(
    basicCodeDiscount: {
      title: "20% off all products",
      code: "DISCOUNT20",
      startsAt: "2023-10-01T00:00:00Z",
      endsAt: "2023-12-31T23:59:59Z",
      customerSelection: {
        customers: {
          add: ["gid://shopify/Customer/12345678"]
        }
      },
      customerGets: {
        value: {
          percentage: 0.2
        },
        items: {
          all: true
        }
      },
      appliesOncePerCustomer: true
    }
  ) {
    codeDiscountNode {
      codeDiscount {
        ... on DiscountCodeBasic {
          title
          codes(first: 10) {
            nodes {
              code
            }
          }
          startsAt
          endsAt
          customerSelection {
            ... on DiscountCustomers {
              customers {
                id
              }
            }
          }
          customerGets {
            value {
              ... on DiscountPercentage {
                percentage
              }
            }
            items {
              ... on AllDiscountItems {
                allItems
              }
            }
          }
          appliesOncePerCustomer
        }
      }
    }
    userErrors {
      field
      code
      message
    }
  }
}

 

You just need to replace `gid://shopify/Customer/12345678` with `gid://shopify/Customer/81044372851254` and it should work. 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me 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

View solution in original post

Replies 2 (2)

Liam
Community Manager
3108 340 873

This is an accepted solution.

Hi Rajith,

 

I was able to get something very similar to work with this mutation - you should be able to adjust it for your purpose:

 

mutation CreateDiscountCode {
  discountCodeBasicCreate(
    basicCodeDiscount: {
      title: "20% off all products",
      code: "DISCOUNT20",
      startsAt: "2023-10-01T00:00:00Z",
      endsAt: "2023-12-31T23:59:59Z",
      customerSelection: {
        customers: {
          add: ["gid://shopify/Customer/12345678"]
        }
      },
      customerGets: {
        value: {
          percentage: 0.2
        },
        items: {
          all: true
        }
      },
      appliesOncePerCustomer: true
    }
  ) {
    codeDiscountNode {
      codeDiscount {
        ... on DiscountCodeBasic {
          title
          codes(first: 10) {
            nodes {
              code
            }
          }
          startsAt
          endsAt
          customerSelection {
            ... on DiscountCustomers {
              customers {
                id
              }
            }
          }
          customerGets {
            value {
              ... on DiscountPercentage {
                percentage
              }
            }
            items {
              ... on AllDiscountItems {
                allItems
              }
            }
          }
          appliesOncePerCustomer
        }
      }
    }
    userErrors {
      field
      code
      message
    }
  }
}

 

You just need to replace `gid://shopify/Customer/12345678` with `gid://shopify/Customer/81044372851254` and it should work. 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me 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

Rajith
Visitor
3 0 1

Thanks a lot brother. It worked for me.