0

i'm using 2.2.X

How to get Stores > Attributes > Product > attributes code custom show to the Front-end and CMS Page.

look like <? php echo:_('xxxx') ?> 18n

THANK YOU!

0

In block file of your module, you have to create object of Magento\Catalog\Model\Product

<?php
namespace Vendor\YourModule\Block;

class Demo extends \Magento\Framework\View\Element\Template
{
    protected $_product;

    public function __construct(Template\Context $context,
        \Magento\Catalog\Model\Product $product
    )
    {
        $this->_product = $product;
        parent::__construct($context);
    }

    /**
     * GetProductObject Method to get product object.
     * @return object Product model object
     */
    public function getProductObject()
    {
        return $this->_product;
    }        
}

In your layout xml file add this code:

Vendor/YourModule/view/frontend/layout/frontname_index_index.xml (make appropriate name of layout xml file)

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\YourModule\Block\Demo" name="name" template="Vendor_YourModule::name.phtml"/>
        </referenceContainer>
    </body>
</page>

In your name.phtml you can call these methods using this code.

Vendor/YourModule/view/frontend/templates/name.phtml

<?php
    $_product = $block->getProductObject();
?>
<?= $_product->getResource()->getAttribute('Attribute   Code')->getFrontend()->getValue($_product);  // To get Label value of dropdown option?>
<?= $_product->getAttributeCode(); // eg. $_product->getPrice()?>
| improve this answer | |

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.