Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert quote attribute to order in Magento?

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!

like image 733
Jan Tomka Avatar asked Jul 26 '12 07:07

Jan Tomka


2 Answers

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.

like image 174
Usman Tiono Avatar answered Oct 02 '22 15:10

Usman Tiono


Please check the following codes which worked for me (however my Magento installation is 1.7.0.2). I think the problem is with your model resource.

app/code/local/Final/Test/etc/config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config>
<modules>
    <Final_Test>
        <version>0.0.1</version>
    </Final_Test>
</modules>
<frontend>
    <routers>
        <test>
            <use>standard</use>
            <args>
                <module>Final_Test</module>
                <frontName>test</frontName>
            </args>
        </test>
    </routers>
</frontend>
<global>
    <models>
        <test>
            <class>Final_Test_Model</class>
            <resourceModel>test_resource</resourceModel>
        </test>
        <test_resource>
            <class>Final_Test_Model_Resource</class>
            <deprecatedNote>test_mysql4</deprecatedNote>
        </test_resource>
    </models>
    <resources>
        <test_setup>
            <setup>
                <module>Final_Test</module>
                <class>Final_Test_Model_Resource_Setup</class>
            </setup>
        </test_setup>
    </resources>
    <fieldsets>
        <sales_convert_quote>
            <final_test1>
                <to_order>*</to_order>
            </final_test1>
            <final_test2>
                <to_order>*</to_order>
            </final_test2>
        </sales_convert_quote>
        <sales_convert_order>
            <final_test1>
                <to_quote>*</to_quote>
            </final_test1>
            <final_test2>
                <to_quote>*</to_quote>
            </final_test2>
        </sales_convert_order>
    </fieldsets>
</global>
</config> 

Your installer should be an instance of Mage_Sales_Model_Resource_Setup so I created my own class which simply extends the aforementioned class

app/code/local/Final/Test/Model/Resource/Setup.php

<?php
    class Final_Test_Model_Resource_Setup extends Mage_Sales_Model_Resource_Setup{

    }
?>

Finally my installer script is sth like this

app/code/local/Final/Test/sql/test_setup/install-0.0.1.php

<?php
$installer=$this;
$installer->startSetup();
$entitiesToAlter = array('quote','order');
$attributes = array(
    'final_test1' => array(
        'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
        'default' =>5
        ),
    'final_test2' => array(
        'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
        'default' =>0
        )
    );

foreach ($entitiesToAlter as $entityName) {
    foreach ($attributes as $attributeCode => $attributeParams) {
        $installer->addAttribute($entityName, $attributeCode, $attributeParams);
    }
}
$installer->endSetup();
?>

you can refer to Mage_Sales_Model_Resource_Setup and Mage_Sales module installer files under it sql folder to get a better understanding of what is going on

like image 28
Ehsan2e Avatar answered Oct 02 '22 16:10

Ehsan2e