- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2019
01:46 AM
03-27-2019
01:46 AM
How to get Current category product collection?
I want to get product collection for current category while I am on a list page.
I want to get current product collection for current category while ajax call (i want the same collection in the controller which return some data based on collection) so is there any way that I can get the same product collection which was used in list.phtml
Does anyone have any idea how can I achieve my above requirement?
Thanks
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2019
02:24 AM
03-27-2019
02:24 AM
Re: How to get Current category product collection?
You can use this blog link: http://blog.chapagain.com.np/magento-2-get-all-products-of-a-category/
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2019
11:42 AM
03-29-2019
11:42 AM
Re: How to get Current category product collection?
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();$categoryId = 47; // YOUR CURRENT CATEGORY ID$category = $categoryFactory->create()->load($categoryId);$categoryProducts = $category->getProductCollection() ->addAttributeToSelect('*'); foreach ($categoryProducts as $product) { $imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage(); ?> <div class="product-container"> <a href="<?= $product->getProductUrl(); ?>"> <div class="new-arrivals-image"><img src="<?= $imageUrl;?>"></div> <div class="product-name"><span class="name"><?= $product->getName(); ?></span></div> </a> <div class="price"><span class="pt"><?= $product->getPrice(); ?></span></div> </div> <?php} ?>
YOU CAN CALL YOUR CURRENT ID IN $categoryId VARIABLE. HOPE IT WILL WORK FOR YOU.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2020
02:05 AM
03-19-2020
02:05 AM
Re: How to get Current category product collection?
thank vinaypal90, work for me, but how can I get product by category array, example $cat = [2,3,4], thank a lot