CommerceSitecore

Extending Sitecore Experience Accelerator(SXA) Repositories

SXA modules(talking about features mainly) follow the repository pattern. Each api controller depends on a repository to call commerce roles. For example, we have a module Sitecore.Commerce.XA.Feature.Cart which takes care of cart and checkout related operations. There is a CartController which talks to AddToCartRepository for adding a product to the cart.

RepositoryPattern

 

This repository exposes three methods

While working on a project recently, we had to implement inventory checks for certain products while adding them to the cart. AddLineItmesToCart method doesn’t check for inventory so we decided to overwrite the functionality for this method.

In order to extend AddToCartRepository, we created a feature module in our project calling it MyProject.Commerce.XA.Feature.Cart. Since this module extends the Sitecore.Commerce.XA.Feature.Cart module, we added required references to our module. We only wanted to extend the functionality of AddToCartRepository so we created the ExtendedAddToCartRepository class which inherits from AddToCartRepository.

And rest was simple C# code 🙂

InventoryManager was required to make the inventory checks.

And the last step to replace the dependency injection

 

There you are!

RepositoryPattern1

 

Leave a Reply

Your email address will not be published. Required fields are marked *