简体   繁体   中英

Custom Magento Order Field and the Magento Database

Alright, so I've successfully added a custom order field to my page layout in magento. I require a "dealer" from which the user has to select a "dealer state" and then I use ajaxt to pull in all the dealers from that specific state into another field called "dealerid". The value of this field is in fact the dealer id in my database. My question is kind of a full one.

First, I need to add a field to the "order table" that saves the dealer id for the specific order and secondly I need to actually have magento take this field from the form and insert it. How do I go about doing these things? Mainly, what table would magento store this dealerid and what files do I modify to actually have magento access and insert this field value?

Note: I am using the one page checkout method only.

First of all, familiarize yourself with magento SQL update mechanism (its diffrent when your dealing with EAV - like customer tables - and regular tables - like orders). Create a module which will have a sql-update script in /YourNamespace/YourModule/sql/yourmodule_setup/mysql4-install-1.0.0.php for instance. You should use something like:

$installer = $this;
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'dealerid', 'decimal(12,4) NULL');

and in your config.xml:

<global>
(...)
       <models>
       (...)
           <yourmodule>
               <class>Mage_Sales_Model</class>
               <resourceModel>yourmodule_mysql4</resourceModel>
           </yourmodule>
           <yourmodule_mysql4>
               <class>YourNamespace_Yourmodule_Model_Mysql4</class>
               <entities>
                   <order><table>sales_order</table></order>
               </entities>
           (...)
           </yourmodule_mysql4>
       (...) 
       </models>
       <resources>
            <yourmodule_setup>
                <setup>
                    <module>YourNamespace_YourModule</module>
                    <class>YourNamespace_YourModule_Model_Mysql4_Setup</class>
                </setup>
            </yourmodule_setup>
        </resources>
(...)
</global>

Remember to create your own classes: YourNamespace_YourModule_Model_Mysql4_Setup, YourNamespace_Yourmodule_Model_Mysql4. Check in app/code/core/Mage/Sales/ how they should look like (nothing special needed there, they just must inherit from proper core classes). Also check out the app/code/core/Mage/Sales/sql and app/code/core/Mage/Sales/etc/config.xml.

If you want to get a field from the onepage checkout, you must do a couple of things... I don't know what you really need from the user (and in what part of checkout its going to be taken). Adding an input is done by the templates (edit proper files in app/design/frontend/yourskin/default/template/checkout).

To capture the right data you must rewrite the checkout controller whit your own controller action (look at the app/code/core/Mage/Checkout/Controllers/OnepageController.php) to get the data from the form and store it in the checkout session. After that you must hook to the checkout_type_onepage_save_order_after event (its in the app/code/core/Mage/Checkou/Model/Type/Onepage.php), take the data from your session and put it in the Order model (and save it of course). Its kind of a standard procedure, so you shouldn't have a problem.

Its possible that you will also have add a column to the sales_flat_order (the structure of order tables changed in 1.4.xx, so I'm not sure of it - you will have to do some experimenting).

If you want to see it in the adminhtml order view, you will have to override some Core/Adminhtml/Blocks/Sales/Order (and templates probably).

Its kind of a complex procedure, so you'll have to figure out some things by yourself. This post is kind of a general idea how it should be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM