Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top