Skip to main content
added 300 characters in body
Source Link
Shoaib Munir
  • 9.6k
  • 10
  • 53
  • 108

You can get category collection from current product using this:

$product = $this->registry->registry('current_product');
$categoryCollection = clone $product->getCategoryCollection();

If you are using Magento 2.3+ then registry wont work for you, in that case you need to get current product id by this:

$this->request->getParam('product_id');

where $this->request is the instance of \Magento\Framework\App\RequestInterface. You can load your product by product_id

where $this->registry is a instance of Magento\Framework\Registry

If you want tree level structure for breadcrumb, then you need to understand that product might be attached to multiple categories. So the best way to get proper categories tree is by getting product from highest level, like this:

    $product = $this->registry->registry('current_product');
    $categoryCollection = clone $product->getCategoryCollection();
    $categoryCollection->clear();
    $categoryCollection->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $this->_storeManager->getStore()->getRootCategoryId() . "/%"));
    $categoryCollection->setPageSize(1);
    $currentTree = $categoryCollection->getFirstItem()->getParentCategories();
    foreach ($currentTree as $category) {
        echo $category->getName()."<br>";
        echo $category->getUrl()."<br>";
    }

Hope this will help you

You can get category collection from current product using this:

$product = $this->registry->registry('current_product');
$categoryCollection = clone $product->getCategoryCollection();

where $this->registry is a instance of Magento\Framework\Registry

If you want tree level structure for breadcrumb, then you need to understand that product might be attached to multiple categories. So the best way to get proper categories tree is by getting product from highest level, like this:

    $product = $this->registry->registry('current_product');
    $categoryCollection = clone $product->getCategoryCollection();
    $categoryCollection->clear();
    $categoryCollection->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $this->_storeManager->getStore()->getRootCategoryId() . "/%"));
    $categoryCollection->setPageSize(1);
    $currentTree = $categoryCollection->getFirstItem()->getParentCategories();
    foreach ($currentTree as $category) {
        echo $category->getName()."<br>";
        echo $category->getUrl()."<br>";
    }

Hope this will help you

You can get category collection from current product using this:

$product = $this->registry->registry('current_product');
$categoryCollection = clone $product->getCategoryCollection();

If you are using Magento 2.3+ then registry wont work for you, in that case you need to get current product id by this:

$this->request->getParam('product_id');

where $this->request is the instance of \Magento\Framework\App\RequestInterface. You can load your product by product_id

where $this->registry is a instance of Magento\Framework\Registry

If you want tree level structure for breadcrumb, then you need to understand that product might be attached to multiple categories. So the best way to get proper categories tree is by getting product from highest level, like this:

    $product = $this->registry->registry('current_product');
    $categoryCollection = clone $product->getCategoryCollection();
    $categoryCollection->clear();
    $categoryCollection->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $this->_storeManager->getStore()->getRootCategoryId() . "/%"));
    $categoryCollection->setPageSize(1);
    $currentTree = $categoryCollection->getFirstItem()->getParentCategories();
    foreach ($currentTree as $category) {
        echo $category->getName()."<br>";
        echo $category->getUrl()."<br>";
    }

Hope this will help you

Source Link
Shoaib Munir
  • 9.6k
  • 10
  • 53
  • 108

You can get category collection from current product using this:

$product = $this->registry->registry('current_product');
$categoryCollection = clone $product->getCategoryCollection();

where $this->registry is a instance of Magento\Framework\Registry

If you want tree level structure for breadcrumb, then you need to understand that product might be attached to multiple categories. So the best way to get proper categories tree is by getting product from highest level, like this:

    $product = $this->registry->registry('current_product');
    $categoryCollection = clone $product->getCategoryCollection();
    $categoryCollection->clear();
    $categoryCollection->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $this->_storeManager->getStore()->getRootCategoryId() . "/%"));
    $categoryCollection->setPageSize(1);
    $currentTree = $categoryCollection->getFirstItem()->getParentCategories();
    foreach ($currentTree as $category) {
        echo $category->getName()."<br>";
        echo $category->getUrl()."<br>";
    }

Hope this will help you