public async Task <bool> Handle(RemoveGiftCardCommand request, CancellationToken cancellationToken) { if (request.Customer == null) { throw new ArgumentNullException("customer"); } var giftCard = await _giftCardService.GetGiftCardById(request.GiftCardId); if (giftCard == null) { throw new ArgumentNullException("giftCard"); } //get applied coupon codes var existingCouponCodes = request.Customer.ParseAppliedGiftCardCouponCodes(); //clear them await _genericAttributeService.SaveAttribute <string>(request.Customer, SystemCustomerAttributeNames.GiftCardCouponCodes, null); //save again except removed one foreach (string existingCouponCode in existingCouponCodes) { if (!existingCouponCode.Equals(giftCard.GiftCardCouponCode, StringComparison.OrdinalIgnoreCase)) { var result = GiftCardExtensions.ApplyCouponCode(request.Customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.GiftCardCouponCodes), existingCouponCode); await _genericAttributeService.SaveAttribute(request.Customer, SystemCustomerAttributeNames.GiftCardCouponCodes, result); } } return(true); }
public async Task <bool> Handle(ApplyGiftCardCommand request, CancellationToken cancellationToken) { if (request.Customer == null) { throw new ArgumentNullException("customer"); } var result = GiftCardExtensions.ApplyCouponCode(request.Customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.GiftCardCouponCodes), request.GiftCardCouponCode.Trim().ToLower()); //apply new value await _genericAttributeService.SaveAttribute(request.Customer, SystemCustomerAttributeNames.GiftCardCouponCodes, result); return(true); }