How do I retrieve a list of products?

Follow

Say you have a carousel that loops through a series of products on your site's homepage. How do you go about retrieving the products to provide data to your sublayout?

First, create a Sitecore item whose template inherits from the ProductSearchGroup template (/sitecore/templates/Ecommerce/Product Categories/Product Search Group). This gives you access to the "Product Page" tab where you can define a product query or manually select the products you want to display. This is the same tab used on Active Commerce's Product Category and Homepage Featured Products.

Next, you'll want to use the IProductBrowser as shown below:

//Get your Sitecore item (which is based on the ProductSearchGroup template) that will define the products - either manually selected or by results of a query.

var carousel = Sitecore.Context.Database.GetItem(ID.Parse(carouselId)); if (carousel == null) return new List<ProductSlide>();

//Note that the SimpleProductList is a basic implementation of the IProductList interface which the IProductBrowser requires.

var productBrowser = Sitecore.Ecommerce.Context.Entity.Resolve<IProductBrowser<ActiveCommerce.Products.Browsing.SimpleProductList, ActiveCommerce.Products.Product>>(); var productList = productBrowser.GetProductList(carousel);

//Now you have a collection of products to bind to your markup
var products = productList.Products;
Have more questions? Submit a request

Comments