3

I want to get the option_id of one of the options on an attribute, but I only know the label of the option (by label, I mean the 'value' column in eav_attribute_option_value).

How can I get it?

Right now I have the following code:

/**  @var \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository */

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

foreach ($options as $option) {
    var_dump($option->getValue());  // option_id
    var_dump($option->getLabel());  // value
}

In other words, it is possible for me to loop through each option and check if the option_id matches the one I want, but I wan't to do this directly. Is it possible?

1
  • Did you figure this out? I've run into the same issue..
    – oscarpas
    Commented Nov 3, 2016 at 14:08

4 Answers 4

3

If you have the attribute, you can do this:

 $id = $attribute->getSource()->getOptionId($label);
0
    <?php 
protected $productAttributeRepository;

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

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

foreach ($options as $option) {
    var_dump($option->getValue());  // option_id
    var_dump($option->getLabel());  // value
}
0

I'm using a function to check optionId find (or exist). You may check it below.

function getOptionId(\Magento\Framework\ObjectManagerInterface $objectManager,\Magento\Eav\Model\Entity\Attribute $attribute, string $label)
{
    /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
    $attributeFactory = $objectManager->create('Magento\Eav\Model\Entity\Attribute');
    $attributeValues=array();

    $attributeValues[$attribute->getAttributeId()] = [];
    /** @var \Magento\Eav\Model\Entity\Attribute\Source\Table $sourceModel */
    $sourceModel = $objectManager->create('Magento\Eav\Model\Entity\Attribute\Source\Table');
    $sourceModel->setAttribute($attribute);

    foreach ($sourceModel->getAllOptions() as $option) {
        $attributeValues[ $attribute->getAttributeId() ][ $option['label'] ] = $option['value'];
    }
    if (isset($attributeValues[ $attribute->getAttributeId() ][ $label ])) {
        return $attributeValues[ $attribute->getAttributeId() ][ $label ];
    }

    // Return false if does not exist
    return false;
}
0

"To get option label by Id"

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    /** @var \Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptions */

    $attributeOptionsManagement = $objectManager->get(\Magento\Eav\Api\AttributeOptionManagementInterface::class);

    /** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $attributeOptions */

    $attributeOptions = $attributeOptionsManagement->getItems(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE, 'feature_products');
    $featuredOptions = [];
    foreach ($attributeOptions as $attributeOption) {
    $featuredOptions[$attributeOption->getValue()] = $block->escapeHtml($attributeOption->getLabel());
    }
echo $featuredOptions['option_id']

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.