Installation
Install npm package with Copy to clipboard
yarn add medusa-plugin-promotion
or Copy to clipboardnpm i medusa-plugin-promotion
Peer dependencies are Copy to clipboard
@medusajs/medusa
Copy to clipboardmedusa-interfaces
Copy to clipboardtypeorm
Application apply
In folder Copy to clipboard
src/subscribers
create file Copy to clipboardpromotions-handler.ts
to subscribe event Copy to clipboardCREATED
,Copy to clipboardUPDATED
, of cartStep 1
Add Copy to clipboard
InjectionDepen
interface123456789101112// src/subscribers/promotions-handler.tsinterface InjectionDepen {//...// Add moreeventBusService: EventBusServicepromotionService: PromotionServicecartService: CartServiceorderService: OrderServicecartRepository: typeof CartRepositorymanager: EntityManager}
Step 2
Create class Copy to clipboard
PromotionHandler
and add property12345678910111213141516171819202122232425262728293031323334353637383940414243444546class PromotionsHandler extends TransactionBaseService {protected manager_: EntityManagerprotected transactionManager_: EntityManagercartService: CartServiceeventBusService: EventBusServicepromotionService: PromotionServiceorderService: OrderServicecartRepository: typeof CartRepositoryconstructor({manager,eventBusService,promotionService,cartService,orderService,cartRepository,}: InjectionDepen) {super(arguments[0])this.cartService = cartServicethis.eventBusService = eventBusServicethis.promotionService = promotionServicethis.orderService = orderServicethis.cartRepository = cartRepositorythis.manager_ = managereventBusService.subscribe(CartService.Events.CREATED, async (data: Cart) => {//Add logic handle apply promotion here//You can call property of promotion service and use it here//Example like : await this.promotionService.applyPromotions(cart, listPromotionAuto)})eventBusService.subscribe(CartService.Events.UPDATED, async (data: Cart) => {//Add logic handle apply promotion here//You can call property of promotion service and use it here//Example like : await this.promotionService.applyPromotions(cart, listPromotionAuto)})}}export default PromotionsHandler
Promotion method
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970export class PromotionService extends TransactionBaseService {//Create promotionasync create(data: PromotionCreateValidator) {}//Update promotionasync update(data: PromotionUpdateValidator) {}//Delete promotionasync delete(data: PromotionDeleteValidator) {}//add action of promotionasync addAction(data: AddPromotionActionValidator) {}//update action of promotionasync updateAction(data: UpdatePromotionActionValidator) {}//delete action of promotionasync deleteAction(data: DeletePromotionActionValidator) {}//add condition of promotionasync addCondition(data: DeletePromotionActionValidator) {}//update condition of promotionasync updateCondition(data: UpdatePromotionConditionValidator) {}//delete condition of promotionasync deleteCondition(data: DeletePromotionConditionValidator) {}//set resource promotion conditionasync setResource(data: SetPromotionConditionResource) {}//resolve resource promotion conditionasync resolveResource(resourceName: string, resourceId: string) {}//retrieve promotionasync retrieve(id: string, relations: string[] = []) {}//get list promotion of cartasync listPromotionForCart(cart: Cart) {}//get list promotionasync list(query: ListPromotionQuery | ListPromotionQuery[]) {}//Validate usage limit conditionhasReachedLimit(promotion: Promotion): boolean {}//Validate start time of promotionhasNotStarted(promotion: Promotion): boolean {}//Validate expired of promotionhasExpired(promotion: Promotion): boolean {}//Validate region condition of promotionasync isValidForRegion(promotion: Promotion, region_id: string): Promise<boolean> {}//Validate promotion is enable or disableisDisabled(promotion: Promotion): boolean {}//Validate promotion with setting conditionasync isValidForCard(cart: Cart, promotion: Promotion): Promise<boolean> {}//Remove all adjustment created by promotion of cartasync resetAdjustmentOfPromotionUsed(cart: Cart) {}//Apply promotion for cartasync applyPromotions(cart: Cart, promotions: Promotion[]) {}}