Start a new topic

How do i work with carts in my own module?

We've implemented our own restapi endpoint, what's the best way to work with virtos cart functionality within our module?


I would like to retrieve the cart, apply taxes and eventually promos. ie. a complete cart object.

In the storefront demo site i have found some code for applying taxes (ShoppingCart.ApplyTaxRates etc) and other stuff.

- Do i need to copy this kind of functionality from storefront demo?

- Is it best practice to use the commercecoreapi to access the "EvaluateTaxes" method from a module? If no, how do i access it?

I have found a checkout module on GitHub with some of this functionality but this is not available on nuget and looks a little unfinished.

CardBuilder.cs: (misspelled?)

//todo:_cart.ApplyTaxeRates(taxRates);

Maybe a work in progress?

Best Regards / Johan

1 Comment

https://github.com/VirtoCommerce/vc-module-checkout it was unsuccessful attempt to putting together a shopping cart service and js checkout (now it placed in https://github.com/VirtoCommerce/vc-module-javascript-shoppingcart). But in the end we realized that this implementation would duplicate a lot of functionality with storefront and it will be hard for support in the future. And we decide to leave tax and promo evaluation on client (storefront) side, and leave for shopping cart API only basic cart operations such as CRUD and some helper methods.(https://github.com/VirtoCommerce/vc-module-cart/tree/dev). This ultimately reduces the responsibility of the cart module and makes it more flexible for others use cases usage ( for example: which lists, multi shopping cart etc).


But for you special case you can implement all tax and promo evaluation in your own module without any limitation, as you wish. I would recommend to use new Cart module version which will be released soon. Here will be wide possibility for extension (Domain and persistent model).https://github.com/VirtoCommerce/vc-module-cart/tree/dev


For evaluate taxes and promotions in your own module  need use follow code. 

For tax evaluation

   

var taxEvalContext = new  TaxEvaluationContext  { ..... }
var store = storeService.GetStoreById("my-store");
 var activeTaxProvider = store.TaxProviders.FirstOrDefault(x => x.IsActive);
       if (activeTaxProvider != null)
       {
            taxEvalContext.Store = store;
            var taxRates = activeTaxProvider.CalculateRates(taxEvalContext ));
        }

   


For promotion evaluation

 

IMarketingPromoEvaluator promoEvaluator;  //inject or resolve from IoC container
var promoEvalContext = new PromotionEvaluationContext { ..... }
var rewards = promoEvaluator.EvaluatePromotion(promoEvalContext )
.... 
//Apply rewards as it is done in storefront 
....

 

All of above abstractions defined in VirtoCommerce.Domain nuget packages in VirtoCommerce,Core,Module

And you do not need any extra dependencies.


Login or Signup to post a comment