-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Implement "containsProducts" PromotionCondition
Relates to #400
- Loading branch information
1 parent
72b6ccd
commit 688d304
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/core/src/config/promotion/conditions/contains-products-condition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { LanguageCode } from '@vendure/common/lib/generated-types'; | ||
import { ID } from '@vendure/common/lib/shared-types'; | ||
|
||
import { idsAreEqual } from '../../../common/utils'; | ||
import { OrderLine } from '../../../entity/order-line/order-line.entity'; | ||
import { Order } from '../../../entity/order/order.entity'; | ||
import { PromotionCondition } from '../promotion-condition'; | ||
|
||
export const containsProducts = new PromotionCondition({ | ||
code: 'contains_products', | ||
description: [ | ||
{ languageCode: LanguageCode.en, value: 'Buy at least { minimum } of the specified products' }, | ||
], | ||
args: { | ||
minimum: { type: 'int' }, | ||
productVariantIds: { | ||
type: 'ID', | ||
list: true, | ||
ui: { component: 'product-selector-form-input' }, | ||
label: [{ languageCode: LanguageCode.en, value: 'Product variants' }], | ||
}, | ||
}, | ||
async check(order: Order, args) { | ||
const ids = args.productVariantIds; | ||
let matches = 0; | ||
for (const line of order.lines) { | ||
if (lineContainsIds(ids, line)) { | ||
matches += line.quantity; | ||
} | ||
} | ||
return args.minimum <= matches; | ||
}, | ||
}); | ||
|
||
function lineContainsIds(ids: ID[], line: OrderLine): boolean { | ||
return !!ids.find(id => idsAreEqual(id, line.productVariant.id)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters