Magento - 为订单添加自定义属性

时间:2013-07-11 17:17:13

标签: magento magento-1.7

我正在尝试为我的订单添加自定义字段。此时,我找到了帮助我在数据库中创建此类属性的帖子: http://fabrizioballiano.net/2011/11/15/create-a-custom-order-attribute-in-magento/

require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$installer = new Mage_Sales_Model_Mysql4_Setup;
$attribute  = array(
   'type'          => 'int',
   'backend_type'  => 'text',
   'frontend_input' => 'text',
   'is_user_defined' => true,
   'label'         => 'My Label',
   'visible'       => true,
   'required'      => false,
   'user_defined'  => true,
   'searchable'    => true,
   'filterable'    => true,
   'comparable'    => true,
   'default'       => 0
);
$installer->addAttribute('order', 'special_attribute', $attribute);
$installer->endSetup();

执行上面的代码并创建多个订单后,我可以遍历所有订单并查看每个订单的默认值。

问题是,如何在此字段中存储我想要的数据?我该如何检索这些数据?

谢谢!

2 个答案:

答案 0 :(得分:28)

将其添加到config.xml中的gobal范围。然后只需在报价中设置属性 - 它就会自动转移到报价到订单转换过程中的订单。

<global>
...
    <fieldsets>
        <sales_convert_quote>
            <your_special_attribute>
                <to_order>*</to_order>
            </your_special_attribute>
        </sales_convert_quote>
    </fieldsets>
...
</global>

您可以随时通过魔术getter / setter检索/设置属性,例如

$quote->getYourSpecialAttribute()
$order->getYourSpecialAttribute()

$quote->setYourSpecialAttribute()

答案 1 :(得分:0)

将文本字段添加到billing.phtml文件并将字段保存在报价和订单表中后,您可以显示该属性。您可以在“我的帐户”中显示该字段 - &gt;查看订单。在custom.xml fie中进行以下更改。

<?xml version="1.0"?>
<layout version="0.1.0">
    <sales_order_view>
        <reference name="my.account.wrapper">
            <block type="custom/custom_order" name="custom.order" template="custom/order.phtml" after='sales.order.info' />
        </reference>
    </sales_order_view>
</layout>
相关问题