0

I get product like this way, but how can I get sub category now and ids and label?

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $categoryFactory = $objectManager::getInstance()->get(\Magento\Catalog\Model\CategoryFactory::class);
        $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
        $resourceConection = $objectManager->get('\Magento\Framework\App\ResourceConnection');
        $eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
        $jsonHelper = $objectManager->get('\Magento\Framework\Json\Helper\Data');

        $category = $categoryFactory->create();

        $products = $category->load($categoryId)->getProductCollection();


        $products->addAttributeToSelect('*');
        $products->addAttributeToFilter('product_type_custom', array('notnull' => true))->getSelect()->group('product_type_custom');

    $_optionText =  "";

    $firstArray = array();
        //echo $products->getSelect();
        foreach($products as $product)
{
}
4
  • i think you are going wrong way!! Jan 28 '19 at 8:53
  • actually i need product of specific cateogeory i load collection like that and got product collecion successfully now i want product category
    – imtiaz
    Jan 28 '19 at 9:06
  • where do you want to use it ? for a block ?
    – fmsthird
    Jan 28 '19 at 9:12
  • actually i am createing search so i need 4 attribute i got others but now i need cateogyr attribute alos
    – imtiaz
    Jan 28 '19 at 9:14
0

Create a Block file : app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{ 
    protected $_categoryCollectionFactory;

    protected $_categoryHelper;

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

    public function getCategoryCollection($isActive = true, $level = false, $sortBy = false, $pageSize = false) {
        $collection = $this->_categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*');

        // select only active categories
        if ($isActive) {
            $collection->addIsActiveFilter();
        }

        // select categories of certain level
        if ($level) {
            $collection->addLevelFilter($level);
        }

        // sort categories by some value
        if ($sortBy) {
            $collection->addOrderField($sortBy);
        }

        // set pagination
        if ($pageSize) {
            $collection->setPageSize($pageSize); 
        } 

        return $collection;
    }

    public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true) {
        return $this->_categoryHelper->getStoreCategories($sorted = false, $asCollection = false, $toLoad = true);
    }
}

Now, we can use the functions in our view (.phtml) file as follows.

// get the list of all categories
$categories = $block->getCategoryCollection(); 
foreach ($categories as $category) {
    echo $category->getId() . '<br />';
    echo $category->getName() . '<br />';
}

// get categories sorted by category name
$categories = $block->getCategoryCollection(true, false, 'name', false);
foreach ($categories as $category) { 
    echo $category->getId() . '<br />';
    echo $category->getName() . '<br />';
}

// get current store’s categories
$categories = $block->getStoreCategories();
foreach ($categories as $category) { 
    echo $category->getId() . '<br />';
    echo $category->getName() . '<br />';
}

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.