# 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.

Sayfa: https://lintelagency.com/tr/kaynaklar/shopify-functions/musteri-etiketine-gore-indirim

## 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.

## Bilgiler

- **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:** 2 Kasım 2026

## 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

```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

```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

```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

```json
{
  "cart": { "buyerIdentity": { "customer": null } },
  "discount": {
    "discountClasses": ["ORDER"],
    "metafield": { "jsonValue": { "customerTags": ["VIP", "wholesale"], "percentage": 15, "message": "VIP 15% off" } }
  }
}
```

### tests/guest-checkout.output.json

```json
{ "operations": [] }
```

### tests/tagged-customer.input.json

```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

```json
{
  "operations": [
    {
      "orderDiscountsAdd": {
        "candidates": [
          {
            "message": "VIP 15% off",
            "targets": [{ "orderSubtotal": { "excludedCartLineIds": [] } }],
            "value": { "percentage": { "value": "15" } }
          }
        ],
        "selectionStrategy": "FIRST"
      }
    }
  ]
}
```

### tests/untagged-customer.input.json

```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

```json
{ "operations": [] }
```

## 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.

## 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.

## Shopify belgeleri

- [Discount Function API](https://shopify.dev/docs/api/functions/latest/discount)
- [Variables in input queries](https://shopify.dev/docs/apps/build/functions/input-queries/use-variables-input-queries)

## İlgili içerikler

- [Bir indirim Function’ını otomatik indirim olarak etkinleştirme](https://lintelagency.com/tr/kaynaklar/graphql/function-tabanli-otomatik-indirim)
- [Ürünleri etiketli müşterilere sınırlama](https://lintelagency.com/tr/kaynaklar/shopify-functions/urunleri-etiketli-musterilere-sinirlama)
- [Shopify Scripts çalışmayı bıraktı. Her script’in mantığı artık nereye gidiyor?](https://lintelagency.com/tr/blog/shopify-scriptsten-functionsa-gecis)
- [Function’ları metafield (meta alan) ile yapılandırmak: kuralı yeni sürüm yayınlamadan değiştirin](https://lintelagency.com/tr/blog/shopify-functions-metafield-ile-yapilandirma)

Lisans: MIT. Kendi mağazanızda da müşteri işlerinizde de ücretsiz kullanabilirsiniz.
