1

On the product page I created a custom module to override the form.phtml file. I also added the option to include a new file called customFile.phtml when the user clicks on a button that is positioned under the product description.

In this customFile.phtml file I am trying to retrieve the current product data but it doesn't work. I also injected the class to take on the function of the current product but it doesn't work and gives me "Fatal error".

This is the xml of my customFile.phtml file which is called on click with Ajax:

<?xml version="1.0"?>

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<container name="product.info.form.content" as="product_info_form_content">
    <update handle="catalog_product_opengraph" />
    <update handle="page_calendar"/>
    <block class="Magento\Framework\View\Element\Template" name="customFile" template="Catalog_Product::customFile.phtml"/>
</container>
</layout>

This instead is the phtml code of the file:

<?php
$_product = $block->getLayout()->createBlock('Catalog\Product\Block\ProductInfo')->getCurrentProduct();

echo $_product->getName();
?>

This is the class where I call the current product in my custom file:

    <?php
    namespace Catalog\Product\Block;

    use Magento\Framework\View\Element\Template;

    class ProductInfo extends 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');
}    

}
?>

I FOUND THE MISTAKE: Calling the customFile.phtml file with ajax I created his xml file for the link and it's called customfile_index_edit.xml and consequently the current link is www.magento2.com/customfile/index/edit. Maybe that's why Magento doesn't give me back the information of the current product.

What could I do at this point?

Thank you

3
  • check this link --- mageplaza.com/devdocs/… Jul 16 '20 at 20:31
  • Man, thanks but i tried this link but not work :(
    – Jackom
    Jul 16 '20 at 20:43
  • You can change block class for phtml from Magento\Framework\View\Element\Template to Magento\Catalog\Block\Product\View and get product like: $block->getProduct();
    – Pawan
    Jul 17 '20 at 7:36
0

This should do it:

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<container name="product.info.form.content" as="product_info_form_content">
    <update handle="catalog_product_opengraph" />
    <update handle="page_calendar"/>
    <block class="Catalog\Product\Block" name="customFile" template="Catalog_Product::customFile.phtml"/>
</container>
</layout>

Is Catalog\Product\Block in your module or did you write directly in the core modules of Magento?

2
  • Man, i tried but not work! Is Catalog\Product\Block in my module. This xml is dedicated to my custom file that i added to product view on click with ajax
    – Jackom
    Jul 17 '20 at 7:57
  • I found my issue man, please read my update answer! :)
    – Jackom
    Jul 17 '20 at 14:53

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.