I have a product bag in home->products->men->handbag category path .I want hangbag category url in product detail page
1 Answer
Please check below code.
<?php
namespace VENDOR\EXTENSION\Block;
class Caturl extends \Magento\Framework\View\Element\Template
{
protected $_registry;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getCurrentCategory()
{
return $this->_registry->registry('current_category');
}
}
?>
And use it with below code in .phtml.
// print for current category
$currentCategory = $block->getCurrentCategory();
echo $currentCategory->getName() . '<br />';
echo $currentCategory->getUrl() . '<br />';
Happy Coding.
-
as per my category path ,echo $currentCategory->getUrl() gives the url of Men .but i need to get the handbag url Commented May 5, 2020 at 6:21
-
Please use this code to get full path. ipfs-sec.stackexchange.cloudflare-ipfs.com/magento/A/question/… Commented May 5, 2020 at 6:33