0

Hi I am using below product collection,but it returns option id of attribute in collection my question is how to get option label rather than option id in collection. Is there any way ? Because I am sending data to other endpoint

/**
 * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
private function getProductCollection()
{
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
    $collection = $this->productCollectionFactory->create();

    $collection
        ->addAttributeToSelect('*');

    return $collection;
}

and I do not want to use below code to get option label in for loop,because I gets everytime get call in loop, Anyone has better option ?

$optionId = 10;

$attr = $_product->getResource()->getAttribute('color');
 if ($attr->usesSource()) {
       $optionText = $attr->getSource()->getOptionText($optionId);
 }

1 Answer 1

0

Did you try the service contract?

use Magento\Catalog\Api\ProductAttributeRepositoryInterface;      
$attr = $this->productAttributeRepository->get('color')->getDefaultFrontendLabel();

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.