2

I have created new phtml page in product/view folder

xml

 <block class="Magento\Catalog\Block\Product\View" name="other.products.category" template="product/view/relatedproduct.phtml" after="require-cookie"></block>

I need to load product listing in same category in product custom phtml page

I tried below code but its not working

$objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
$curentProduct = $objectManager->getCurrentProduct();
echo $currentProduct->getName();

2 Answers 2

0

Add bellow code in your block file.

for example app/code/AR/CustomModule/Block/CustomBlock.php

<?php
namespace AR\CustomModule\Block;
class CustomBlock 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');
    }

    public function getCurrentProduct()
    {       
        return $this->_registry->registry('current_product');
    }   

}
?>

Printing current product data in new phtml file

if ($currentProduct = $block->getCurrentProduct()) {
    echo $currentProduct->getName();

}
2
0

You could try below code to get current product in custom phtml.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();    
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); 
$name  = $product->getName();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.