Currently product name (title) is displayed from attribute name
.
How to display product name (title) from custom attribute second_name
on product view page?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communitySetp 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 :-
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.
echo "Hello"; die();
Your demo.phtml file call or not?
Dec 15, 2021 at 6:32