0

I want to display custom product attribute value on PDP Page just below the product name. How that can be achieved, please guide. Rashi

2 Answers 2

1

You can achieve this by overriding block and phtml file

Follow the link : How to display custom text from attribute after product title on product page

2

To call the custom attribute follow the below steps:

  1. Add this code in Magento_Catalog/layout/catalog_product_view.xml file and changed the name and its code.

<referenceContainer name="product.info.main">
    <block name="product.custom.attributes" class="Magento\Catalog\Block\Product\View" template="Magento_Catalog::product/view/custom-attribute.phtml" after="page.main.title">
    <arguments>
    <argument name="at_call" xsi:type="string">getDemoLink</argument>
    <argument name="at_code" xsi:type="string">customAttribute</argument>
    <argument name="css_class" xsi:type="string">customAttribute</argument>
    <argument name="at_label" xsi:type="string">Custom Attribute</argument>
    <argument name="add_attribute" xsi:type="string">itemprop="customAttribute"</argument>
    </arguments>
    </block>
    
    </referenceContainer>

  1. Create a template file in Magento_Catalog/templates/product/view/custom-attribute.phtml and add this code and change the attribute name.

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'default') {
    $_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>
<?php if ($_attributeValue): ?>
    <div class="custom-attribute">
        <?php if ($_attributeLabel != 'none'): ?>
            <strong class="type"><?php echo $_attributeLabel?></strong>
        <?php endif; ?>
        <div class="value" <?php echo $_attributeAddAttribute;?>>
            <?php echo $_attributeValue; ?>
        </div>
    </div>
<?php endif; ?>
1
  • Thanks this worked Commented Feb 7, 2022 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.