Magento - change product custom options values in observer

How can I change product custom options values when product is adding to cart? My pseudo code -

public function generatePreviewImage(Varien_Event_Observer $obs) {
    // Get the order item
    $orderItem = $obs->getOrderItem();

    // Get product options with values
    $productOptions = $orderItem->getProductOptions()

    // Set new value
    $productOptions['options'][$index]['option_value'] = 'my new value';

    // Update options and save orderItem


My solution

// Add new info_buyRequest option
$item = $obs->getQuoteItem();
$product = $item->getProduct();
$options = $item->getOptions();

foreach ($options as $option){
    // Select buy request options
    if($option->getCode() == 'info_buyRequest') {
        $infoBuyRequestOption = $option;

        $unserializedInfoBuyRequest = unserialize($infoBuyRequestOption->getValue());

        // Set my new option
        $unserializedInfoBuyRequest['preview'] = 'My new preview image';

        // Store new options

// And then in cart rendering view
$preview = unserialize($this->getProduct()->getCustomOption('info_buyRequest')->getValue())['preview'];

// In admin panel
$preview = $_item->getProductOptionByCode('info_buyRequest')['preview'];

Need Your Help

unable to break out of jquery each loop

jquery return iteration each

I have a nested loop written in jquery and return false inside my child loop keeps appending the same text to the parent row. My code,