Query 库在 FluentCart 中为 AppSumo 创建数千个折扣码
在 FluentCart 中为 AppSumo 创建数千个折扣码
此 query 连接到 FluentCart REST API,一次性创建 100 个 100% 折扣码。
多次执行此 query 以创建运行 AppSumo 活动所需的 10,000 个折扣码。
您需要提供:
- 连接 FluentCart REST API 所需的用户名和应用程序密码,通过变量
$wpUsername和$wpApplicationPassword传入 - FluentCart 商店的域名,通过变量
$fluentCartDomain传入 - 该折扣码可兑换的商品变体,通过变量
$productVariationIDs传入
生成的折扣码将是一个随机字符串。您可以为折扣码添加前缀(通过变量 $codePrefix),指定折扣码的长度(通过变量 $codeLength),并自定义折扣码的名称(通过变量 $discountNamePrefix 和 $firstRecordNumber),以便在 FluentCart 控制台中进行搜索。
通过提供 $postId,可以收集所有新创建的折扣码,并将其追加到该文章末尾。
# 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
}
}
}...传入以下变量:
{
"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 }" ]
}