11

我想为订单添加一个对客户不可见的属性。我只想在数据库中使用它并为每个订单存储一个特定的值。我想根据这个值打印订单。那么如何在magento中添加订单属性。该属性就像订单状态一样。如果我想在管理员/销售/订单中显示该属性,我该怎么做?

4

1 回答 1

18

假设您要添加my_custom_input_field到您的订单并向管理员创建订单页面添加一个字段(要将字段添加到前端,您只需将输入字段添加到前端模板并仔细检查观察者)

在 /app/code/local/MageIgniter/CustomOrderStatus/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MageIgniter_CustomOrderStatus>
            <version>1.1</version>
        </MageIgniter_CustomOrderStatus>
    </modules>

    <global> 
         <fieldsets>
            <sales_convert_quote>                           
                <my_custom_input_field><to_order>*</to_order></my_custom_input_field>
            </sales_convert_quote>

            <sales_convert_order>                                              
                <my_custom_input_field><to_quote>*</to_quote></my_custom_input_field>
            </sales_convert_order>
        </fieldsets>
        <helpers>
            <customorderstatus>
                <class>MageIgniter_CustomOrderStatus_Helper</class>
            </customorderstatus>            
        </helpers>

        <models>
            <customorderstatus>
                <class>MageIgniter_CustomOrderStatus_Model</class>
                <resourceModel>customorderstatus_mysql4</resourceModel>
            </customorderstatus>
        </models>
        <resources>
            <customorderstatus_setup>
                <setup>
                    <module>MageIgniter_CustomOrderStatus</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </customorderstatus_setup>
            <customorderstatus_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </customorderstatus_write>
            <customorderstatus_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </customorderstatus_read>
        </resources>

        <events>
            <adminhtml_sales_order_create_process_data_before>
                <observers>
                    <customorderstatus>
                        <type>singleton</type>
                        <class>customorderstatus/observer</class>
                        <method>saveCustomData</method>
                    </customorderstatus>
                </observers>
            </adminhtml_sales_order_create_process_data_before>
        </events>

        <blocks>
            <customorderstatus>
                <class>MageIgniter_CustomOrderStatus_Block</class>
            </customorderstatus>
        </blocks>
    </global>
</config>

在 /app/code/local/MageIgniter/CustomOrderStatus/sql/customorderstatus_setup/mysql4-install-1.1.php

<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("order", "my_custom_input_field", array("type"=>"varchar"));
$installer->addAttribute("quote", "my_custom_input_field", array("type"=>"varchar"));
$installer->endSetup();

在 /app/code/local/MageIgniter/CustomOrderStatus/Model/Observer.php

class MageIgniter_CustomOrderStatus_Model_Observer 
{
    public function saveCustomData($event)
    {
        $quote = $event->getSession()->getQuote();
        $quote->setData('my_custom_input_field', $event->getRequestModel()->getPost('my_custom_input_field'));

        return $this;
    }
}

(您应该避免更改核心默认值 - 您应该对此进行一些研究)

在 /app/design/adminhtml/default/default/template/sales/order/view/info.phtml

<?php if($_order->getMyCustomInputField()): ?>
<tr>
    <td class="label"><label><?php echo Mage::helper('sales')->__('My Custom Input Field') ?></label></td>
    <td class="value"><strong><?php echo $_order->getMyCustomInputField() ?></strong></td>
</tr>
<?php endif; ?>

在 /app/design/adminhtml/default/default/template/sales/order/create/form/account.phtml

(添加到前端更改value="<?php echo Mage...>"

<input id="my_custom_input_field" name="my_custom_input_field" value="<?php echo Mage::getSingleton('adminhtml/session_quote')->getQuote()->getMyCustomInputField() ?>" class="input-text" type="text">
于 2012-11-29T22:32:43.257 回答