cancel
Showing results for 
Search instead for 
Did you mean: 

How to get item customizable option in Observer?

Highlighted

How to get item customizable option in Observer?

Hi, I have configurable product with customizable option "custom width" and I need to read this value in Observer for event  'checkout_cart_product_add_after'. Observer class:

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\Product\Type\Configurable;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Serialize;

class FooClass implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
//$serializer = ObjectManager::getInstance()->get(Serialize::class);

$item = $observer->getEvent()->getData('quote_item');
$item = ($item->getParentItem() ? $item->getParentItem() : $item);

// get 'custom width'

// logic for changing custom price depends of option 'custom width'
// $customPrice = ...............

$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
}
}

 

1 REPLY 1
Highlighted

Re: How to get item customizable option in Observer?

@antoniogur615a ,

you can use the below code to get all options for quote item 

$item->getProductOptions()

if that returns null then you can use the below code

$item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());