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
- 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
- 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;
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
- ProductModel productModel = productService.getProductForCode(“M35364”);
ProductModel productModel = productService.getProductForCode(“M35364”);
We can use this productModel object to get all the variants as below
- 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.
Can you please share infi abt classificatim system and its attributes plz