VirtoCommerce / vc-module-cart

Shopping cart management module
12 stars 15 forks source link

Adding coupon code to cart doesn't work #125

Open azapotoczny-forte opened 1 year ago

azapotoczny-forte commented 1 year ago

When adding the coupon to the shopping cart using CartModuleController.AddCartCoupon, the coupon code is not saved in the database.

I checked the code and it looks like ShoppingCartBuilder sets the property value ShoppingCart.Coupon. However, the conversion to the entity loses that value in the method ShoppingCart.FromModel:

if (model.Coupon != null)
{
   // The coupon code is stored in the Coupons collection
    Coupons = new ObservableCollection<CouponEntity>(new[] { new CouponEntity { Code = model.Coupon } });
}

if (model.Coupons != null)
{
   // The coupon code save above is erased by the line below
    Coupons = new ObservableCollection<CouponEntity>(model.Coupons.Select(x => new CouponEntity { Code = x }));
}
Dan-BV commented 1 year ago

Hello, @azapotoczny-forte ! Thank you for your appeal. The above code is legacy code. To implement the functionality you need, we can recommend two ways:

  1. Using the API, in order to apply coupons, you need to implement saving the cart manually.
  2. Use the XAPI https://github.com/VirtoCommerce/vc-module-experience-api/blob/dev/src/XPurchase/VirtoCommerce.XPurchase/Commands/AddCouponCommandHandler.cs#L16. It contains business operations and covers your scenario and is already integrated with the B2B Theme.

Cupon

azapotoczny-forte commented 1 year ago

Hi @Dan-BV, when it comes to the first solution, do you mean just adding a coupon code to the cart and saving the whole cart object, right?