Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DOC

How to get Product’s Custom Attribute and their Options in Magento 2

How to get product’s custom attribute and their options in Magento 2

We’ll explore how to get product custom attribute options in Magento 2, including practical methods, code examples, and best practices for accurate and efficient data retrieval.

Suppose you want to show a product creation form on frontend with some custom attributes and you do not know which attributes are assigned to which attribute set , so this blog will be very helpful for you :

Let’s create a function in helper, from which we are getting attributes according to attribute set ID :

<?php
namespace Company\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    private $attributeManagement;

    /**
     * @param \Magento\Framework\App\Helper\Context        $context
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Eav\Model\AttributeManagement $attributeManagement
    ) {
        parent::__construct($context);
        $this->attributeManagement = $attributeManagement;
    }

    public function getCustomAttributes($attributeSetId)
    {
        try{
            $groups = $this->attributeManagement->getAttributes(
               \Magento\Catalog\Model\Product::ENTITY, // entity type
               $attributeSetId // this will contain your attribute set ID
            );
            foreach ($groups as $node) {
                return $node->getData(); // in this you will get particular attribute data
            }
        }catch(\Exception $e){
            return false;
        }
    }
}

When you call above function getCustomAttributes($attributeSetId), you will get all attributes for an attribute set Id.

Now, suppose you want to get a dropdown type of attribute’s options. So here is the code to get all options of an attribute of type Dropdown :

<?php
namespace Company\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    private $productAttributeRepository;

    /**
     * @param \Magento\Framework\App\Helper\Context        $context
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository
    ) {
        parent::__construct($context);
        $this->productAttributeRepository = $productAttributeRepository;
    }

    public function getCatalogResourceEavAttribute($attrCode)
    {
        // $attrCode will be attribute code, i.e. 'manufacturer'
        try{
            return $this->productAttributeRepository->get($attrCode)->getOptions();
        }catch(\Exception $e){
            return false;
        }
    }
}

When you call above function  getCatalogResourceEavAttribute($attrCode), you will get an array of options, if any.

Hope this blog will help you to develop custom functionality in your module in a better way. Try this and if you have any query then just comment below 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home