lets say in my catalog_product_view.xml I have this additional Block:

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.demo" template="product/view/warranty.phtml" after="product.info.price">

in warranty.phtml I use the following:

$_product = $block->getProduct();
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);

In the block class Description.php:

 public function getProduct()
    if (!$this->_product) {
        $this->_product = $this->_coreRegistry->registry('product');
    return $this->_product;

We use getProduct() to get product object from registry.

Info: This product object was earlier first obtained and then put in the registry, in class Magento\Catalog\Helper\Product:

    try {
             //productRepository is Catalog\Api\ProductRepositoryInterface
            //getById() returns \Magento\Catalog\Api\Data\ProductInterface
        $product = $this->productRepository->getById($productId, false, $this->_storeManager->getStore()->getId());
    } catch (NoSuchEntityException $e) {
        return false;
         //put obtained product object into registry
    $this->_coreRegistry->register('product', $product);

Method getById() in Catalog\Api\ProductRepositoryInterface:

     * @param int $productId
 * @param bool $editMode
 * @param int|null $storeId
 * @param bool $forceReload
 * @return \Magento\Catalog\Api\Data\ProductInterface
 * @throws \Magento\Framework\Exception\NoSuchEntityException
public function getById($productId, $editMode = false, $storeId = null, $forceReload = false);

Soo: our product-object in warranty.phtml $_product = $block->getProduct(); should be \Magento\Catalog\Api\Data\ProductInterface right ?

Also: In warranty.phtml we call $_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product); , but there are no such methods defined in \Magento\Catalog\Api\Data\ProductInterface. Where are these methods declared/implemented ??


Your product object is actually an instance of \Magento\Catalog\Model\Product that implements the product interface.
And this class contains the methods you need.

  • Links are not valid please update your answer. Thanks – Mubo Feb 6 '19 at 12:00

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.