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.

  • You can try by adding $value->getData('add column name which contains values.') – Arjun Jul 14 '16 at 6:20
  • @Arjun ` codeprint_r($value->getData('color'));code ` not working – Arun SS Jul 14 '16 at 6:24
  • My answer hasn't helped you? – Siarhey Uchukhlebau Sep 9 '16 at 12:48
  • Check the link, rakeshjesadiya.com/… – Rakesh Jesadiya Feb 25 at 10:47
4

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.

0
$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;
-4

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
    You should not use the object manager like this. You should be injecting your classes using Magento 2's dependency injection. – nick.graziano Sep 26 '17 at 16:46
  • Using break is not a good idea...any other solution ? – Manish Nov 7 '17 at 6:12

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.