How to get all the variants of a base product in Hybris?

Requirement :

Sometimes, in our requirement, we need to fetch all the variants of a specific base product to display in the UI or to perform some logic on variants etc

Let us see how we can do that in this article

There are 2 ways to achieve this

1) Directly using query, we can get all the variants for a specific base product

2) Load the base product first and then call getVariants() method on the base product object to load all the variants of that base product.

Note: We should know that , relation between Base Product and its variants is One to Many


Let’s do the implementation in both the ways


1.Using Query


We can write below query to achieve it

  1.  
  2. Select {vp.pk} from {VariantProduct as vp join product as p on {vp.baseproduct}={p.pk}} where {p.code}= ‘M35364’
 
Select {vp.pk} from {VariantProduct as vp join product as p on {vp.baseproduct}={p.pk}} where {p.code}= ‘M35364’ 


Above flexible search query will get all the variants for the base product ‘M35364’

We can call this query in DAO layer as below

  1. String query = “Select {vp.pk} from {VariantProduct as vp join product as p on {vp.baseproduct}={p.pk}}
  2.                      where {p.code}=?productCode”;
  3.  
  4. final Map<String, Object> params = new HashMap<String, Object>();
  5.  params.put(“productCode”, “M35364”);
  6.  
  7.         final FlexibleSearchQuery searchQuery = new FlexibleSearchQuery(query);
  8.         searchQuery.addQueryParameters(params);
  9.         searchQuery.setResultClassList(Collections.singletonList(VariantProductModel.class));
  10.         final SearchResult searchResult = getFlexibleSearchService().search(searchQuery);
  11.         List<VariantProductModel> variants = searchResult.getResult();
  12.  return variants;
String query = “Select {vp.pk} from {VariantProduct as vp join product as p on {vp.baseproduct}={p.pk}} 
                     where {p.code}=?productCode”;

final Map<String, Object> params = new HashMap<String, Object>();
 params.put(“productCode”, “M35364”);

		final FlexibleSearchQuery searchQuery = new FlexibleSearchQuery(query);
		searchQuery.addQueryParameters(params);
		searchQuery.setResultClassList(Collections.singletonList(VariantProductModel.class));
		final SearchResult searchResult = getFlexibleSearchService().search(searchQuery);
		List<VariantProductModel> variants = searchResult.getResult();
 return variants;


2.Using getVariants() method


In this way, we can load ProductModel first and then call getVariants() method

We have OOTB method to load the ProductModel based on code as below

  1. ProductModel productModel = productService.getProductForCode(“M35364”);
ProductModel productModel = productService.getProductForCode(“M35364”);


We can use this productModel object to get all the variants as below

  1. Collection<VariantProductModel> variants = productModel.getVariants();
Collection<VariantProductModel> variants = productModel.getVariants();


Note:
I would prefer first approach as best choice in terms of performance, because we are loading only required variants from DB but in second approach, we are loading base product first and then loading all variants.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on