Queries-Bibliothek
Queries-BibliothekTausende von Rabattcodes für AppSumo in FluentCart erstellen

Tausende von Rabattcodes für AppSumo in FluentCart erstellen

Diese query verbindet sich mit der FluentCart REST API und erstellt auf einen Schlag hundert 100%-Rabattcodes.

Führe diese query mehrmals aus, um die 10.000 Codes zu erstellen, die für eine AppSumo-Kampagne benötigt werden.

Du musst Folgendes angeben:

  • Den Benutzernamen und das Anwendungspasswort zur Verbindung mit der FluentCart REST API, über die Variablen $wpUsername und $wpApplicationPassword
  • Die Domain des FluentCart-Shops, über die Variable $fluentCartDomain
  • Die Produktvariante(n), die mit dem Code eingelöst werden sollen, über die Variable $productVariationIDs

Der generierte Rabattcode ist eine zufällige Zeichenfolge. Du kannst dem Code ein Präfix voranstellen (über die Variable $codePrefix), die Länge des Codes festlegen (über die Variable $codeLength) und den Namen des Rabattcodes anpassen (über die Variablen $discountNamePrefix und $firstRecordNumber), um ihn im FluentCart-Dashboard zu finden.

Sammle alle neu erstellten Rabattcodes, indem du eine $postId angibst – dann werden alle Codes am Ende dieses Beitrags angehängt.

# Export FluentCart API config and build mutation inputs for creating coupons.
# FluentCart REST API: { fluentCartDomain }/wp-json/fluent-cart/v2
# Auth: WordPress Application Passwords (Basic auth: username + application_password)
# Create Coupon: POST /coupons — https://dev.fluentcart.com/restapi/operations/coupons/create-coupon
 
query ExportFluentCartAPIData(
  $fluentCartDomain: String!,
  $fluentCartBaseURL: String! = "wp-json/fluent-cart/v2",
  $postId: ID,
) {
  fluentCartCouponsURL: _sprintf(
    string: "%s/%s/coupons",
    values: [$fluentCartDomain, $fluentCartBaseURL]
  )
    @export(as: "fluentCartCouponsURL")
    @remove
 
  hasPostId: _notEmpty(value: $postId)
    @export(as: "hasPostId")
}
 
query CreateMutationInputs(
  $wpUsername: String!,
  $wpApplicationPassword: String!,
  $discountNamePrefix: String! = "AppSumo campaign",
  $discountNotes: String! = "",
  $codePrefix: String! = "",
  $numberCodes: Int! = 100,
  $codeLength: Int! = 16,
  $firstRecordNumber: Int! = 1,
  $productVariationIDs: [ID!],
)
  @depends(on: "ExportFluentCartAPIData")
{
  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: $fluentCartCouponsURL,
            method: POST,
            options: {
              auth: {
                username: $wpUsername,
                password: $wpApplicationPassword,
              },
              headers: [
                {
                  name: "Content-Type",
                  value: "application/json",
                },
              ],
              json: {
                title: $discountName,
                code: $discountCode,
                type: "percentage",
                amount: 100,
                status: "active",
                stackable: "no",
                show_on_checkout: "no",
                priority: 0,
                notes: $discountNotes,
                conditions: {
                  max_uses: 1,
                  included_products: $productVariationIDs
                }
              }
            }
          }
        },
        setResultInResponse: true
      )
    @export(as: "mutationInputs")
    @remove
}
 
query CreateCouponsInFluentCart
  @depends(on: "CreateMutationInputs")
{
  createCouponsInFluentCart: _sendJSONObjectItemHTTPRequests(inputs: $mutationInputs)
    @underEachArrayItem
      @underJSONObjectProperty(by: { path: "data.code" })
        @export(as: "discountCodes")
}
 
query PrintDiscountCodesFromFluentCart
  @depends(on: "CreateCouponsInFluentCart")
{
  discountCodes: _echo(value: $discountCodes)
}
 
query GetPostWithDiscountCodes($postId: ID)
  @depends(on: "CreateCouponsInFluentCart")
  @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
    }
  }
}

...mit den folgenden Variablen:

{
  "fluentCartDomain": "{ fluentCartDomain }",
  "wpUsername": "{ username }",
  "wpApplicationPassword": "{ appPassword }",
  "codeLength": 24,
  "codePrefix": "APS2V1T1",
  "discountNamePrefix": "AppSumo campaign@2",
  "postId": "{ postId }",
  "discountNotes": "AppSumo campaign #2, starting on 26/03/2026",
  "productVariationIDs": [ "{ productVariationID }" ]
}