feat(medusa, admin-ui): Improve gift card application (#4944)
Fix for the problems identified in issue #4892 Bugfix: admin-ui order summary no longer uses gift card total from order when displaying how much has been withdrawn from each giftcard. Bugfix(?): no longer keep applying gift cards (at 0 value) when sufficient balance has been reached Feature: multiple giftcards are now applied in ordered fashion. First by end_date (supports null), then by remaining balance. In order to ensure that customers ends up with as long lasting and few remaining gift cards as possible after the transaction.
This commit is contained in:
@@ -733,7 +733,13 @@ class OrderService extends TransactionBaseService {
|
||||
let giftCardableAmountBalance = giftCardableAmount
|
||||
const giftCardService = this.giftCardService_.withTransaction(manager)
|
||||
|
||||
for (const giftCard of cart.gift_cards) {
|
||||
//Order the gift cards by first ends_at date, then remaining amount. To ensure largest possible amount left, for longest possible time.
|
||||
const orderedGiftCards = cart.gift_cards.sort((a, b) => {
|
||||
let aEnd = a.ends_at ?? new Date(2100, 1, 1)
|
||||
let bEnd = b.ends_at ?? new Date(2100, 1, 1)
|
||||
return aEnd.getTime() - bEnd.getTime() || a.balance - b.balance
|
||||
})
|
||||
for (const giftCard of orderedGiftCards) {
|
||||
const newGiftCardBalance = Math.max(
|
||||
0,
|
||||
giftCard.balance - giftCardableAmountBalance
|
||||
@@ -755,6 +761,9 @@ class OrderService extends TransactionBaseService {
|
||||
|
||||
giftCardableAmountBalance =
|
||||
giftCardableAmountBalance - giftCardBalanceUsed
|
||||
|
||||
if (giftCardableAmountBalance == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
const shippingOptionServiceTx =
|
||||
|
||||
Reference in New Issue
Block a user