【问题标题】:How to modify custom attribute of custom attribute set while add to cart?添加到购物车时如何修改自定义属性集的自定义属性?
【发布时间】:2015-09-23 13:58:14
【问题描述】:

我想在添加到购物车时更改自定义属性集(优惠券)的自定义属性(优惠券值)值。我在观察者事件 checkout_cart_product_add_after 中尝试过。我可以设法更改价格,但无法更改自定义属性凭证值的值。

这是我的功能

    public function change_price($observer) {
        // Get the quote item
        $item = $observer->getQuoteItem();
        // Ensure we have the parent item, if it has one
        $item = ($item->getParentItem() ? $item->getParentItem() : $item );
        Mage::log("Item Details: ".print_r($item, true), null, 'item_before1.log');
        // Load the custom price
        $price = Mage::app()->getRequest()->getPost('custom_voucher_price');
            if(!empty($price)){
                    $product = Mage::getModel('catalog/product')->load($item->getProductId());
                    $product->addAttributeUpdate('voucher_value', 39 , 1);
                    //$product->addCustomOption('voucher_value_value', 259 , $product);
                    $product->save();
                    $voucher_discount = $product->getAttributeText('voucher_discount');
                    $voucher_value = $product->getAttributeText('voucher_value');
                    Mage::log("voucher_value: ".print_r($voucher_value, true), null, 'voucher_value.log');
                    $voucher_discount_amount = 0;
                    if($voucher_discount!=null && (!empty($voucher_discount))){
                    $voucher_discount = floatval($voucher_discount);
                    $voucher_discount_amount = $price * $voucher_discount/100;
                    }
                    else{
                    $voucher_discount = 0;
                    }
                    $custom_price = $price - $voucher_discount_amount;

                    $item->setCustomPrice($custom_price);

                    $item->setOriginalCustomPrice($custom_price);
                    $info = $item->getOptions();

                $option = Mage::getModel('sales/quote_item_option')
                    ->setProductId($product->getId())
                    ->setCode('voucher_value')
                    ->setProduct($product) // needed for EE only ?
                    ->setValue(39);
                $item->addOption($option); 
                $product = $observer->getProduct();

                $item->setCustomAttribute($product->getCustomAttribute());

                //  $item->setOptions($option);                
                //  $item->setData('voucher_value_value', $price ); */
                } 
    $item->save(); 
    // Enable super mode on the product.
    $item->getProduct()->setIsSuperMode(true);
}

请帮助我如何更改自定义属性集的自定义属性值。

【问题讨论】:

    标签: magento magento-1.9


    【解决方案1】:

    你可以尝试使用:

    $product = Mage::getModel('catalog/product')->load($item->getProductId());
                        $product->setVoucherCode($new_value);
                        $product->save();
    

    【讨论】:

    • setVoucherCode 方法不可用
    • 此自定义属性 (voucher_value) 在后端的下拉框中有价格。我可以使用 setData('voucher_value',$new_value);函数下拉选项中的值之一,但我想设置用户在前端输入的自定义值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2012-07-10
    • 1970-01-01
    相关资源
    最近更新 更多