4

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

<?php 
$_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"/>
</referenceContainer>

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? :(

Thanksss

3 Answers 3

5

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"/>
</referenceContainer>

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

1
  • Thanksss!!! I did not know that it took the value of the class of the block. Thanks again!! Commented Sep 21, 2020 at 13:19
2

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

app/design/frontend/VENDOR/THEME/Magento_Catalog/templates/product/view/attribute1.phtml

Add your attribute code.

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

Inside your xml file,

app/design/frontend/VENDOR/THEME/Magento_Catalog/layout/catalog_product_view.xml


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

after that move your attribute1 block where ever you want.

for example,

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

Thanks....

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

try this it will help you.

<?php
$_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;
            endif;
        endforeach;
    }
}

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.