1

I need to get the selected custom option value of product added in the cart. To observe the product add to cart, i have used an event checkout_cart_product_add_after

In the observer, I have loaded the product using below code:

$item = $observer->getEvent()->getData('quote_item');
$product = $observer->getEvent()->getData('product');

I am able to get product quantity added to the cart using $product->getQty() however, i am not getting the values of selected custom option.

By using below code i can get all the custom options for product

foreach($product->getOptions() as $o){
        $optionType = $o->getType();
        foreach($o->getValues() as $value){
            echo "<pre>";
            print_r($value->getData());
            echo "</pre>";
        }
}

Can anyone please guide how to get selected option value.

[Edit]

As per the answer by Raphael, i observed an event catalog_product_type_prepare_full_options and got the value of selected option for the product.

Now, my problem is i need to pass these values to the observer i have created for checkout_cart_product_add_after. The reason behind this is, I am trying to add a product into cart when certain product with selected custom option is added to the basket.

Is there a way to pass these value to my observer or any better/correct solution for this.

3

As an alternative I reckon you should try observing catalog_product_type_prepare_full_options which is the event that is dispatched when Magento prepare the custom defined options for a product before adding it to the cart. It is declared in /Magento/Catalog/Model/Product/Type/AbstractType :

protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $product, $processMode)
{
    $transport = new \StdClass();
    $transport->options = [];
    foreach ($product->getOptions() as $option) {
        /* @var $option \Magento\Catalog\Model\Product\Option */
        $group = $option->groupFactory($option->getType())
            ->setOption($option)
            ->setProduct($product)
            ->setRequest($buyRequest)
            ->setProcessMode($processMode)
            ->validateUserValue($buyRequest->getOptions());

        $preparedValue = $group->prepareForCart();
        if ($preparedValue !== null) {
            $transport->options[$option->getId()] = $preparedValue;
        }
    }

    $eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode);
    $this->_eventManager->dispatch(
        $eventName,
        ['transport' => $transport, 'buy_request' => $buyRequest, 'product' => $product]
    );
    return $transport->options;
}

You should be able to retrieve the selected option with the following code:

$transport = $observer->getEvent()->getData('transport');
$chosenOption = $transport->options;
  • Thanks for your suggestion, but the thing is I can't use this event as i need to add a product to the cart once a particular product is added with selected custom option. Hence, i am using checkout_cart_product_add_after event for this. I need to get the option value from there. – Dexter May 20 '16 at 5:55
  • Do you know a way how i can get the selected option value from event catalog_product_type_prepare_full_options and pass it to checkout_cart_product_add_after observer file? – Dexter May 20 '16 at 6:41
  • do you know how can i get the SKU of the options selected from option id? – Dexter May 20 '16 at 9:53
  • @Dexter Hmm I'm not sure I reckon you should try to open a new question for that and close this one if my answer helped you – Raphael at Digital Pianism May 20 '16 at 10:05
  • I can only give you +1 but can't accept at present, as I need to get the selected custom option on checkout_cart_product_add_after which still remains a mistery. However your answer helped me a lot in exploring. Thanks – Dexter May 20 '16 at 12:02
1

I was not able to get the selected custom option on event checkout_cart_product_add_after

however, i was successful in getting the values using checkout_cart_save_before event.

For those who might need it in future. The code for retrieving the custom options is as below:

public function execute(\Magento\Framework\Event\Observer $observer) {  
    /* Fetches data of items available in cart. */
    $cart = $observer->getEvent()->getData('cart');
    $cartItems = $cart->getItems();

    foreach($cartItems as $item){
        //here you do what you want with the product.
        $_customOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct()); 
        if(array_key_exists('options', $_customOptions)){
            //print_r($_customOptions);
            foreach($_customOptions['options'] as $option){
                print_r($option);
            }
        }
    }
}

Hope it helps. I am accepting this answer as this is the closest I could get to. I will be happy to accept the solution if someone posts it.

  • I am struggling to add price of custom option with my product price. since i am getting the additional price using your above solution , how can i add this price in product total price? – Yudi Feb 17 '17 at 7:51

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.