0

i have product attribute in dropdown i want to get all option of that attribute how i get programtically

2

you can add to the constructor of your class an instance of \Magento\Eav\Model\Config like this:

protected $eavConfig;
public function __construct(
    ...
    \Magento\Eav\Model\Config $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

then

$attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
$options = $attribute->getSource()->getAllOptions();
$arr = [];
    foreach ($options as $option) {
      if ($option['value'] > 0) {
        $arr[] = $option;
      }
    }       

Out put

(
    [0] => Array
        (
            [value] => 280
            [label] => option1
        )

    [1] => Array
        (
            [value] => 281
            [label] => option2
        )

    [2] => Array
        (
            [value] => 282
            [label] => option3
        )

    [3] => Array
        (
            [value] => 283
            [label] => option4
        )
)

Which is better? Answer is below.

\Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository ultimately at end call $attribute = $this->eavConfig->getAttribute($entityTypeCode, $attributeCode); You can judge by it which is better. :-)

1st call file : vendor\magento\module-catalog\Model\Product\Attribute\Repository.php method

public function get($attributeCode)
    {
        return $this->eavAttributeRepository->get(
            \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
            $attributeCode
        );
    }

2nd file call : vendor\magento\module-eav\Model\AttributeRepository.php

public function get($entityTypeCode, $attributeCode)
    {
        /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
        $attribute = $this->eavConfig->getAttribute($entityTypeCode, $attributeCode);
        if (!$attribute || !$attribute->getAttributeId()) {
            throw new NoSuchEntityException(
                __(
                    'The attribute with a "%1" attributeCode doesn\'t exist. Verify the attribute and try again.',
                    $attributeCode
                )
            );
        }
        return $attribute;
    }
  • how get values Array ( [label] => [value] => ) Array ( [value] => 2608 [label] => LKD - 2NS Large ) – dfsd Dec 18 '19 at 7:44
  • i want attribute text value – dfsd Dec 18 '19 at 8:13
  • @dfsd check updated answer. – Padhiyar Gaurang Dec 18 '19 at 10:39
  • \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository, its good or load from \Magento\Eav\Model\Config $eavConfig, – dfsd Dec 18 '19 at 11:01
  • @dfsd check the updated answer. :-) – Padhiyar Gaurang Dec 18 '19 at 11:09
0

Using Product Repository :-

protected $productAttributeRepository;

public function __construct(
    \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository,
){
    $this->productAttributeRepository = $productAttributeRepository;
}

After This You Can Get Options Like This :-

$options = $productAttributeRepository->get('attribute_code')->getOptions();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.