0

my catalog_category_view.xml

<?xml version="1.0"?>
<page 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="YourVendor\YourModule\Block\Product\CustomAttribute" name="custom.attribute" template="YourVendor_YourModule::product/custom_attribute.phtml" />
        </referenceContainer>

    </body>
</page>

my custom_attribte.phtml

<?php
$product = $block->getProduct();
$attrData = $product->getData('feature');
$blockTitle = $this->getData('title');
if ($attrData !== "") : ?>
    <div class="product-full-width-section">
        <h2 class="product-section-title"><?= $blockTitle; ?></h2>
        <?php echo $attrData; ?>
    </div>
<?php endif; ?>
1
  • Go to Stores -> Attributes -> Product, search for your attribute code feature and check in the storefront properties if you have enabled "Used in Product Listing"
    – schnere
    Apr 21, 2023 at 5:02

3 Answers 3

1

Please follow the below point.

To create the attributes from the admin panel, the admin will navigate to Stores > Attributes > Products.

Tapping the Product menu option brings up another page to add product attributes. Here, click the “Add New Attribute” button to add a new attribute. and then save the attribute.

After that, to assign this attribute to an attribute set navigate to Stores > Attributes > Attribute Sets. and save.

Then after you need to create a helper in your module.

app/code/Vendor/Catalog/Helper/classname.php

 <?php
    declare(strict_types=1);
    
    namespace Vendor\Catalog\Helper;
    
    use Magento\Framework\App\Helper\AbstractHelper;
    use Magento\Framework\App\Helper\Context;
    
    
    class ClassName extends AbstractHelper
    {
        protected $_registry;
    
        public function __construct(
            \Magento\Framework\Registry $registry,
            \Magento\Cms\Model\Template\FilterProvider $filterProvider,
            Context $context
        ) {
            $this->_registry = $registry;
            $this->_filterProvider = $filterProvider;
            parent::__construct($context);
        }
    
        public function getAttributes(){
    
           return $this->_registry->registry('current_product');
        }
        public function getAttributeFilter($content){
            
           return $this->_filterProvider->getPageFilter()->filter($content);
        }
    
       
    }

and you can get attributes in your Phtml file using the below code.

<?php 
    $eavHelper = $this->helper(vendor\Catalog\Helper\ClassName::class);
    $product = $eavHelper->getAttributes();
    $attribute = $product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($product);  
?>
0

Please following the below step

Stores -> Attributes -> Product

and check your attribute code feature and check in the storefront properties if you have enabled "Used in Product Listing"

Hope it is help for you.

0

If you need to get a custom product attribute value on the category page then kindly update your code in the catalog_category_view.xml layout file.

<?xml version="1.0"?>
<page 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="Magento\Catalog\Block\Product\View" name="custom.attribute" template="YourVendor_YourModule::product/custom_attribute.phtml" />
        </referenceContainer>

    </body>
</page>

Now you can get the product attribute values in the custom_attribute.phtml file.

Kindly accept my answer if your problem is fixed.

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.