3

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.

2 Answers 2

1

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

1
  • I have already gotten the option_type_id. But I want to get the label and value of option
    – Nero Phung
    Commented Dec 15, 2017 at 12:44
0

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.

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.