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

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

Today I am going to explain that how you can get product’s custom attributes in magento 2 in this article. 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 🙂

. . .

Comment

Add Your Comment

Be the first to comment.

css.php