Migrating a percentage off the cheapest item script
Shopify shipped this one in the picker, so it is widely copied, and almost every copy carries the same gate: more than one cart LINE. Two of the same item on one line is two units and one line, and the original stayed silent on it.
再構築可It found the cheapest line in the cart and took a percentage off one unit of it.
この処理はマーチャントによって 2 通りの書き方がされており、すべてが同じ意味とは限りません。それぞれの書き方を、専用のカートと数値とともに以下に示します。
スクリプトレスキューに貼り付けて、何が再現されるか確認する
- このページの各スクリプトの横にあるボタンで Ruby をコピーします。
- Shopify 管理画面で Stackable を開き、ツール、スクリプトレスキューの順に進みます。
- そこに貼り付け、ツールが作成すると示した内容を読み、このページの設定と見比べます。
スクリプトレスキューは Pro プランの機能です。以前のスクリプトの割引額を入力し、両方の金額が最小通貨単位まで一致するまで、何も公開されません。
Cheapest item percentage line count (D15)
再構築可危険なペア D15。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
DISCOUNT_AMOUNT = 50
if (Input.cart.line_items.size > 1)
line_item = Input.cart.line_items.sort_by { |line_item| line_item.variant.price }.first
if line_item.quantity > 1
line_item = line_item.split(take: 1)
Input.cart.line_items << line_item
end
line_item.change_line_price(
line_item.line_price * (1.0 - (DISCOUNT_AMOUNT / 100.0)),
message: "50% off your least expensive item"
)
end
Output.cart = Input.cartCheapest item discounted として認識され、bxgy タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。
| 設定項目 | 値 | 設定キー |
|---|---|---|
| Offer type | bxgy | type |
| Counting | per_group | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 2 -> free item | tiers[0] |
| Buy quantity | 2 | bxgy.buyQty |
| Get quantity | 1 | bxgy.getQty |
| Get selection | cheapest_eligible | bxgy.getSelection |
| Reward on the get units | 50% off | bxgy.getDiscountPct |
| Free units are additional | false | bxgy.freeUnitsAreAdditional |
| Target scope | all | target.scope |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- Your Script counted separate cart lines. We count units, so one line holding two items now qualifies where it did not before. Confirm the buy requirement.
- Confirm which products this applies to. We could not read product targeting from the script.
- Your Script needed two separate cart lines. This offer counts units, so two of the same item on one line now qualifies.
公開デモストア Meridian Goods で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。
| 商品 | 数量 | 単価 |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $32.00 | |
| 割引 | -$7.00 | |
| 合計 | $25.00 | |
未ログイン、またはタグのない顧客としてログインしています。
公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Braided USB-C Cable 2、単価 $14.00 に設定すれば、答えは一つに定まります。
割引額は $7.00 です。購入者が支払った合計は $21.00 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
- swap the cart for a single Braided USB-C Cable
| 商品 | 数量 | 単価 |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 合計 | $14.00 | |
未ログイン、またはタグのない顧客としてログインしています。
Cheapest item percentage unit count (D15)
再構築可危険なペア D15。このファイルと対になるファイルは見た目がほぼ同じで、動作が異なります。どちらを使っているか判断する前に両方を読んでください。
DISCOUNT_AMOUNT = 50
total_units = Input.cart.line_items.map(&:quantity).reduce(0, :+)
if total_units >= 2
line_item = Input.cart.line_items.sort_by { |li| li.variant.price }.first
if line_item.quantity > 1
line_item = line_item.split(take: 1)
Input.cart.line_items << line_item
end
line_item.change_line_price(
line_item.line_price * (1.0 - (DISCOUNT_AMOUNT / 100.0)),
message: "50% off your least expensive item"
)
end
Output.cart = Input.cartCheapest item discounted として認識され、bxgy タイプのオファーとして再構築されます。以下はオファーが実際に保存される設定キーそのものなので、ご自身の再構築結果を項目ごとに照合できます。
| 設定項目 | 値 | 設定キー |
|---|---|---|
| Offer type | bxgy | type |
| Counting | per_group | counting |
| Recurrence | once | recurrence |
| Tier 1 | min qty 2 -> free item | tiers[0] |
| Buy quantity | 2 | bxgy.buyQty |
| Get quantity | 1 | bxgy.getQty |
| Get selection | cheapest_eligible | bxgy.getSelection |
| Reward on the get units | 50% off | bxgy.getDiscountPct |
| Free units are additional | false | bxgy.freeUnitsAreAdditional |
| Target scope | all | target.scope |
| Method | automatic | discountMethod |
| Allocation | standard | stacking.allocation |
| Combines with other discounts | no | stacking |
- Confirm which products this applies to. We could not read product targeting from the script.
公開デモストア Meridian Goods で作成しているため、ここに載せた価格はすべて、信じるのではなく再現して確かめられます。以下の割引額は、決済時に動くのと同じエンジンがこのカートに対して算出した金額です。
| 商品 | 数量 | 単価 |
|---|---|---|
| Softcover Notebook Set | 1 | $18.00 |
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $32.00 | |
| 割引 | -$7.00 | |
| 合計 | $25.00 | |
未ログイン、またはタグのない顧客としてログインしています。
公開前に、ツールは手元のカートに対して以前の Script が何をしていたかを尋ねます。カートを Braided USB-C Cable 2、単価 $14.00 に設定すれば、答えは一つに定まります。
割引額は $7.00 です。購入者が支払った合計は $21.00 です。
照合は通貨の最小単位まで完全一致で行われます。1 単位でもずれれば公開はできません。それがこの手順の目的です。
- swap the cart for a single Braided USB-C Cable
| 商品 | 数量 | 単価 |
|---|---|---|
| Braided USB-C Cable | 1 | $14.00 |
| 小計 | $14.00 | |
| 合計 | $14.00 | |
未ログイン、またはタグのない顧客としてログインしています。
見た目がそっくりな 2 つのファイル
以下の各ペアは、同じ形のスクリプトを 2 通りに書いたものです。違いは 1 行か 2 行ですが、動作は異なります。どちらが手元のものか決める前に、両方を読んでください。
紛らわしいペア D15
Cheapest item percentage line count (D15)
再構築可bxgy オファーとして再現されます。
このカートでの割引: $7.00
if (Input.cart.line_items.size > 1)
line_item = Input.cart.line_items.sort_by { |line_item| line_item.variant.price }.firstCheapest item percentage unit count (D15)
再構築可bxgy オファーとして再現されます。
このカートでの割引: $7.00
total_units = Input.cart.line_items.map(&:quantity).reduce(0, :+)
if total_units >= 2
line_item = Input.cart.line_items.sort_by { |li| li.variant.price }.firstよくある質問
- Will Stackable rebuild this script?
- Yes. Every spelling on this page is rebuilt today as a bxgy offer, and the cart above is the one that proves it.
- What cart proves it works?
- 1 x Softcover Notebook Set at $18.00 plus 1 x Braided USB-C Cable at $14.00. The engine takes $7.00 off, and checkout charges $25.00.
- What will I have to check before publishing?
- Your Script counted separate cart lines. We count units, so one line holding two items now qualifies where it did not before. Confirm the buy requirement. Confirm which products this applies to. We could not read product targeting from the script. Your Script needed two separate cart lines. This offer counts units, so two of the same item on one line now qualifies.
関連する Script の型
A set of paid units earns an extra unit at no charge.
The same buy-and-get offer, counted so the reward sits inside each set rather than beside it.
The discount applied to the first few units only, with the rest at full price.