如何在magento中添加购物车中的自定义数据

时间:2017-01-02 06:46:00

标签: php magento

我想在购物车项目中保存服装数据,我已将检查数据保存在数据库中,但当我收到时,它将返回null

我已添加将服装数据添加到购物车中的事件。

Observer.php

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer){
    try {
        $data = $this->_getRequest()->getPost();
        $item = $observer->getEvent()->getQuoteItem();
        $item->setData('customize_data', $data['customize_data']);
        $item->setData('customize_image', $data['customize_image']);
        $item->save();
    }
    catch (Exception $e) {
        Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
    }
}

我想更改购物车页面中的图片,以便创建以下文件。

<?php
class ProductCustomizer_ProductCustomizer_Block_Checkout_Cart_Item_Renderer extends Mage_Checkout_Block_Cart_Item_Renderer{
    public function getProductThumbnail()
    {
        $customize_data = $this->getItem()->getData('customize_data');
        $customize_image = $this->getItem()->getData('customize_image');
        Mage::log('customize_data');
        Mage::log($customize_data);
        Mage::log('customize_image');
        Mage::log($customize_image);
        if (!empty($customize_image)) {
            return $customize_image;
        } else {
            return parent::getProductThumbnail();
        }
    }
}

我在 system.log 文件

中获得了以下日志
2017-01-02T06:38:29+00:00 DEBUG (7): customize_data
2017-01-02T06:38:29+00:00 DEBUG (7): 
2017-01-02T06:38:29+00:00 DEBUG (7): customize_image
2017-01-02T06:38:29+00:00 DEBUG (7): 

1 个答案:

答案 0 :(得分:2)

您可以在不在项目表中添加新列的情况下执行此操作,

<强> Observer.php

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer){
    try {
            $data = Mage::app()->getRequest()->getPost();

            $item = $observer->getQuoteItem();

            $additional_info = $item->getadditional_info();
            $additional_info = unserialize($additional_info);

            $additional_info['customize']['customize_data'] = $data['customize_data'];
            $additional_info['customize']['customize_image'] = $data['customize_image'];

            $item->setAdditionalInfo(serialize($additional_info));

            $item->save();
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
}

CART页面:

class ProductCustomizer_ProductCustomizer_Block_Checkout_Cart_Item_Renderer extends Mage_Checkout_Block_Cart_Item_Renderer{
    public function getProductThumbnail()
    {
     $additional_info = $this->getItem()->getData('additional_info');
     $additional_info = unserialize($additional_info);
     if(isset($additional_info['customize']) && $additional_info['customize']){
        Mage::log('customize_data');
        Mage::log($additional_info['customize']['customize_data']);
        Mage::log('customize_image');
        Mage::log($additional_info['customize']['customize_image']);

        return $additional_info['customize']['customize_image'];
     }
     return parent::getProductThumbnail();
   }
}