Using: Magento 2.4.5 Community Edition
I currently have two custom phtml blocks {short_description.phtml, description.phtml} located in app/code/Vendor/Theme/view/frontend/templates.
As an example, short_description.phtml contains:
<div class="container attribute-container">
<div class="attribute-row">
<div class="col-12">
<div class="attribute-label">Attribute Label<span style="color #4361ee;">Attribute Value</span>
</div>
<div class="attribute-label">Attribute Label<span style="color: #4361ee;">Attribute Value</span>
</div>
<div class="attribute-label">Attribute Label<span style="color: #4361ee;">Attribute Value</span>
</div>
</div>
</div>
</div>
On the product page content tab, I am calling these phtml templates using the page builder for short description and description using the following:
{{block class="MagentoFrameworkViewElementTemplate" template="Vendor_Theme::short_description.phtml"}}
{{block class="MagentoFrameworkViewElementTemplate" template="Vendor_Theme::description.phtml"}}
At the moment, the product pages load the static html content perfectly fine. However, I would like to display the custom product attributes and their labels dynamically.
How would I accomplish this?
Attempt #1 – Unsuccessful
- Under storefront properties, the attributes are set to “Yes” for “Used in Product Listing.”
I changed the short_description.phtml template to include:
<?php
$product = $block->getProduct();
$sku = $product->getAttributeName('sku');
$manufacturer = $product->getAttributeName('manufacturer');
$warranty = $product->getAttributeName('warranty');
?>
<div class="container attribute-container">
<div class="attribute-row">
<div class="col-12">
<div class="attribute-label"><?php echo $sku ?><span style="color #4361ee;">Attribute Value</span>
</div>
<div class="attribute-label"><?php echo $manufacturer?><span style="color: #4361ee;">Attribute Value</span>
</div>
<div class="attribute-label"><?php echo $warranty ?><span style="color: #4361ee;">Attribute Value</span></div>
</div>
</div>
</div>
The error:
[1]: https://i.stack.imgur.com/eZ47j.png
I realize that I need to call the attribute values with a different function/line of code but I can’t seem to get just the attribute labels to display.