Magento 2 – How to Add Custom Text Attribute to Products

custom-attributes

I add a Custom TEXT Attribute (more_info) to my Magento 2 Store and I will show the Value to my product.info.main Page.

I add

mytheme\Magento_Catalog\layout\catalog_product_view.xml

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

and

mytheme\Magento_Catalog\templates\product\view\more_info.phtml

<?php $_product = $this->getProduct(); ?>
    <?php echo $_product->getAttributeText('more_info') ?>

but they show me no value:-(

I'm very thankful for any help.

Best Answer

You need to do this changes,

Change your xml like this,

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

And modify your phtml file :

mytheme\Magento_Catalog\templates\product\view\more_info.phtml


<?php $_product = $block->getProduct(); ?>
<?php echo $_product->getAttributeText('more_info') ?>

OR

<?php $_product = $block->getProduct(); ?>
<?php echo $_product->getResource()->getAttribute('more_info')->getFrontend()->getValue($_product) ?>
Related Topic