TransWikia.com

Display product attribute on product page magento 2

Magento Asked on October 1, 2021

I am using magento 2.3

I want to display product attribute on product page, I select the storefront properties tab and change the value of Visible on Catalog Pages on Storefront drop down value No to Yes. but custom product attribute is not showing.

enter image description here

please see attached screenshot. same I want to display.

3 Answers

In order to do add your custom attributes to the product detail page, you must:

  1. Set up a custom attribute in the admin,
  2. Configure your custom attribute to display on the product page,
  3. Add your custom attribute to your product's attribute set,
  4. Add content to the attribute on your product's edit page.

Note the attribute code of your custom attribute.

Once that is complete, if not already setup, you must set up a custom theme:

  1. Create theme: https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/themes/theme-create.html

  2. Once your theme is created, applied and configured, add the following folder to your custom theme:

    [theme] / Magento_Catalog / layout
    

Create the following file:

[theme] / Magento_Catalog / layout / catalog_product_view.xml

Examine the following file:

vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml

Determine which container you would like to display your custom attribute within and note the name of the container. We will use the name of the container in order to reference the container in the catalog_product_view.xml file.

i.e. <referenceContainer name="content"> or <referenceContainer name="product.info.main"> or <referenceContainer name="add-container-name-here">

Place this code within catalog_product_view.xml, replacing {attributeCode} with your custom attribute code:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.main">
            <block class="MagentoCatalogBlockProductView" name="product.attribute.{attributeCode}" template="Magento_Catalog::product/view/attribute.phtml">
                <arguments>
                    <argument name="at_call" xsi:type="string">get{attributeCode}</argument>
                    <argument name="at_code" xsi:type="string">{attributeCode}</argument>
                    <argument name="at_label" xsi:type="string">Any Custom Attribute Label</argument>
                    <argument name="css_class" xsi:type="string">Any CSS Class Name</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

Flush cache & deploy static resources (you may also need to run indexer).

This will display the attribute using the default Magento 2 attribute template.


If you would like to use a custom template to display your custom attribute, use this code within the catalog_product_view.xml file:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.main">
            <block class="MagentoCatalogBlockProductView" name="product.attribute.custom" template="Magento_Catalog::product/view/attribute/custom.phtml">
                <arguments>
                    <argument name="title" translate="true" xsi:type="string">Your Custom Attribute Title</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

Add the following folder to your custom theme:

 [theme] / Magento_Catalog / templates / product / view / attribute

Create the following file:

[theme] / Magento_Catalog / templates / product / view / attribute / custom.phtml

Add the following code to custom.phtml, replace {attributeCode} with your custom attribute code:

<?php
$product = $block->getProduct();
$attrData = $product->getData('{attributeCode}');
$blockTitle = $this->getData('title');
if (trim($attrData) !== "") : ?>
    <div class="product-full-width-section">
        <h2 class="product-section-title"><?= $blockTitle; ?></h2>
        <?php echo $attrData; ?>
    </div>
<?php endif; ?>

Adjust the HTML code, as needed.

Flush cache & deploy static resources (you may also need to run indexer).

Answered by Joshua34 on October 1, 2021

Try it like this:-

app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/view/attr.phtml

<?php echo $this->helper('MagentoCatalogHelperOutput')->productAttribute($block->getProduct(), $block->getProduct()->getAttributeCode(), 'attribute_code'); ?>

app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_product_view.xml

<referenceBlock name="product.info.main">
    <block class="MagentoCatalogBlockProductView" name="attr" template="product/view/attr.phtml" />
</referenceBlock>

now remember you can select the container according to your requirement here <referenceBlock name="product.info.main">

hope this helps!

Answered by Pramod on October 1, 2021

please assign value to that attribute in product edit, so that attribute display in product edit,

if it not display than run the command

php bin/magento indexer:reindex
php bin/magento cache:flush

Answered by Jigs Parmar on October 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP