0

Is it possible to filter products by category in Magento 2 under Catalog?

So when I go in the admin to catalog, can I filter the product list according to the category / categories the products are assigned to?

closed as unclear what you're asking by liyakat, Siarhey Uchukhlebau, sv3n, Keyul Shah, Prince Patel Aug 31 '17 at 7:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • By "under catalog", do you mean you need category filter in admin product grid? – Piyush Aug 25 '17 at 5:06
  • Yes, exactly so I can choose only products under specific category. – Kaki Sami Aug 25 '17 at 5:07
  • For this you have to add category column in the product grid programmatically. – Piyush Aug 25 '17 at 5:08
  • How would I do that? Custom theme for admin? – Kaki Sami Aug 25 '17 at 5:09
  • theme won't do that , you have to write your own code – Piyush Aug 25 '17 at 5:11
1

You can simply call block and get product collection using below code :

Block File :

<?php
namespace Company\Categories\Block;

class Categoryproduct extends \Magento\Framework\View\Element\Template
{
     protected $categoryFactory;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,        
        \Magento\Catalog\Model\CategoryFactory $categoryFactory
    ) {
        $this->categoryFactory = $categoryFactory;
        parent::__construct($context);
    }

    public function getCategoryProduct($categoryId)
    {
        $category = $this->categoryFactory->create()->load($categoryId)->getProductCollection()->addAttributeToSelect('*');
        return $category;
    }
}

phtml File :

$cat_Id = 10;
$getProudctcollection = $block->getCategoryProduct($cat_Id);

<ul class="category-products">  
                        <?php 
                                foreach ($getProudctcollection as $product) : ?>
                                    <li class="level0-child">
                                        <a href="<?php echo $product->getProductUrl(); ?>">
                                            <?php echo $product->getName();?>
                                        </a>
                                    </li> 
                        <?php   endforeach;?>
                    </ul>
  • Happy to help you sir ... @Kaki Sami ... !! Happy Coding :) – Rohan Hapani Aug 27 '17 at 16:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.