Skip to content

推薦結果詳細說明

讓我們用簡單的例子說明。

假設商品資料為:

ts
const a30: Commodity = {
  key: '1',
  name: 'A-30ml',
  price: 100
}

const a50: Commodity = {
  key: '2',
  name: 'A-50ml',
  price: 200
}

優惠如下:

ts
const promotionAnyTwoA15Off: Promotion = {
  key: '5',
  type: 'commodity-offer',
  name: 'A 商品優惠',
  description: '30ml、50ml,任兩件 85 折',
  contents: [
    // ...
  ]
}

const promotionA5Get1Free: Promotion = {
  key: '10',
  type: 'commodity-offer',
  name: `A 商品優惠`,
  description: `A 30ml 買 5 送 1`,
  contents: [
    // ...
  ],
}

const promotion100ForEvery1000 = {
  key: '7',
  type: 'minimum-spend-offer',
  name: `周年慶大特價`,
  description: `每 1000 送 100`,
  contents: [
    // ...
  ],
}

contents 內容同優惠定義,此章節不再贅述。

不會同時推薦的優惠

假設現在買了 1 個 a30 並候選 promotionAnyTwoA15OffpromotionA5Get1Free 優惠。

ts
const results = getRecommendResults({
  items: [a30],
  promotions: [promotionAnyTwoA15Off, promotionA5Get1Free],
})

由於只有 1 個 a30,「A 系列任兩件 85 折」與「A 30ml 買 5 送 1」這兩個優惠只能擇一使用。

所以 results 會有 2 個項目(對應至以上 2 個優惠):

ts
[
  {
    recommendedList: [
      {
        bundledItems: [
          {
            commodity: a30,
            quantity: 1
          }
        ],
        content: {
          list: [
            {
              commodities: [a30, a50],
              comparison: {
                logic: 'equal',
                target: 'purchase-quantity',
                value: 2
              },
              quantity: 1
            }
          ]
        },
        promotedContent: {
          // ...
        },
        promotion: {
          key: '7',
          type: 'commodity-offer',
          name: 'A 商品優惠',
          description: 'A 30ml、50ml,任兩件 85 折',
          contents: [
            // ...
          ]
        }
      }
    ]
  },
  {
    recommendedList: [
      {
        bundledItems: [
          {
            commodity: a30,
            quantity: 1
          }
        ],
        content: {
          list: [
            {
              commodities: [a30],
              comparison: {
                logic: 'equal',
                target: 'purchase-quantity',
                value: 5
              },
              quantity: 4
            }
          ]
        },
        promotedContent: {
          // ...
        },
        promotion: {
          key: '8',
          type: 'commodity-offer',
          name: 'A 商品優惠',
          description: 'A 30ml 買 5 送 1',
          contents: []
        }
      }
    ]
  }
]

results 中的項目只能擇一使用,意思是使用了 results[0] 的優惠後,就不可能套用 results[1] 的優惠,反之如此。

TIP

resultsrecommendedList 中的所有項目則是可同時套用,讓我們在下一範例探討。

現在讓我們仔細看看內容。

promotedContent、promotion

promotion 為推薦優惠,而 promotedContent 表示此推薦匹配的 promotion.contents 內容。

具體達成條件則在 content 中說明。

bundledItems

表示與此推薦綁定的商品。

可以發現 2 個推薦結果的 bundledItems 都是 1 個 a30,符合目前只有買 1 個 a30。

content

content 中的 list 表示此推薦具體的達成條件。

可以看到「A 30ml、50ml,任兩件 85 折」優惠的 content 為:

ts
content: {
  list: [
    {
      commodities: [a30, a50],
      // `comparison` 用於紀錄比對條件,可忽略。
      comparison: {},
      quantity: 1
    }
  ]
}

此內容表示可以在買 1 個 a30 或 a50,即可達成此優惠。

而「A 30ml 買 5 送 1」優惠的 content 為:

ts
content: {
  list: [
    {
      commodities: [a30],
      comparison: {},
      quantity: 4
    }
  ]
}

同上類推,此內容表示買 4 個 a30 即可達成此優惠。

可同時使用的優惠

假設現在買了 1 個 a30 並候選 promotionAnyTwoA15Offpromotion100ForEvery1000 優惠。

ts
const results = getRecommendResults({
  items: [a30],
  promotions: [promotionAnyTwoA15Off, promotion100ForEvery1000],
})

「A 系列任兩件 85 折」與「每 1000 送 100」這兩個優惠可以同時使用。

所以 resultsrecommendedList 會有 2 個項目:

ts
[
  {
    recommendedList: [
      {
        bundledItems: [
          {
            commodity: a30,
            quantity: 1
          }
        ],
        content: {
          list: [
            {
              commodities: [a30, a50],
              comparison: {
                logic: 'equal',
                target: 'purchase-quantity',
                value: 2
              },
              quantity: 1
            }
          ]
        },
        promotedContent: {
          // ...
        },
        promotion: {
          key: '5',
          type: 'commodity-offer',
          name: 'A 商品優惠',
          description: 'A 30ml、50ml,任兩件 85 折',
          contents: [
            // ...
          ]
        }
      },
      {
        bundledItems: [],
        content: {
          price: 1000
        },
        promotedContent: {
          // ...
        },
        promotion: {
          key: '7',
          type: 'minimum-spend-offer',
          name: `周年慶大特價`,
          description: `每 1000 送 100`,
          contents: [
            // ...
          ]
        }
      }
    ]
  }
]

「A 30ml、50ml,任兩件 85 折」內容與上一個範例相同,我們來詳細看看「每 1000 送 100」部分。

promotedContent、promotion

同理,不贅述。

bundledItems

滿額優惠不綁定特定商品,所以 bundledItems 為空陣列。

content

可以看到 content 中不是 list 而是 price,這表示達成此推薦的價格。

由於此優惠是「每 1000 送 100」,達成條件為滿 1100 元,所以 price 為:

txt
1100 - 100(1 個 A 30ml)= 1000