5

我希望有一个人可以帮助我...

目标是将产品分开放入购物车。因此,如果购物车中有 2 个具有相同 ID 的产品,它们应该单独显示和处理,而不是作为数量为 2 的一个产品/项目。

为此,我使用报价项目选项和事件。

事件:checkout_cart_product_add_after 代码:

     $quoteItem->addOption(new Varien_Object(
       array(
            'product' => $quoteItem->getProduct(),
            'code' => 'unique_id',
           'value' => $uniqueId
      )
  ));

我也尝试过使用addional_options:

   $additionalOptions = array(
            array(
                'code'  => 'unique_id',
                'label' => 'Uniaue ID',
                'value' => $uniqueId
            )
        );
    $quoteItem->addOption(
            array(
                'code'  => 'additional_options',
                'value' => serialize($additionalOptions),
            )
        );

之后 - 一切正常。选项显示在购物车中,它们存储在数据库中(sales_flat_quote_item_option),它们都有自己的 item-id。

但是,如果我编辑一个产品并将其再次放入购物车 - 附加选项会丢失,它们不再在数据库中......这会影响购物车中具有相同 ID 的所有产品......

如果有人可以帮助我,那就太好了...

4

1 回答 1

0

I had them same problem and used a whole different approach. I made a module for it and added the following in config.xml

<global>
    <models>            
        <sales>
            <rewrite>
                <!-- Model -->
                <quote>Companyname_Modulename_Model_Override_Mage_Sales_Model_Quote</quote>
            </rewrite>
        </sales>
...

And created:

app/code/community/Companyname/Modulename/Model/Override/Mage/Sales/Model/Quote.php

With the following code: (slightly edited from the original Magento code)

<?php
class Companyname_Modulename_Model_Override_Mage_Sales_Model_Quote extends Mage_Sales_Model_Quote
{
    /**
     * Adding catalog product object data to quote
     *
     * @param   Mage_Catalog_Model_Product $product
     * @return  Mage_Sales_Model_Quote_Item
     */
    protected function _addCatalogProduct(Mage_Catalog_Model_Product $product, $qty = 1)
    {
        $newItem = false;
        $item = $this->getItemByProduct($product);
        $item = false;
        if (!$item) {
            $item = Mage::getModel('sales/quote_item');
            $item->setQuote($this);
            if (Mage::app()->getStore()->isAdmin()) {
                $item->setStoreId($this->getStore()->getId());
            } else {
                $item->setStoreId(Mage::app()->getStore()->getId());
            }
            $newItem = true;
        }

        /**
         * We can't modify existing child items
         */
        if ($item->getId() && $product->getParentProductId()) {
            return $item;
        }

        $item->setOptions($product->getCustomOptions())
            ->setProduct($product);

        // Add only item that is not in quote already (there can be other new or already saved item
        if ($newItem) {
            $this->addItem($item);
        }

        return $item;
    }
}

This way it forces every product to be added separated to the cart, this is specially nice if you add the option to place comments per product in your cart, then people add the same products with different comments.

I hope it helps someone else :)

于 2015-02-05T09:59:14.340 回答