Magento如何向销售/订单模型添加新属性?

时间:2012-10-23 14:36:46

标签: magento extend

我想在销售/订单中添加一个属性,即Mage_Sales_Model_Order,以存储一些额外的数据。

我发现销售/订单的资源使用普通表。当我保存订单时,它只保存与表中列相匹配的字段。

添加此属性的正确方法是什么?

3 个答案:

答案 0 :(得分:3)

我会推荐以下几个答案的混合。您肯定希望通过config xml将此代码放在安装脚本中,但如果您想利用addAttribute函数,则需要使用Mage_Eav_Model_Entity_Setup作为安装类。所以你的config xml看起来像这样...

<config>
...
    <resources>
        <modulename_setup>
            <setup>
                <module>Your_Module</module>
                <class>Mage_Eav_Model_Entity_Setup</class>
            </setup>               
        </modulename_setup>           
    </resources>
...
</config>

并且您的安装脚本应该看起来像这样

$installer = $this;
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

$installer->startSetup();

$installer->addAttribute('sales_order', 'attributename', array(
            'group'             => 'General',
            'label'             => 'Label frontend',
            'note'              => '',
            'type'              => 'string',    //backend_type
            'input'             => 'text', //frontend_input
            'frontend_class'    => '',
            'source'            => '',
            'backend'           => '',
            'frontend'          => '',
            'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
            'required'          => false,
            'visible_on_front'  => false,
            'apply_to'          => 'simple',
            'is_configurable'   => false,
            'used_in_product_listing'   => false,
            'sort_order'        => 5,
        ));

$installer->endSetup();

请记住,这是未经测试的代码,您可能需要调整一些属性选项,以使其按您需要的方式运行。

答案 1 :(得分:0)

要在订单上添加新属性,请使用此代码,但只需一次。获取任何文件并将代码放在那里,执行一次并删除代码。

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

        $setup->addAttribute('sales_order', 'attributename', array(
            'group'             => 'General',
            'label'             => 'Label frontend',
            'note'              => '',
            'type'              => 'string',    //backend_type
            'input'             => 'text', //frontend_input
            'frontend_class'    => '',
            'source'            => '',
            'backend'           => '',
            'frontend'          => '',
            'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
            'required'          => false,
            'visible_on_front'  => false,
            'apply_to'          => 'simple',
            'is_configurable'   => false,
            'used_in_product_listing'   => false,
            'sort_order'        => 5,
        ));

答案 2 :(得分:0)

按照以下步骤添加自定义属性以进行订购。

修改文件persistent/checkout/onepage/billing.phtml并在此行<?php if ($this->canShip()): ?>

上方添加以下代码
<li class="fields">
    <div class="field">
        <label for="billing:ssn" class="required"><em>*</em><?php echo $this->__('SSN') ?></label>
        <div class="input-box">
            <input type="text" name="custom[ssn]" value="<?php echo $this->htmlEscape($this->getQuote()->getSsn()) ?>" title="<?php echo $this->__('SSN') ?>" class="input-text required-entry" id="billing:ssn" />
        </div>
    </div>
</li>

这会将文本字段添加到billing.phtml

接下来,您需要创建两个表。销售的'sales_quote_custom'和订单的'sales_order_custom'。打开phpmyadmin并确认是否创建了报价和订单表。打开模块config.xml文件并放入

中的标记内
<events>
    <sales_quote_save_before> <!-- Event Called Before Quote Object is saved -->
        <observers>
            <save_before>
                <type>singleton</type>
                <class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
                <method>saveQuoteBefore</method> <!-- name of function -->
            </save_before>
        </observers>
    </sales_quote_save_before>
    <sales_quote_save_after> <!-- Event called After Quote Object is saved -->
        <observers>
            <save_after>
                <type>singleton</type>
                <class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
                <method>saveQuoteAfter</method> <!-- name of function -->
            </save_after>
        </observers>
    </sales_quote_save_after>
    <sales_quote_load_after> <!-- Event called when Quote Object is loaded -->
        <observers>
            <load_after>
                <type>singleton</type>
                <class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
                <method>loadQuoteAfter</method> <!-- name of function -->
            </load_after>
        </observers>
    </sales_quote_load_after>
    <sales_model_service_quote_submit_after> <!-- Event called after order placed -->
        <observers>
            <sales_model_service_quote_submit_after>
                <type>singleton</type>
                <class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
                <method>saveOrderAfter</method> <!-- name of function -->
            </sales_model_service_quote_submit_after>
        </observers>
    </sales_model_service_quote_submit_after>
    <sales_order_load_after> <!-- Event called after Order Object is loaded -->
        <observers>
            <sales_order_load_after>
                <type>singleton</type>
                <class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
                <method>loadOrderAfter</method> <!-- name of function -->
            </sales_order_load_after>
        </observers>
    </sales_order_load_after>
</events>

要获得完整的解决方案,请访问: http://www.demagento.com/tutorial-magento-add-custom-field-to-order-checkout-page/