Thư viện Queries
Thư viện QueriesTạo hàng nghìn mã giảm giá cho AppSumo trong LemonSqueezy

Tạo hàng nghìn mã giảm giá cho AppSumo trong LemonSqueezy

Query này kết nối với API LemonSqueezy và tạo một trăm mã giảm giá 100% chỉ trong một lần thực thi.

Thực thi query này nhiều lần để tạo 10.000 mã cần thiết để chạy một chiến dịch AppSumo.

Bạn cần cung cấp:

  • Token truy cập để kết nối với API Lemon Squeezy, thông qua biến $lemonSqueezyAccessToken
  • Cửa hàng Lemon Squeezy, thông qua biến $storeID
  • Sản phẩm được đổi bằng mã đó, thông qua biến $variantIDs

Mã giảm giá được tạo ra sẽ là một chuỗi ngẫu nhiên. Bạn có thể thêm tiền tố vào mã (thông qua biến $codePrefix), chỉ định độ dài của mã (thông qua biến $codeLength), và tùy chỉnh tên mã giảm giá (thông qua biến $discountNamePrefix$firstRecordNumber) để tìm kiếm trong bảng điều khiển Lemon Squeezy.

Thu thập tất cả các mã giảm giá vừa tạo bằng cách cung cấp $postId, sau đó tất cả các mã sẽ được thêm vào cuối bài đăng đó.

query ExportLemonSqueezyAPIData(
  $lemonSqueezyAccessToken: String!,
  $variantIDs: [String!]!,
  $postId: ID,
) {
  bearerToken: _sprintf(
    string: "Bearer %s",
    values: [$lemonSqueezyAccessToken]
  )
    @remove
 
  lemonSqueezyAPIHeaders: _echo(value: [
    {
      name: "Accept",
      value: "application/vnd.api+json"
    },
    {
      name: "Content-Type",
      value: "application/vnd.api+json"
    },
    {
      name: "Authorization",
      value: $__bearerToken
    }
  ])
    @export(as: "lemonSqueezyAPIHeaders")
    @remove
 
  variants: _echo(value: $variantIDs)
    @underEachArrayItem(
      passValueOnwardsAs: "variantID"
    )
      @applyField(
        name: "_echo",
        arguments: {
          value: {
            type: "variants",
            id: $variantID,
          }
        }
        setResultInResponse: true
      )
    @export(as: "variants")
 
  hasPostId: _notEmpty(value: $postId)
    @export(as: "hasPostId")
}
 
query CreateMutationInputs(
  $storeID: String!,
  $discountNamePrefix: String! = "AppSumo campaign",
  $codePrefix: String! = "",
  $numberCodes: Int! = 100,
  $codeLength: Int! = 16,
  $firstRecordNumber: Int! = 1,
  $isTestMode: Boolean! = false,
)
  @depends(on: "ExportLemonSqueezyAPIData")
{
  mutationInputs: _arrayPad(array: [], length: $numberCodes, value: null)
    @underEachArrayItem(
      passIndexOnwardsAs: "key"
      affectDirectivesUnderPos: [1, 2, 3, 4, 5]
    )
      @applyField(
        name: "_generateRandomString",
        arguments: {
          length: $codeLength,
          characters: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
        },
        passOnwardsAs: "randomCode"
      )
      @applyField(
        name: "_strAppend",
        arguments: {
          after: $codePrefix,
          append: $randomCode,
        },
        passOnwardsAs: "discountCode"
      )
      @applyField(
        name: "_intAdd",
        arguments: {
          add: $key,
          to: $firstRecordNumber,
        },
        passOnwardsAs: "recordNumber"
      )
      @applyField(
        name: "_sprintf",
        arguments: {
          string: "%s #%s",
          values: [$discountNamePrefix, $recordNumber],
        },
        passOnwardsAs: "discountName"
      )
      @applyField(
        name: "_echo",
        arguments: {
          value: {
            url: "https://api.lemonsqueezy.com/v1/discounts",
            method: POST,
            options: {
              headers: $lemonSqueezyAPIHeaders
              json: {
                data: {
                  type: "discounts",
                  attributes: {
                    name: $discountName,
                    code: $discountCode,
                    amount: 100,
                    amount_type: "percent",
                    is_limited_to_products: true,
                    is_limited_redemptions: true,
                    max_redemptions: 1,
                    test_mode: $isTestMode,
                  },
                  relationships: {
                    store: {
                      data: {
                        type: "stores",
                        id: $storeID,
                      }
                    },
                    variants: {
                      data: $variants
                    }
                  }
                }
              }
            }
          }
        },
        setResultInResponse: true
      )
    @export(as: "mutationInputs")
    @remove
}
 
query CreateDiscountCodesInLemonSqueezy
  @depends(on: "CreateMutationInputs")
{
  createDiscountCodesInLemonSqueezy: _sendJSONObjectItemHTTPRequests(inputs: $mutationInputs)
    @underEachArrayItem
      @underJSONObjectProperty(by: { path: "data.attributes.code" })
        @export(as: "discountCodes")
}
 
query PrintDiscountCodesFromLemonSqueezy
  @depends(on: "CreateDiscountCodesInLemonSqueezy")
{
  discountCodes: _echo(value: $discountCodes)
}
 
query GetPostWithDiscountCodes($postId: ID)
  @depends(on: "CreateDiscountCodesInLemonSqueezy")
  @include(if: $hasPostId)
{
  post(by: { id: $postId }, status: any) {
    title
    postContent: rawContent
 
    discountCodesAsContent: _arrayJoin(
      array: $discountCodes,
      separator: "\n"
    )
    updatedPostContent: _sprintf(
      string: "%s\n%s"
      values: [$__postContent, $__discountCodesAsContent]
    )
      @export(as: "updatedPostContent")
  }
}
 
mutation UpdatePostWithDiscountCodes($postId: ID)
  @depends(on: "GetPostWithDiscountCodes")
  @include(if: $hasPostId)
{
  updatePost(input: {
    id: $postId,
    contentAs: { html: $updatedPostContent },
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      title
      rawContent
    }
  }
}