3

anyone knows how to load from my custom block.php all the products of a specific category,

I would like to get them by the name of the category and not by the id.

I tried to change the standard code used by magento \Magento\CatalogWidget\Block\Product\ProductsList

here is the code:

public function createCollection()
{
    $collection = $this->productCollectionFactory->create();
    $collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());

    $collection = $this->_addProductAttributesAndPrices($collection)
        ->addStoreFilter()
        ->setPageSize($this->getPageSize())
        ->setCurPage($this->getRequest()->getParam(self::PAGE_VAR_NAME, 1));

    $conditions = $this->getConditions();
    $conditions->collectValidatedAttributes($collection);
    $this->sqlBuilder->attachConditionToCollection($collection, $conditions);

    return $collection;
}

but this code get all categories products.

4
  • welcome ,MSe, You have you tried anyting?
    – Amit Bera
    Jun 21 '16 at 9:30
  • @AmitBera I edit my question with my test Jun 21 '16 at 9:37
  • what you mean by I would like to get them by the name of the category and not by the id. Are you get category by category name?
    – Amit Bera
    Jun 21 '16 at 9:42
  • Yes I want to load the category not by id, but by the name. Jun 21 '16 at 9:44
1

You need inject factory class \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory to __construct() function in order get category by name

  <?php
     namespace [VendorName]\[ModuleName]\Block;
         /**
          * @var Category
          */
     class Categories extends \Magento\Framework\View\Element\Template     
         protected $_categoryCollectionFactory;

        public function __construct(
             \Magento\Framework\View\Element\Template\Context $context,
             \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
 $categoryCollectionFactory,
             array $data = []
         ) {
            $this->categoryCollectionFactory = $categoryCollectionFactory;
             parent::__construct($context, $data);
         }

         public function getCategoriesbyName($name)
         {
            $categoryCollection = $this->categoryCollectionFactory->create();
             $categories = $categoryCollection->addAttributeToFilter('name', $name);
             return $categories;
         }
      }
1
0
<?php

namespace [VendorName]\[ModuleName]\Block;

class [ClassName] extends \Magento\Framework\View\Element\Template{


    /**
     * @var \Magento\Catalog\Model\CategoryFactory
     */
    protected $categoryFactory;



    public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->categoryFactory = $categoryFactory;
    }


    public function getCategoryName()
    {
        $categoryName =  "mobiles";
        $category = $this->categoryFactory->create()->addAttributeToFilter('name', $categoryName);
        return $category;
    }


    public function getProductCollection()
    {
        $collection = $this->getCategoryName()->getProductCollection();
        return $collection;
    }

}

feel free to ask if any queries.

0

Use the following code to get all category products by category name.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$name = 'Gear'; //category name
$categoryobj = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$categoryproductcollection = $categoryobj->create()->load($name, 'name')->getProductCollection()->addAttributeToSelect('*');
1

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.