Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento-在observer中更改产品自定义选项值_Magento_Options - Fatal编程技术网

Magento-在observer中更改产品自定义选项值

Magento-在observer中更改产品自定义选项值,magento,options,Magento,Options,当产品添加到购物车时,如何更改产品自定义选项值?我的伪代码- public function generatePreviewImage(Varien_Event_Observer $obs) { // Get the order item $orderItem = $obs->getOrderItem(); // Get product options with values $productOptions = $orderItem->getProd

当产品添加到购物车时,如何更改产品自定义选项值?我的伪代码-

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
    $orderItem->setProductOptions($productOptions);
    $orderItem->save();
}
我的解决方案

// 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
        $infoBuyRequestOption->setValue(serialize($unserializedInfoBuyRequest));
        $item->setOptions($options)->save();
        Mage::getSingleton('checkout/cart')->save();
        break;
    }
}

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

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

非常感谢。你的代码帮了我。。但我还要加上定制价格。请你建议我,我如何才能添加额外的价格与选择。请为我以前的评论做必要的