0

I need to get a custom attribute for catalog products on the product page. I added code,

<?php $_getMyAttr = $_product->getResource()->getAttribute('my_custom_attr'); ?>
<?php if ($_getMyAttr){ ?>
<?php /* @escapeNotVerified */ echo $attrTestValue = $_getMyAttr->getFrontend()->getValue($_product); }?>

This works fine when I added to the,

app/design/frontend/Mgs/ethan/Magento_Catalog/templates/product/view/type/default.phtml

but does not work when I added to,

app/design/frontend/Mgs/ethan/Magento_Theme/templates/html/title.phtml

I have also tried to creating a block with the same code and adding that block to my catalog_product_view.xml in the directory app\design\frontend\Mgs\Ethan\Magento_Catalog\layout\catalog_product_view.xml. But I get a 500 error anytime when I try this.

Is there anything I need to do differently?

catalog_product_view.xml

   <?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>



        <referenceBlock name="page.main.title">
            <arguments>
                <argument name="css_class" xsi:type="string">product</argument>
                <argument name="add_base_attribute" xsi:type="string">itemprop="name"</argument>
            </arguments>
        <block class="Magento\Catalog\Block\Product\View\Attributes" name="product.color.attributes" as="additional_color" template="Magento_Catalog::product/view/color_attribute.phtml" />
    </referenceBlock>


    <referenceBlock name="head.additional">
        <block class="Magento\Framework\View\Element\Template" name="script.sharethis" template="MGS_Mpanel::sharethis/sharethis_script.phtml" ifconfig="mpanel/share_config/share_product_detail"/>
    </referenceBlock>

    <block class="Magento\Theme\Block\Html\Title" name="mgs.product.title" template="Magento_Theme::html/title.phtml"/>

    <referenceBlock name="product.info.extrahint" remove="true"/>

    <referenceBlock name="product.info.sku" remove="true"/>

    <move element="mgs.product.title" destination="product.info.main" before="-" />

    <move element="product.info.overview" destination="product.info.main" after="product.addtocaart.form"/>

    <!-- <move element="product.info.price" destination="product.info.main" after="product.brand.name"/> -->

    <container name="product.info.sku.brand" htmlTag="div" htmlClass="product-sub-infomation" after="product.info.review">
        <block class="Magento\Catalog\Block\Product\View\Description" name="mgs.product.info.sku" template="product/view/attribute.phtml">
            <arguments>
                <argument name="at_call" xsi:type="string">getSku</argument>
                <argument name="at_code" xsi:type="string">sku</argument>
                <argument name="css_class" xsi:type="string">sku</argument>
                <argument name="at_label" xsi:type="string">default</argument>
                <argument name="add_attribute" xsi:type="string">itemprop="sku"</argument>
            </arguments>
        </block>
    </container>



    <move element="product.info.sku.brand" destination="product.info.main" after="product.info.price" />

    <move element="product.brand.name" destination="product.info.sku.brand" after="-" />

    <move element="product.attribute.specs.table" destination="product.info.main" after="product.info.price"/>
</body>

Thank you. I have asked this question somewhere else, so when you chance upon that, just ignore it.

  • Yeah, that's me, that didn't work. I tried it a couple of times and came up with this. I did be glad if we can go back there and get the answer right there. I will be glad to mark your answer as correct – winfred adrah Mar 26 '18 at 16:37
  • do you want to show all attributes or just specific attributes ? – Naveed Asim Mar 26 '18 at 17:06
  • just one attribute – winfred adrah Mar 26 '18 at 18:29
  • do you want to show it under product price ? – Naveed Asim Mar 26 '18 at 18:33
0

To show attribute on product detail page you have to create a template file color_attribute.phtml in Magento_Catalog/templates/product/view folder.

and add below code in that template file :

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * Product view template
 *
 * @see \Magento\Catalog\Block\Product\View\Description
 */
?>

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();

echo $_product->getResource()->getAttribute('ccolor')->getFrontend()->getValue($_product);

?>

and place this xml block in catalog_product_view.xml file where ever you want :

<block class="Magento\Catalog\Block\Product\View\Attributes" name="product.color.attributes" as="additional_color" template="Magento_Catalog::product/view/color_attribute.phtml" />

for example if you want to show color attribute under title then place this block in page.main.title block .

<referenceBlock name="page.main.title">
    <arguments>
        <argument name="css_class" xsi:type="string">product</argument>
        <argument name="add_base_attribute" xsi:type="string">itemprop="name"</argument>
    </arguments>
    <block class="Magento\Catalog\Block\Product\View\Attributes" name="product.color.attributes" as="additional_color" template="Magento_Catalog::product/view/color_attribute.phtml" />
</referenceBlock>

Updated :

To Add Attribute above product title open catalog_product_view.xml and search for "product.info.main" container and then place below code above this container ( code will be between <referenceContainer name="content"> and "product.info.main" container )

<container name="product-info-main" htmlTag="div" htmlClass="product-attr" before="-">
   <block class="Magento\Catalog\Block\Product\View\Attributes" name="product.color.attributes" as="additional_color" template="Magento_Catalog::product/view/color_attribute.phtml" before="page.main.title" />
</container>

Hope it will work for you

NOTE : you would need to adjust template styling and other parts as per your need.

  • Hi, this did not work for me, i don't have these containers in my child theme <referenceContainer name="content"> and "product.info.main" container, i placed the updated version of your answer in my parent theme, but that did not have any effect too. Or I have too run these commands? *run php bin/magento setup:di:compile command *run php bin/magento setup:static-content:deploy command *run php bin/magento cache:clean command *run php bin/magento cache:flush command – winfred adrah Mar 27 '18 at 10:06
  • I have updated my question by adding catalog_product_view.xml , I believe that is where my problem is coming from. – winfred adrah Mar 27 '18 at 11:46
  • you also need to create template color_attribute.phtml on that path : Magento_Catalog/templates/product/view/, this is the reason you are getting 500 error, since there is no template – Naveed Asim Mar 27 '18 at 12:05
  • I have created that file color_attribute.phtml at the right file path with the content in your answer. I am not getting a 500 error now, there is no change to the page even after clearing cache. – winfred adrah Mar 27 '18 at 12:43
  • you have to deploy static content and also you would need to change the attribute code , secondly have you followed all steps which i have written in my answer – Naveed Asim Mar 27 '18 at 12:47

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.