Magento 2.3.0 – Get Product Attribute Name in PHTML in CMS Block

magento2magento2.3PHPproduct-attribute

I have a attribute called

Artist

It has several artist names

XYZ
ABC
RST

I need to be get the artist name associated to the current product page via a call to a phtml in a CMS Block.

What do I need to put in the block file and in the phtml file for this?

I've tried this solution but I'm unsure how to go about it or if even a recommended method of doing it.


UPDATE

What I'm looking for is to use a short code to call the attribute name within the short description block, as follows, rather than changing the view xml file to get and position the attribute name on the product page.

<p>Painting by {{block class="" template=""}} in oil on canvas</p>

Where

{{block class="" template=""}}

= attribute artist name

Best Answer

You can directly call product attribute in following file.

app/design/frontend/{Package}/{theme}/Magento_Catalog/layout/catalog_product_view.xml

Using following code:

<referenceContainer name="product.info.main">
        <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.custom" template="Magento_Catalog::product/view/attribute.phtml" group="detailed_info" after="product.info.stock.sku">
            <arguments>
                <argument name="at_call" xsi:type="string">getShortDescription</argument>
                <argument name="at_code" xsi:type="string">short_description</argument>
                <argument name="css_class" xsi:type="string">overview</argument>
                <argument name="at_label" xsi:type="string">none</argument>
                <argument name="title" translate="true" xsi:type="string">Overview</argument>
                <argument name="add_attribute" xsi:type="string">itemprop="description"</argument>
            </arguments>
        </block>
</referenceContainer>

Note: You need to replace short_description with your new attribute code. You can also change display position of product attribute.

Update I

If you want to manage it from static block you need to create a static block with identifier artist_block with following content

{{block class="Magento\Catalog\Block\Product\View" name="artist" template="Magento_Catalog::artist.phtml"}}

Now create artist.phtml at:

app/design/frontend/{Package}/{theme}/Magento_Catalog/templates/artist.phtml

with following content

<?php 
$product = $block->getProduct();
echo $product->getId();
echo $product->getName();//you can change attribute here

Now you need to call static block at:

app/design/frontend/{Package}/{theme}/Magento_Catalog/layout/catalog_product_view.xml

by following code:

<referenceContainer name="product.info.main">
        <block class="Magento\Cms\Block\Block" name="artist.block" after="product.info.stock.sku">
            <arguments>
                <argument name="block_id" xsi:type="string">artist_block</argument>
            </arguments>
        </block>
    </referenceContainer>

Hope above will help!

Related Topic