Etiketli müşterilere indirim

VIP ya da wholesale gibi etikete sahip, giriş yapmış müşterilere sipariş toplamından yüzde indirim veren Shopify indirim Function’ı; test edilmiş JavaScript.

Function API
Discount
Hedef (target)
cart.lines.discounts.generate.run
İndirim sınıfı
ORDER
Plan
Herkese açık uygulamada her plan; özel uygulamada Shopify Plus
Uygulama yetkisi
write_discounts
API sürümü
2026-10
Son kontrol

Kısaca

Bu indirim Function’ı, etiketlerinizden birine (örneğin VIP) sahip ve giriş yapmış müşterilere sipariş ara toplamından yüzde indirim verir; varsayılan %10. Etiketler, oran ve mesaj indirimin JSON metafield’ında durur; etiketler sorguya değişken olarak gider. Discount Function API’nin cart.lines.discounts.generate.run hedefini sipariş sınıfıyla (ORDER) kullanır, sürüm 2026-10.

Nasıl kullanılır

  1. Shopify CLI 4.9.0 ya da üstüyle bir uygulama oluşturun ve uzantıyı ekleyin: shopify app generate extension --template discount, dil olarak JavaScript.
  2. Oluşturulan shopify.extension.toml ve src/ dosyalarını aşağıdakilerle değiştirin.
  3. Testleri çalıştırın: shopify app function run --input tests/tagged-customer.input.json.
  4. shopify app deploy ile deploy edin ve discountAutomaticAppCreate ile discountClasses: [ORDER] olan otomatik bir indirim oluşturun.
  5. İndirimin $app:function-configuration metafield’ını ayarlayın; örneğin {"customerTags": ["VIP"], "percentage": 15, "message": "VIP %15 indirim"}.

Kod

shopify.extension.toml
api_version = "2026-10"

[[extensions]]
name = "Discount by customer tag"
handle = "discount-by-customer-tag"
type = "function"

  [[extensions.targeting]]
  target = "cart.lines.discounts.generate.run"
  input_query = "src/cart_lines_discounts_generate_run.graphql"
  input_variables = { namespace = "$app", key = "function-configuration" }
  export = "cart-lines-discounts-generate-run"

  [extensions.build]
  command = ""
  path = "dist/function.wasm"
src/cart_lines_discounts_generate_run.graphql
query CartLinesDiscountsGenerateRunInput($customerTags: [String!]) {
  cart {
    buyerIdentity {
      customer {
        hasAnyTag(tags: $customerTags)
      }
    }
  }
  discount {
    discountClasses
    metafield(namespace: "$app", key: "function-configuration") {
      jsonValue
    }
  }
}
src/cart_lines_discounts_generate_run.js
// @ts-check

/**
 * @typedef {import("../generated/api").CartLinesDiscountsGenerateRunInput} CartLinesDiscountsGenerateRunInput
 * @typedef {import("../generated/api").CartLinesDiscountsGenerateRunResult} CartLinesDiscountsGenerateRunResult
 */

/**
 * Settings stored as JSON in the discount's metafield. The same metafield
 * feeds the query's $customerTags variable (see input_variables in
 * shopify.extension.toml), so Shopify checks the tags.
 */
const DEFAULT_CONFIG = {
  percentage: 10,
  message: "Member discount",
};

/** @type {CartLinesDiscountsGenerateRunResult} */
const NO_DISCOUNT = { operations: [] };

/**
 * @param {CartLinesDiscountsGenerateRunInput} input
 * @returns {CartLinesDiscountsGenerateRunResult}
 */
export function cartLinesDiscountsGenerateRun(input) {
  // A Function may only return operations for the classes the discount was created with.
  if (!input.discount.discountClasses.includes("ORDER")) return NO_DISCOUNT;
  // Guests have no customer record; tags only apply after the customer signs in.
  if (!input.cart.buyerIdentity?.customer?.hasAnyTag) return NO_DISCOUNT;

  const config = { ...DEFAULT_CONFIG, ...input.discount.metafield?.jsonValue };
  return {
    operations: [
      {
        orderDiscountsAdd: {
          candidates: [
            {
              message: config.message,
              targets: [{ orderSubtotal: { excludedCartLineIds: [] } }],
              value: { percentage: { value: String(config.percentage) } },
            },
          ],
          selectionStrategy: "FIRST",
        },
      },
    ],
  };
}
Test girdisi ve beklenen çıktı
tests/guest-checkout.input.json
{
  "cart": { "buyerIdentity": { "customer": null } },
  "discount": {
    "discountClasses": ["ORDER"],
    "metafield": { "jsonValue": { "customerTags": ["VIP", "wholesale"], "percentage": 15, "message": "VIP 15% off" } }
  }
}
tests/guest-checkout.output.json
{ "operations": [] }
tests/tagged-customer.input.json
{
  "cart": { "buyerIdentity": { "customer": { "hasAnyTag": true } } },
  "discount": {
    "discountClasses": ["ORDER"],
    "metafield": { "jsonValue": { "customerTags": ["VIP", "wholesale"], "percentage": 15, "message": "VIP 15% off" } }
  }
}
tests/tagged-customer.output.json
{
  "operations": [
    {
      "orderDiscountsAdd": {
        "candidates": [
          {
            "message": "VIP 15% off",
            "targets": [{ "orderSubtotal": { "excludedCartLineIds": [] } }],
            "value": { "percentage": { "value": "15" } }
          }
        ],
        "selectionStrategy": "FIRST"
      }
    }
  ]
}
tests/untagged-customer.input.json
{
  "cart": { "buyerIdentity": { "customer": { "hasAnyTag": false } } },
  "discount": {
    "discountClasses": ["ORDER"],
    "metafield": { "jsonValue": { "customerTags": ["VIP", "wholesale"], "percentage": 15, "message": "VIP 15% off" } }
  }
}
tests/untagged-customer.output.json
{ "operations": [] }

Lintel tarafından yazıldı. MIT lisansı: kendi mağazanızda da müşteri işlerinizde de ücretsiz kullanabilirsiniz.

Sınırlar ve özel durumlar

  • Müşterinin giriş yapmış olması gerekir; misafir müşterilerin etiketi yoktur.
  • Etiketleri Shopify, hasAnyTag ve $customerTags değişkeniyle kontrol eder; etiketleri değiştirmek için deploy gerekmez.
  • Diğer indirimlerle birleşip birleşmeyeceği indirimin combinesWith ayarlarına bağlıdır.

Sık sorulan sorular

Shopify’da etiketli müşterilere nasıl indirim verilir?
Müşterinin etiketlerini hasAnyTag ile kontrol edip sipariş indirimi döndüren bir Function’a bağlı indirim oluşturun; bu örnek tam olarak bunu yapıyor.
Müşteri checkout sırasında giriş yaparsa ne olur?
Function o anki sepet üzerinde çalışır. Müşteri giriş yaptığında etiket kontrolü geçer ve indirim uygulanır.
İndirimli ürünleri VIP indiriminden hariç tutabilir miyim?
Evet. Sorguya sepet satırlarını ekleyin ve kimliklerini orderSubtotal hedefindeki excludedCartLineIds listesine yazın.
  • Function API sürümü 2026-10 için yazıldı ve gösterilen tarihte Shopify’ın başvuru belgeleriyle karşılaştırıldı. Shopify her çeyrekte yeni bir sürüm yayımlar.
  • Function içeren özel (custom) uygulamalar yalnızca Shopify Plus mağazalarında çalışır. Aynı Function herkese açık bir uygulamada, sayfada aksi yazmıyorsa her planda çalışır.
  • Her örnek test verileriyle gelir ve yayımlamadan önce bu testleri çalıştırırız. Kodu değiştirdikten sonra testleri tekrar çalıştırın: shopify app function run.
  • MIT lisansı. Kodu kendi mağazanızda da müşteri işlerinizde de kullanabilirsiniz.

Mağazanız için kurulmasını, test edilmesini ve bakımını isterseniz: Shopify Functions geliştirme.

Geliştirmek istediğiniz bir şey mi var? Gelin, birlikte konuşalım.

Aynı anda sınırlı sayıda proje alıyoruz.

Gencer KarakayaShopify mühendisi

Proje özetinizi bir mühendis okur; size e-postayla yanıt veririz.