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.

1

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

| improve this answer | |
  • I have already gotten the option_type_id. But I want to get the label and value of option – Nero Phung Dec 15 '17 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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