0

Currently product name (title) is displayed from attribute name.

How to display product name (title) from custom attribute second_name on product view page?

enter image description here

1 Answer 1

1

Setp 1 :- Go to or override app/design/frontend/Your_CompanyName/Your_ThemeName/Magento_catalog/layout/catalog_product_view.xml and add this code.

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.demo" template="product/view/demo.phtml" after="page.main.title">
    <arguments>
        <argument name="at_call" xsi:type="string">getProductName</argument>
        <argument name="at_code" xsi:type="string">product_name</argument>
        <argument name="css_class" xsi:type="string">product_name</argument>
        <argument name="at_label" xsi:type="string">ProductName</argument>
        <argument name="add_attribute" xsi:type="string">itemprop="product_name"</argument>
    </arguments>
</block>

Above code attribute name change as per your attribute name. Custom Attribute Just i have shown after main product title.

Step 2 :- Create New file like app/design/frontend/YOUR_VENDOR/YOUR_THEME/Magento_catalog/templates/product/view/demo.phtml. Add below code

<?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>
    <div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
</div>
<?php endif; ?>

After you run below one command like:-

- php bin/magento c:f

Output :-

enter image description here

Other thing if you want to show listing page. Follow below setps

Setp 1 :- Go to your custom attribute and To Display, the new attribute in List Page, set the value “YES” to “Used in Product Listing” under storefront Properties while creating the attribute.

Setp 2 :- To display the attribute value in product list page, add the below code in app/design/frontend/YOUR_VENDOR/YOUR_THEME/Magento_Catalog/templates/product/list.phtml. Add below code.

<?php echo$_item->getDemoLink(); ?>

THANKS.

3
  • Hell Mohit, thanks for reply. This not working for me. Can you please check my code? I need to overite standard title and display this from attribute second_name prnt.sc/22tgxs0
    – Sylvester
    Dec 13, 2021 at 14:40
  • and then I add this code and custom to second_name prnt.sc/22th5uv and add demo.phtml to this path and after c:f no result
    – Sylvester
    Dec 13, 2021 at 14:41
  • In your demo.phtml file first lie add echo "Hello"; die(); Your demo.phtml file call or not? Dec 15, 2021 at 6:32

Your Answer

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

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