2

I want to give one link to browse the root category. root category should load all products and with out tool bar.i found some articles for m1. any one have idea?

  • You should accept answer to help other developers to find there solutions – Prince Patel Nov 25 '17 at 6:56
1

Inject Magento\Catalog\Model\CategoryFactory class to get product collection.

protected $categoryFactory;

public function __construct(
    ...    
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
    ...
) {
    ...
    $this->categoryFactory = $categoryFactory;
    ...
}

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

    return $category;
}

Now call getCategoryProduct() in phtml file,

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

foreach ($getProudctcollection as $product) {
    echo $product->getName(); //Get Product Name
    echo $product->getProductUrl(); //Get Product URL
}

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.