How to convert quote attribute to order in Magento?

attributes, magento, magento-1.6, quote

Solution

On Magento 1.6.2.0, Orders are saved on sales_flat_order & Quotes are saved on sales_flat_quote. They don't use the eav structure anymore AFAIK so I would say it's OK. You should take a look at class Mage_Sales_Model_Convert_Quote and add a debug code :

public function toOrder(Mage_Sales_Model_Quote $quote, $order=null)
{
    if (!($order instanceof Mage_Sales_Model_Order)) {
        $order = Mage::getModel('sales/order');
    }
    /* @var $order Mage_Sales_Model_Order */

    $order->setIncrementId($quote->getReservedOrderId())
        ->setStoreId($quote->getStoreId())
        ->setQuoteId($quote->getId())
        ->setQuote($quote)
        ->setCustomer($quote->getCustomer());

    Mage::helper('core')->copyFieldset('sales_convert_quote', 'to_order', $quote, $order);

    Mage::dispatchEvent('sales_convert_quote_to_order', array('order'=>$order, 'quote'=>$quote));

    //I add my debug code here using Mage::log, you can debug using your own method
    Mage::log($order->getData());

    return $order;
}

To see whether the module_job_id and module_channel_id were set there.

Problem

I've spent two days on this, I feel like I've tried everything,still I keep hitting the wall. I've got two attributes (module_job_id, module_channel_id) that I'd love to add to quote and order. Where I managed to get is that the quote attributes work fine, I can see them stored in database and they can be retrieved fine. The only thing left is moving the values across from quote to order. What am I doing wrong? Here's my module config file: ``` <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Company_Module> <version>0.1.9</version> </Company_Module> </modules> <global> <fieldsets> <sales_convert_quote> <module_job_id> <to_order>*</to_order> </module_job_id> <module_channel_id> <to_order>*</to_order> </module_channel_id> </sales_convert_quote> </fieldsets> <resources> <company_module> <setup> <module>Company_Module</module> <class>Mage_Sales_Model_Mysql4_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </company_module> </resources> </global> </config> ``` The installation file sql/company_module/mysql4-install-0.1.0.php: ``` <?php $installer = $this; $installer->startSetup(); $installer->getConnection()->addColumn($installer->getTable('sales/quote'), 'module_job_id', 'VARCHAR(255) NULL DEFAULT NULL'); $installer->getConnection()->addColumn($installer->getTable('sales/quote'), 'module_channel_id', 'VARCHAR(255) NULL DEFAULT NULL'); $installer->getConnection()->addColumn($installer->getTable('sales/order'), 'module_job_id', 'VARCHAR(255) NULL DEFAULT NULL'); $installer->getConnection()->addColumn($installer->getTable('sales/order'), 'module_channel_id', 'VARCHAR(255) NULL DEFAULT NULL'); $installer->addAttribute('order', 'module_job_id', array('type' => 'varchar')); $installer->addAttribute('quote', 'module_job_id', array('type' => 'varchar')); $installer->addAttribute('order', 'module_channel_id', array('type' => 'varchar')); $installer->addAttribute('quote', 'module_channel_id', array('type' => 'varchar')); $installer->endSetup(); ``` I've tried all possible combinations of addAttribute and addColumns in the installation file. The result is that I've got both attributes as columns in both sales_flat_quote and sales_flat_order. However, none of the attributes is in eav_attribute. I'm not sure if that's OK. One more thing I tried is setting the order attribute values explicitly in the sales_convert_quote_to_order observer. This didn't work: ``` public function salesConvertQuoteToOrder($observer) { $order = $observer->getEvent()->getOrder(); $order->setModuleJobId('123'); $order->setModuleChannelId('456'); } ``` I don't know if it's important, but these are the entity types on my system (only order, no quote...): ``` mysql> SELECT entity_type_id, entity_type_code FROM eav_entity_type; +----------------+------------------+ | entity_type_id | entity_type_code | +----------------+------------------+ | 3 | catalog_category | | 4 | catalog_product | | 7 | creditmemo | | 1 | customer | | 2 | customer_address | | 6 | invoice | | 5 | order | | 8 | shipment | +----------------+------------------+ ``` Also, eav_entity is empty. I hope that's OK, too. ``` mysql> select * from eav_entity; Empty set (0.00 sec) ``` This is on Magento 1.6.2.0. Thanks heaps!

Original source