I have a main category "Computer" with 3 sub categories:
- Laptop
- Storage
- Software
I know how can I get a sub category. But I need best the selling product for each category, for example:
Laptop
- Product Laptop1
- Product Laptop2 (This 2 product is best seller product for laptop category)
Storage
- Product Storage1
- Product Storage2 (This 2 product is best seller product for storage category)
Software
- Product Software1
- Product Software2 (This 2 product is best seller product for software category)
My code to get the category list for the main category is:
public function __construct(
Template\Context $context,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Helper\Category $categoryHelper,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
array $data = array())
{
parent::__construct($context, $layerResolver, $registry, $categoryHelper,$data);
$this->_categoryFactory = $categoryFactory;
$this->_collectionFactory = $collectionFactory;
}
public function getCategoryList()
{
$_category = $this->getCurrentCategory();
$collection = $this->_categoryFactory->create()->getCollection()->addAttributeToSelect('*')
->addAttributeToFilter('is_active', 1)
->setOrder('position', 'ASC')
->addIdFilter($_category->getChildren());
return $collection;
}
Best Answer
Check my code below for more information :
You can also download my extension from Github. https://github.com/hirenrk4/Magento-2-Best-Seller
This way user can get a Category and sub category best the selling product for each category I've created the module following your instructions to Best sellerFilter On category list Page Filter.
app/code/Hiren/BestsellerFilter/composer.json
app/code/Hiren/BestsellerFilter/etc/frontend/di.xml