Magento – Reorder problem with custom quote attribute

cartdatabasemagento-1.7

I've defined a custom attribute called productcomment on sales_flat_quote_item and in sales_flat_order_item and the problem is when i want to reorder an specific order, the values are not copied.

The quote attribute is saved with the product when i click "add to cart" or with an specific button through ajax.

my config.xml:

<fieldsets>
        <sales_convert_quote_item>
            <productcomment>
                <to_order_item>*</to_order_item>
            </productcomment>
        </sales_convert_quote_item>
        <sales_convert_order_item>
            <productcomment>
                <to_quote_item>*</to_quote_item>
            </productcomment>
        </sales_convert_order_item>
</fieldsets>

The first part (sales_convert_quote_item) works like a charm, but the second one, don't.

Magento executes this code to process a reorder:

public function addOrderItem($orderItem, $qtyFlag=null)
    {
        /* @var $orderItem Mage_Sales_Model_Order_Item */
        if (is_null($orderItem->getParentItem())) {
            $product = Mage::getModel('catalog/product')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($orderItem->getProductId());
            if (!$product->getId()) {
                return $this;
            }

            $info = $orderItem->getProductOptionByCode('info_buyRequest');
            $info = new Varien_Object($info);
            if (is_null($qtyFlag)) {
                $info->setQty($orderItem->getQtyOrdered());
            } else {
                $info->setQty(1);
            }

            $this->addProduct($product, $info);
        }
        return $this;
    }

The problem with this, is that magento uses

$product = Mage::getModel('catalog/product')

and don't care about the $orderItem which already has the data

Which do you think is the best solution to this problem?

edit:
I've already tried solving it with these events:

  • checkout_cart_product_add_after : don't have older order data

  • sales_convert_order_item_to_quote_item : only fired on
    adminhtml

Best Answer

I would try a slightly different approach. Instead of adding your own field you can use Magento's product options. This will be saved as part of the info_buyRequest and will be part of a reorder as well.

I am currently not sure when you save your comments. The product option needs to be added before the product is added to the cart.

This event might help:

    $eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode);
    Mage::dispatchEvent($eventName, array(
        'transport'   => $transport,
        'buy_request' => $buyRequest,
        'product' => $product
    ));

with $processMode='full'; you can then add your comment as a custom option:

$product->addCustomOption('fooman_product_comment', 'The Comment!'); 

Something to consider - The product option will automatically be displayed everywhere.