1

How to get product custom option value and title from ids, I have following ids

[0] => stdClass Object
        (
            [option_id] => 3283
            [option_value] => 8893
        )

i want to get title of option_id(id) and value of option_value(id)

2 Answers 2

1

You can get the option details from Magento\Catalog\Helper\Product\Configuration.

By default, you will get title values under getCustomOptions(). Instead of getting direct options details, you can pass the product with selected customOptions Items, so you can easily get the title.

public function getCustomOptions(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item) {
    $product = $item->getProduct();
    $options = [];
    $optionIds = $item->getOptionByCode('option_ids');
    if ($optionIds) {
        $options = [];
        foreach (explode(',', $optionIds->getValue()) as $optionId) {
            $option = $product->getOptionById($optionId);
            if ($option) {
                $itemOption = $item->getOptionByCode('option_' . $option->getId());
                /** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
                $group = $option->groupFactory($option->getType())
                    ->setOption($option)
                    ->setConfigurationItem($item)
                    ->setConfigurationItemOption($itemOption);

                if ('file' == $option->getType()) {
                    $downloadParams = $item->getFileDownloadParams();
                    if ($downloadParams) {
                        $url = $downloadParams->getUrl();
                        if ($url) {
                            $group->setCustomOptionDownloadUrl($url);
                        }
                        $urlParams = $downloadParams->getUrlParams();
                        if ($urlParams) {
                            $group->setCustomOptionUrlParams($urlParams);
                        }
                    }
                }
                $options[] = [
                    'label' => $option->getTitle(),
                    'value' => $group->getFormattedOptionValue($itemOption->getValue()),
                    'option_id' => $option->getId(),
                    'option_type' => $option->getType(),
                    'option_type_id' => $itemOption->getValue(),
                    'custom_view' => $group->isCustomizedView(),
                ];
            }
        }
    }
0

Try this code

public function __construct(,
    \Magento\Catalog\Model\ResourceModel\ProductFactory $productFactory
) {
    parent::__construct();
    $this->productFactory = $productFactory;
}

$poductReource=$this->productFactory->create();
$attribute = $poductReource->getAttribute($attributeCode);
if ($attribute->usesSource()) {
    return  $option_Text = $attribute->getSource()->getOptionText($optionId);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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