discountCodeBasicCreate Restrict the application of the discount code to a single variant only once

discountCodeBasicCreate Restrict the application of the discount code to a single variant only once

igorsrb997
Shopify Partner
1 0 0

I've created an application that generates discounts for product variants. For each selected date, it generates a unique discount code. The problem arises when I choose a date for delayed delivery, intending to receive a discount code, but then decide to have the product delivered the next day. The system still applies the discount code to this subsequent delivery.

Is it possible to use discountCodeBasicCreate to ensure that the code can only be applied to a single variant once?

This is my mutation

onst graphqlQuery = `
    mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
      discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
        codeDiscountNode {
          codeDiscount {
            ... on DiscountCodeBasic {
              title
              usageLimit
              codes(first: 10) {
                nodes {
                  code
                }
              }
              startsAt
              endsAt
              customerSelection {
                ... on DiscountCustomerAll {
                  allCustomers
                }
              }
              customerGets {
                value {
                  ... on DiscountPercentage {
                    percentage
                  }
                }
                items {
                  ... on DiscountProducts {
                    productVariants(first: 10) {
                      edges {
                        node {
                          id
                        }
                      }
                    }
                  }
                  ... on AllDiscountItems {
                    allItems
                  }
                }
              }
              appliesOncePerCustomer
            }
          }
        }
        userErrors {
          field
          code
          message
        }
      }
    }
  `;

// Function for applying a discount code
const applyDiscountCode = async (req, res) => {
  const { productVariantId, percentage, shopURL, discountLabel, template } =
    req.body;

  try {
    const discountCodeDetails = {
      title: `Ships on ${discountLabel}`,
      code: uniqid(template, discountLabel),
      startsAt: `${discountStart}T00:00:00Z`,
      endsAt: `${discountEnd}T00:00:00Z`,
      appliesOncePerCustomer: true,
      usageLimit: 1,
      customerSelection: {
        all: true,
      },
      combinesWith: {
        orderDiscounts: true,
        productDiscounts: true,
        shippingDiscounts: true,
      },
      customerGets: {
        value: {
          percentage: percentage || 0,
        },
        items: {
          products: {
            productVariantsToAdd: [productVariantId],
          },
        },
      },
    };

    const response = await createDiscountCode(discountCodeDetails, shopURL);

    res.json(response);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};
Replies 0 (0)