> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aftersell.com/llms.txt
> Use this file to discover all available pages before exploring further.

# カートオブジェクト

> Aftersell Cart SDK のカートとそのラインの構造: カート、カートライン、バンドル、販売プランのすべてのフィールド。

1 つのオブジェクトの形が SDK 全体を流れます。[`getCart()`](/ja/aftersell/cart/sdk-actions#getcart) が返すもの、[`cart_loaded` と `cart_updated`](/ja/aftersell/cart/sdk-events) がハンドラに渡すもの、そして [Custom code ブロック](/ja/aftersell/cart/custom-code-blocks)が受け取るものがこれです。

<Note>
  **すべての金額は通貨の最小単位**（USD ならセント）で、フォーマット済みの文字列になることはありません。`5779` は \$57.79 です。表示には [`formatMoney`](/ja/aftersell/cart/sdk-actions#formatmoneycents) を使ってください。
</Note>

<div id="the-cart">
  ## カート
</div>

| フィールド                  | 型                        | 説明                                                                                             |
| ---------------------- | ------------------------ | ---------------------------------------------------------------------------------------------- |
| `token`                | `string`                 | Shopify のカートトークン。                                                                              |
| `items`                | `AftersellCartLine[]`    | ラインアイテム。[カートライン](#cart-lines)を参照。                                                              |
| `itemCount`            | `number`                 | 買い物客が見ているアイテムの合計数量。                                                                            |
| `hasSubscriptionItems` | `boolean`                | `items` 内の少なくとも 1 つのラインが販売プランを持つ場合に `true`。`itemCount` が除外するアドオンラインも含みます。空のカートでは `false`。      |
| `totalPrice`           | `number`                 | 現在の合計（セント単位）。                                                                                  |
| `originalTotalPrice`   | `number`                 | 割引前の合計（セント単位）。                                                                                 |
| `totalDiscount`        | `number`                 | 割引の合計（セント単位）。                                                                                  |
| `compareAtTotalPrice`  | `number \| null`         | 各ラインの compare-at（MSRP）× 数量の合計（セント単位）。利用できない場合は `null` なので、`originalTotalPrice` にフォールバックしてください。 |
| `currency`             | `string`                 | 通貨コード。                                                                                         |
| `discountCodes`        | `string[]`               | カートで受け付けられたディスカウントコード（ソート済み）。ない場合は `[]`。                                                       |
| `attributes`           | `Record<string, string>` | カート属性。SDK からは読み取り専用。                                                                           |

<Warning>
  **`itemCount` は必ずしも `items` の合計と一致しません。** `items` は実際の Shopify カートを反映し、配送保護など、ドロワーが非表示にするアドオンラインも含みます。`itemCount` はカートバッジと一致する買い物客向けの数字です。「買い物客がいくつ選んだか」には `itemCount` を、カートがレンダリングしているラインを走査するには `items` を使ってください。

  `items` から完全に欠けているものが 2 つあります。[`setHidden`](/ja/aftersell/cart/sdk-hooks#registerlinetransform) で非表示になったラインと、アンカーに移動する[バンドルの子](#bundles)です。どちらも Shopify から直接取得されるカート合計には引き続き計上されます。
</Warning>

```js theme={"theme":{"light":"snazzy-light","dark":"github-dark"}}
window.aftersell.cart.events.on('cart_updated', (state) => {
  console.log(state.itemCount, 'items');
  console.log('Total:', window.aftersell.cart.actions.formatMoney(state.totalPrice));
  console.log('Saved:', window.aftersell.cart.actions.formatMoney(state.totalDiscount));
  console.log('Codes:', state.discountCodes.join(', ') || 'none');
});
```

<div id="cart-lines">
  ## カートライン
</div>

`items` の各エントリ、および [`item_added`](/ja/aftersell/cart/sdk-events#item_added) と [`item_removed`](/ja/aftersell/cart/sdk-events#item_removed) の `item`:

| フィールド                 | 型                                | 説明                                                                                                                                   |
| --------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `key`                 | `string`                         | ラインの Shopify key。アイテム系の[アクション](/ja/aftersell/cart/sdk-actions)に渡します。                                                                 |
| `productId`           | `number`                         | Shopify の商品 ID。                                                                                                                      |
| `variantId`           | `number`                         | Shopify のバリアント ID。                                                                                                                   |
| `handle`              | `string`                         | 商品ハンドル。                                                                                                                              |
| `title`               | `string`                         | 表示タイトル。                                                                                                                              |
| `productTitle`        | `string`                         | バリアントを含まない商品タイトル。                                                                                                                    |
| `variantTitle`        | `string \| null`                 | バリアントのラベル、または `null`。                                                                                                                |
| `variantOptions`      | `Array<{ name, value }>`         | 選択されたオプション。例: `[{ name: 'Size', value: 'Medium' }]`。単一バリアント商品では Shopify は `Title: Default Title` を出力します。                             |
| `quantity`            | `number`                         | このラインの数量。                                                                                                                            |
| `linePrice`           | `number`                         | ラインの価格（セント単位）。                                                                                                                       |
| `finalLinePrice`      | `number`                         | 割引後のラインの価格（セント単位）。                                                                                                                   |
| `originalLinePrice`   | `number`                         | 割引前のラインの価格（セント単位）。                                                                                                                   |
| `compareAtPrice`      | `number \| null`                 | バリアントの compare-at（MSRP）の**単価**（セント単位）。ない場合は `null`。                                                                                  |
| `properties`          | `Record<string, string> \| null` | ラインアイテムのプロパティ。                                                                                                                       |
| `internalProperties`  | `Record<string, string>`         | [`registerLineTransform`](/ja/aftersell/cart/sdk-hooks#registerlinetransform) によるレンダリング専用のオーバーレイ。Shopify に永続化されることはありません。ない場合は `{}`。 |
| `discountAllocations` | `Array<{ title, amount }>`       | このラインに適用された割引。`amount` はセント単位。ない場合は `[]`。                                                                                            |
| `isGiftCard`          | `boolean`                        | ラインがギフトカードかどうか。                                                                                                                      |
| `sellingPlan`         | `{ id, name } \| null`           | 有効なサブスクリプションプラン。単発購入の場合は `null`。                                                                                                     |
| `bundle`              | `AftersellCartBundle \| null`    | アンカーライン上の[バンドル](#bundles)ビューモデル。バンドル以外のラインと子では `null`。                                                                               |
| `metadata`            | `Record<string, unknown>`        | エンリッチャーの `id` をキーとする[エンリッチメント](/ja/aftersell/cart/sdk-hooks#registercartenricher)データ。エンリッチャーが設定するまでは `{}`。                           |

<Warning>
  `properties` には、商品フォームのカスタムテキストフィールドなど、買い物客が入力した内容が含まれることがあります。生の HTML としてではなく、必ずテキストとしてレンダリングしてください。
</Warning>

<div id="identifying-a-line">
  ### ラインの識別
</div>

ラインに対して操作を行うものには `key` を、*商品*を識別するものには `variantId` または `productId` を使ってください:

```js theme={"theme":{"light":"snazzy-light","dark":"github-dark"}}
// ✅ Acting on a line: use key.
window.aftersell.cart.actions.removeItem(line.key);

// ✅ Recognising a product: use variantId.
const hasGift = state.items.some((line) => line.variantId === GIFT_VARIANT_ID);
```

プロパティが異なると、同じバリアントが複数のラインに現れることがあります。刻印テキストの異なる 2 つの刻印入りマグカップは、1 つの `variantId` を共有する 2 つのラインです。アクションが `key` を取るのはこのためです。

<div id="prices-on-a-line">
  ### ラインの価格
</div>

混同しやすい 3 つの価格:

| 知りたいもの            | 使うフィールド                       |
| ----------------- | ----------------------------- |
| このラインで買い物客が支払う金額  | `finalLinePrice`              |
| カート割引前の金額         | `originalLinePrice`           |
| MSRP の取り消し線表示（単価） | `compareAtPrice` × `quantity` |

```js theme={"theme":{"light":"snazzy-light","dark":"github-dark"}}
// Is this line discounted?
const isDiscounted = line.finalLinePrice < line.originalLinePrice;

// Is it free? (A common way to detect a gift line.)
const isFree = line.finalLinePrice === 0;
```

<div id="bundles">
  ## バンドル
</div>

ラインがバンドルにグループ化されると、**アンカー**ラインが `bundle` オブジェクトを持ちます。子はその中に折りたたまれ、`items` に単独では現れなくなります。グループ化の設定方法については、[他のアプリのバンドルラインをグループ化する](/ja/aftersell/cart/sdk-use-case-bundles)を参照してください。

| フィールド          | 型                        | 説明                                              |
| -------------- | ------------------------ | ----------------------------------------------- |
| `id`           | `string`                 | バンドルの識別子。                                       |
| `source`       | `'native' \| 'grouped'`  | Shopify ネイティブのバンドルか、Aftersell によってグループ化されたラインか。 |
| `memberKeys`   | `string[]`               | バンドル内のすべてのラインの `key`。                           |
| `children`     | `AftersellBundleChild[]` | バンドルの内容。                                        |
| `displayPrice` | `number`                 | バンドルとして表示される価格（セント単位）。                          |

各子は `key`（ネイティブコンポーネントの場合は `null`）、`title`、`variantTitle`、`quantity`、`perAnchorQty`、`imageUrl`、`finalLinePrice`、`originalLinePrice`、`compareAtPrice` を持ちます。

```js theme={"theme":{"light":"snazzy-light","dark":"github-dark"}}
// Skip bundle children when totalling your own line list.
const topLevel = state.items.filter((line) => !isBundleChild(line, state));
```

<div id="subscription-plans">
  ## サブスクリプションプラン
</div>

ラインの有効なプランは `sellingPlan` で、単発購入の場合は `null` です。カート全体について知りたい場合は、自分でラインを走査するのではなく `hasSubscriptionItems` を読んでください。これは `items` には現れるが `itemCount` からは除外されるアドオンラインもカウントします:

```js theme={"theme":{"light":"snazzy-light","dark":"github-dark"}}
if (state.hasSubscriptionItems) {
  // The cart contains at least one subscription line.
}

const subscriptions = state.items.filter((line) => line.sellingPlan);
console.log(subscriptions.length, 'subscription lines');
```

ラインで*選択可能な*プラン（ピッカーに表示されるもの）はカートオブジェクトにはありません。それらは [`registerSubscriptionOptionsTransform`](/ja/aftersell/cart/sdk-hooks#registersubscriptionoptionstransform) と [`registerDefaultSubscriptionOptionSelector`](/ja/aftersell/cart/sdk-hooks#registerdefaultsubscriptionoptionselector) で加工してください。

<div id="where-to-go-next">
  ## 次のステップ
</div>

* **[アクション](/ja/aftersell/cart/sdk-actions)**: カートを読み取り、変更します。
* **[イベント](/ja/aftersell/cart/sdk-events)**: このオブジェクトの供給元。
* **[フック](/ja/aftersell/cart/sdk-hooks)**: エンリッチャーでラインに独自のデータを追加します。
* **[ユースケース](/ja/aftersell/cart/sdk-use-cases)**: これらのフィールドを読み取る完全なソリューション。
