修改购物车中商品的名称


Modify name of item in cart

我正试图将字符串ABC附加到添加到购物车的每个项目的名称中。这应该显示在购物车/结账中,并且可以通过编程访问。

到目前为止,我已经用我的自定义ABC_DEF_CartController重载了Mage_Checkout_CartController,如下所示:

<frontend>
    <routers>
        <checkout>
            <args>
                <modules>
                    <ABC_DEF before='Mage_Checkout'>ABC_DEF</ABC_DEF>
                </modules>
            </args>
        </checkout>
    </routers>
</frontend>

CartController.php过载addAction():

public function addAction(){
    //prepare 
    $cart = $this->_getCart();
    $params = $this->getRequest()->getParams();
    $params['qty']=1;

    try{
        $product = $this->_initProduct();
        $product->setData('qty',1);
        if(!$product){
            //TODO product not found
            return;
        }
        $product->setData('name',$product->getName()."ABC");
        $cart->addProduct($product,$params);
        $cart->save();
        $this->_getSession()->setCartWasUpdated(true);
        Mage::dispatchEvent('checkout_cart_add_product_complete',
                array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
            );   
    } catch (Exception $e) {
        //TODO
        Mage::logException($e);
    }

据我所知,所有基本的产品信息都是从数据库的某个地方重新加载的,但我不知道在哪里或如何使更改持久化。提前谢谢。

我认为您最好挂接事件sales_quote_item_set_product。此事件在Mage_Sales_Model_Quote_Item::setProduct()中调度
在调度事件之前,有以下线路:

->setName($product->getName()) 

因此,我假设您可以在自定义观察器中更改我提到的事件的名称
该事件接收当前报价项目和分配给它的产品作为参数。