【问题标题】:Magento - change product custom options values in observerMagento - 在观察者中更改产品自定义选项值
【发布时间】:2015-12-02 17:04:55
【问题描述】:

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

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

【问题讨论】:

    标签: magento options


    【解决方案1】:

    我的解决方案

    // 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'];
    

    【讨论】:

    • 谢谢您..您的代码帮助了我..但我还必须添加自定义价格。请您建议我如何使用该选项添加附加价格。
    • 请对我之前的评论做些必要的事情
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2013-03-23
    • 1970-01-01
    相关资源
    最近更新 更多