1

I want to retrieve the products under a certain category. Now what I'm trying to do is pass the Category Name and retrieve the actual Category ID. So in return I can use this category id to retrieve the products under this category. My previous code didn't work anymore although it worked previously but now it's not working anymore

Below is my current code

public function getProductsCategoryByName() 
{
    $categoryName = "Extra category";
    $categoryCollection = $this->_categoryFactory->create()->getCollection()->addAttributeToFilter('name',$categoryName)->setPageSize(1);

    if ($categoryCollection->getSize()) {

        $categoryId = $categoryCollection->getFirstItem()->getId();


        $category = $this->_categoryFactory->create()->load($categoryId);

        $product_collection = $this->_productCollectionFactory->create()
            ->addAttributeToSelect('*')
            ->addCategoryFilter($category)
            ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) 
            ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

        return $product_collection;
    }

    return null;        
}
5
  • can you check if you are getting category id from your code? If you are getting category id, then problem would be in you product collection code Feb 25, 2020 at 7:52
  • the IF condition for $categoryCollection->getSize() returns 0 so basically it's not getting the category by the name Feb 25, 2020 at 8:11
  • Print Collection query $categoryCollection->getSelect()->__toString() and run this query to database and check what result comming. I think, you have different for name for store view level
    – Amit Bera
    Feb 25, 2020 at 9:04
  • @AmitBera the $categoryCollection is empty that's why I can't check it Feb 25, 2020 at 9:11
  • let check my post
    – Msquare
    Feb 25, 2020 at 9:18

2 Answers 2

0

But do not use object manager directly.

          $name = 'women';

          $bootstraps = Bootstrap::create(BP, $_SERVER);
          $objectManager = $bootstraps->getObjectManager();
          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $_categoryFactory = $objectManager->get('Magento\Catalog\Model\CategoryFactory');

            $collection = $_categoryFactory->create()->getCollection()->addAttributeToFilter('name',$name);
            if ($collection->getSize()) {
              print_r($collection->getFirstItem()->getId().'<br/>');


            }
0

try this code here you pass category name this function return product collection

    protected $_pageFactory;
    protected $collecionFactory;
    protected $categoryFactory;
    protected $_productCollectionFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
    ) {
        $this->_pageFactory = $pageFactory;
        $this->collecionFactory = $collecionFactory;
        $this->categoryFactory = $categoryFactory;
        $this->_productCollectionFactory = $productCollectionFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        echo "<pre>";
        $data = $this->collecionFactory->create();
        $data->addAttributeToSelect('*');
        //->getCollection()->addAttributeToFilter('name',$categoryName)
        //print_r($data->getData());
        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $category = $_objectManager->create('Magento\Catalog\Model\Category')
            ->load(4);
        //echo $category->getName();
        $category_name = 'Bags'; \\ your category name
        $prd_coll = $this->getProductsCategoryByName($category_name);
        print_r($prd_coll->getData());
        exit();
    }
    public function getProductsCategoryByName($category_name)
    {
        $collection = '';
        $category_collaction = $this->collecionFactory->create();
        foreach ($category_collaction as $key => $value) {
            //print_r($value->getId());
            $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $category = $_objectManager->create('Magento\Catalog\Model\Category')->load($value->getId());
            if ($category->getName() == $category_name) {

                $category = $this->categoryFactory->create()->load($value->getId());

                $collection = $this->_productCollectionFactory->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);
                break;
            }
        }
        return $collection;
    }
}

enter image description here

5
  • This didn't work also.. It returned an empty string Feb 25, 2020 at 10:00
  • @MadzQuestioning it return empty because category name not have any product
    – Msquare
    Feb 25, 2020 at 10:21
  • please put right category name
    – Msquare
    Feb 25, 2020 at 10:22
  • I already did and double check the category. It has 4 products and all products are visible and enabled and has stock assigned to it Feb 25, 2020 at 10:26
  • please check your category name because code is running perfectly . i put screenshot
    – Msquare
    Feb 25, 2020 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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