cancel
Showing results for 
Search instead for 
Did you mean: 

How to get item customizable option in Observer?

SOLVED

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 ACCEPTED SOLUTION

Accepted Solutions

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());

 

View solution in original post

3 REPLIES 3

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());

 

Re: How to get item customizable option in Observer?

Thank you!

Re: How to get item customizable option in Observer?

@antoniogur615a kindly accept as a solution if this works for you!