How do You Get Current Categories Product Collection in Magento

This is an option for a user to purchase multiple items in online shopping through Related products, most view and best selling. These options are available on the product view page in Magento. It provides customers to add products to the shopping cart just with a single click.


 < $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $curent_category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
   $categoryId = $curent_category->getId();
    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
    $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
    $productFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $category = $categoryFactory->create()->load($categoryId);
    $collection = $productFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoryFilter($category);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)->load();
   ?>

To create the related products, most view, and best selling on the product view page please follow the above code.

You can use the code in Magento development. Please share your review in the comment section.

We Serve clients globally in diverse industries

Stay Upto Date With Our Newsletter.