6

I have two custom options for a product. Color and Size and both are drop-downs. In product detail pages, I have to display all available colors of that product.

I tried the following code and it works:

$_product = $block->getProduct();
foreach ($_product->getOptions() as $o) {
    foreach ($o->getValues() as $value) {
        print_r($value->getData());
    }
}

But it returns all the values of Color and Size. But I only need the color values. That is I want to select the custom options by color.

5
  • You can try by adding $value->getData('add column name which contains values.')
    – Arjun
    Commented Jul 14, 2016 at 6:20
  • @Arjun ` codeprint_r($value->getData('color'));code ` not working
    – Arun SS
    Commented Jul 14, 2016 at 6:24
  • My answer hasn't helped you? Commented Sep 9, 2016 at 12:48
  • Check the link, rakeshjesadiya.com/… Commented Feb 25, 2019 at 10:47
  • Any have idea I used Mageworx Product Options extension and need to get those custom option field in product rest API option's value ? Commented Oct 17, 2019 at 10:56

3 Answers 3

5

Try this:

$_product = $block->getProduct();
foreach ($_product->getOptions() as $o) {
    if ($o->getTitle() != 'Color') { // or another title of option
        continue;
    }
    foreach ($o->getValues() as $value) {
        print_r($value->getData());
    }
}

You should search for your option by the title because the option ID is unknown.

3
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product);

foreach($customOptions as $optionKey => $optionVal):
               foreach($optionVal->getValues() as $valuesKey => $valuesVal) {
                      echo $valuesVal->getId().' '.$valuesVal->getTitle();
                }
endforeach;
1
  • 3
    Everything is good just if someone is reffering this please use Dependency Injection method instead of Object Manager
    – Jaymin
    Commented May 17, 2020 at 6:08
-8

This topic is old but I have found a solution. (For all people that found this topic via Google, like me :D)

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $optionisset = false;
    $allItems = $this->cart->getQuote()->getAllVisibleItems();
    //loop all cart items and check options
    foreach($allItems as $item) {
        $itemOptions = $item->getOptions();
        $itemOptions = unserialize($itemOptions[0]['value']);
        //get set option ids for parent and child option
        foreach ($itemOptions['options'] as $key=>$value) {
            $parentOptionId = $key;
            $optionId = $value;
            $option = $objectManager->get('\Magento\Catalog\Model\Product\Option')->load($parentOptionId);
            $product_id = $option->getProductId();
            $product = $objectManager->get('\Magento\Catalog\Model\Product')->load($product_id);
            $options = $objectManager->get('\Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);
            //loop all parent options
            foreach($options as $option) {
                $values = $option->getValues();
                //loop all child options
                foreach($values as $value) {
                    //if name is MyOptionName and it is set for this cart item
                    if($value->getTitle() == "MyOptionName" && $value->getOptionTypeId() == $optionId) {
                     //Do something here
                        $optionisset = true;
                        break;
                    }
                }
                if($optionisset == true) break;
            }
            if($optionisset == true) break;
        }
        if($optionisset == true) break;
    }

You can change "MyOptionName" to your Option Name and you will get back true if one of your products in your cart is set to this option.

2
  • 2
    You should not use the object manager like this. You should be injecting your classes using Magento 2's dependency injection. Commented Sep 26, 2017 at 16:46
  • Using break is not a good idea...any other solution ?
    – Manish
    Commented Nov 7, 2017 at 6:12

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.