1

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

  • As per my understating you want to call a phtml in static block and html will be responsible for getting some product attribute.You will call static block on product view page .Right ? – Pawan Jan 14 '19 at 4:25
  • yes you are right. – user2240778 Jan 14 '19 at 4:30
  • Ok, let me try some solution for you. – Pawan Jan 14 '19 at 4:34
  • You can directly call attribute to view page. why creating phtml and static block ? – Pawan Jan 14 '19 at 5:04
  • How do I do that? – user2240778 Jan 14 '19 at 5:04
1

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!

  • I'm trying to use a short code to call the attribute name within the short description block, so this solution will not work. I have updated my question above. – user2240778 Jan 14 '19 at 5:54
  • See my update in answer and let me know if works. – Pawan Jan 15 '19 at 5:11
  • Thank you, solution partly works, it only gives me the current product name, I need to get the attribute name. Also the phtml files for are in this location app/design/frontend/{package}/{theme}/Magento_Catalog/templates/product so how do I modify the block call location accordingly. – user2240778 Jan 15 '19 at 6:40
  • You can use $product->getArtist(); – Pawan Jan 15 '19 at 6:43
  • It doesnt work, this is what I'm using <?php $product = $block->getProduct(); echo $product->getId(); echo $product->getArtist(); It only shows the number 1 followed by the product name as such "1productname" – user2240778 Jan 15 '19 at 6:53
0

UPDATE - Thanks to Pawan for helping with part of the solution.

Solution - modified from this article.

In the artist.phtml file

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

I used the following code.

<?php
$product = $block->getProduct();
$product_name = $product->getName();
$attr_exists = $product->getResource()->getAttribute('artist');

if ($attr_exists){
    $attr_name = $product->getAttributeText('artist');
}

echo $product->getAttributeText('artist');
?>

and called it from with a CMS Block using the following shortcode

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

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.