Question

I know the way to get the customizable options by this code below:

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

$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product


foreach ($product->getOptions() as $options) {
     $optionData = $options->getValues();
     foreach ($optionData as $data) {
            print_r($data->getData());
            echo   $optionPrice[] = $data->getPrice();
            echo $optionDuration[] = $data->getTitle();
     }
}

But I don't know how to get the option_type_id of that product. I want to know what option that product have selected like: Size : 10 kg in the select 10kg,20kg,30kg.

Was it helpful?

Solution 2

Finally, I found my own answer. The code below will get an array contain label and value of customizable product.

public function getCustomizableOption($item) {
        /**
         * @var $product \Magento\Catalog\Api\Data\ProductInterface
         */
        $product = $item->getProduct();
        $options = $product->getOptions();
        $responseData = [];
        $optionIds = explode(',',$product->getCustomOptions()['option_ids']->getData('value'));
        $optionTypeIds = [];
        foreach ($optionIds as $id) {
            array_push($optionTypeIds,$product->getCustomOptions()['option_'.$id]->getData('value'));
        }
        $i = 0;
        foreach ($options as $option) {
            $optionData = $option->getValues();
            foreach ($optionData as $data) {
                if (in_array($data->getData('option_type_id'),$optionTypeIds)) {
                    array_push($responseData,array('label' => $product->getData('options')[$i++]->getData('title'), 'value' => $data->getData('title')));
                    break;
                }
            }
        }
        return $responseData;
    }

Hope it will help who have the same question with me.

OTHER TIPS

I think you are looking for $product->getOptions()[0]->getValues()[0]->getOptionTypeId()

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top