I need to get the value of a custom attribute on the product page, as I want to display different sizechart per attribute. For this reason, I have created the following .phtml to get the value of the attribute:

File: /app/design/frontend/Mgs/claue/Magento_Catalog/templates/product/view/sizechart.phtml

$_product = $block->getProduct();
$attribute = $_product->getResource()->getAttribute('sizechart2');

And I have called this file from:

File: /app/design/frontend/Mgs/claue/Magento_Catalog/layout/catalog_product_view.xml
<referenceContainer name="product.info.main">
            <block class="Magento\Framework\View\Element\Template" name="custom.text" template="Magento_Catalog::product/view/sizechart.phtml" after="product.info.price"/>

But when I go to product page show the following error:

Fatal error: Uncaught Error: Call to a member function getResource() on null

What is the problem? :(



Your template is getting value from the Block class as you've mentioned in the xml.

Magento\Framework\View\Element\Template does not have any product instance.

In your xml you can simply modify your xml by the below

File: /app/design/frontend/Mgs/claue/Magento_Catalog/layout/catalog_product_view.xml
<referenceContainer name="product.info.main">
            <block class="Magento\Catalog\Block\Product\View" name="custom.text" template="Magento_Catalog::product/view/sizechart.phtml" after="product.info.price"/>

Note: I have changed from Magento\Framework\View\Element\Template to Magento\Catalog\Block\Product\View

  • Thanksss!!! I did not know that it took the value of the class of the block. Thanks again!! – IvannVerano Sep 21 '20 at 13:19

Create attribute1.phtml file and call all your attributes inside that file.


Add your attribute code.

<?php echo $this->helper('Magento\Catalog\Helper\Output')->productAttribute($block->getProduct(), $block->getProduct()->getAttributeCode(), 'attribute_code'); ?>

Inside your xml file,


<referenceBlock name="product.info.main">
    <block class="Magento\Catalog\Block\Product\View" name="attribute1" template="product/view/attribute1.phtml" />

after that move your attribute1 block where ever you want.

for example,

<move element="attribute1" destination="product.info.main" before="product.info.price" />


  • Thanks for your solution! – IvannVerano Sep 21 '20 at 13:20
  • 1
    Please mark as correct if its working for you. – Mayur Sisodiya Sep 22 '20 at 4:03
  • I try Rinto George solution because is easier, sorry. – IvannVerano Sep 22 '20 at 9:22
  • Ok no worries @IvannVerano – Mayur Sisodiya Sep 22 '20 at 9:22
  • Thanks again!!!! – IvannVerano Sep 22 '20 at 9:55

try this it will help you.

$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct(); 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$productTypeInstance = $product->getTypeInstance();
if ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) 
    $usedProducts = $productTypeInstance->getUsedProducts($product);

    $data = explode(',',$_product->getData('your_attribute'));
    if(count($data) > 1)
        foreach($data as $key => $value):                    
            $attr = $_product->getResource()->getAttribute('your_attribute');
            if ($attr->usesSource()): 
                $option_value = $attr->getSource()->getOptionText($value);
                echo $option_value;

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.