0

I would like to get the current product info and display to "app/design/frontend/Myproject/Myname/Magento_Catalog/layout/catalog_product_view.xml" by using getProduct() funciton of calss - \Magento\Catalog\Block\Product\View. However, it just showed "Magento Fatal Error: Call to member function getSku()"

app/code/Myproject/Myname/Block/Popup.php

<?php

 namespace Myproject\Myname\Block;

 class Popup extends \Magento\Catalog\Block\Product\View {

/**
 * @var ProductRepositoryInterface
 */
protected $productRepository;


public function getProduct123() {
        return $this->getProduct();
}
}

app/code/Myproject/Myname/view/frontend/templates/popup.phtml

<?php

/**
* @var \MyProject\Myname\Block\Popup $block
*/

$productName = $block->getProduct123()->getSku(); ?>

<div class="block-title">
  <strong>hihihi<?php  echo $productName; ?></strong>
</div>

app/design/frontend/Myproject/Myname/Magento_Catalog/layout/catalog_product_view.xml

 <referenceContainer name="content">
        <block class="Magento\Catalog\Block\Product\View" name="pop_up_2" 
          template="MyProject_Myname::popup.phtml"/>
 </referenceContainer>

3 Answers 3

0

Create layout catalog_product_view.xml :

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.main">
            <block class="Vendor\Module\Block\View\Test" name="test"  template="Vendor_Module::account/test.phtml"/>
        </referenceContainer>
    </body>
</page>

Create block and extend it with view like below :

<?php
declare(strict_types=1);

namespace Vendor\Module\Block\View;

use Magento\Catalog\Block\Product\View;
use Magento\Catalog\Api\ProductRepositoryInterface;

class Test extends View
{
    /**
     * @param \Magento\Catalog\Block\Product\Context $context
     * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
     * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
     * @param \Magento\Framework\Stdlib\StringUtils $string
     * @param \Magento\Catalog\Helper\Product $productHelper
     * @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig
     * @param \Magento\Framework\Locale\FormatInterface $localeFormat
     * @param \Magento\Customer\Model\Session $customerSession
     * @param ProductRepositoryInterface $productRepository
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
     * @param array $data
     */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Url\EncoderInterface $urlEncoder,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Framework\Stdlib\StringUtils $string,
        \Magento\Catalog\Helper\Product $productHelper,
        \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
        \Magento\Framework\Locale\FormatInterface $localeFormat,
        \Magento\Customer\Model\Session $customerSession,
        ProductRepositoryInterface $productRepository,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        array $data = []
    )
    {
        parent::__construct($context, $urlEncoder, $jsonEncoder, $string, $productHelper, $productTypeConfig, $localeFormat, $customerSession, $productRepository, $priceCurrency, $data);
    }

    /**
     * @return array
     */
    public function getProductData()
    {
        $product = $this->getProduct();
        if($product) {
            return $product;
        }
        return [];
    }
}

And call this function in phtml using $block like below. create test.phtml :

<?php
/** @var Vendor\Module\Block\View\Test $block */
$productData = $block->getProductData();
$productSku = $productData->getSku();
1
  • After extend \Magento\Catalog\Block\Product\View this block , use registry is not proper way , if you use registry , the main think of view block is lost Commented Nov 11, 2022 at 9:11
1

Please update the code in app/design/frontend/Myproject/Myname/Magento_Catalog/layout/catalog_product_view.xml to consider your custom block in phtml template.

From

 <referenceContainer name="content">
        <block class="Magento\Catalog\Block\Product\View" name="pop_up_2" 
          template="MyProject_Myname::popup.phtml"/>
 </referenceContainer>

to

 <referenceContainer name="content">
        <block class="Myproject\Myname\Block\Popup" name="pop_up_2" 
          template="MyProject_Myname::popup.phtml"/>
 </referenceContainer>
0

Update your app/code/Myproject/Myname/Block/Popup.php with below code

And change name of file from Showproduct.php to Popup.php

<?php

namespace Myproject\Myname\Block;

class Popup extends \Magento\Catalog\Block\Product\View {

    public $registry;

    public function __construct(\Magento\Framework\Registry $registry)
    {
        $this->registry = $registry;
    }

    public function getProduct123() {
        $product = $this->registry->registry('current_product');
        return $product;
    }
}

And replace the code with below code for file app/design/frontend/Myproject/Myname/Magento_Catalog/layout/catalog_product_view.xml

 <referenceContainer name="content">
        <block class="Myproject\Myname\Block\Popup" name="pop_up_2" 
          template="MyProject_Myname::popup.phtml"/>
 </referenceContainer>

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.