按顺序保存自定义属性

时间:2016-04-28 09:17:01

标签: magento attributes save custom-attribute

我无法在订单中获取和设置自定义属性。

我有以下SQL脚本:

<?php

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$attribute = array(
    'position'          => 1,
    'type'              => 'text',
    'input'             => 'varchar',
    'label'             => 'My Order Field',
    'global'            => 1,
    'visible'           => 1,
    'required'          => 0,
    'user_defined'      => 1,
    'searchable'        => 0,
    'filterable'        => 0,
    'comparable'        => 0,
    'visible_on_front'  => 1,
    'visible_in_advanced_search' => 0,
    'unique'            => 0,
    'is_configurable'   => 0,
    'position'          => 1,
);

$setup->addAttribute('order', 'my_order_field', $attribute);

$installer->endSetup();

运行之后,我可以在eav表中看到该属性。

然后我设置了一个观察员,该观察员在保存新订单时运行,我已经确认该订单正在运行(带有die()语句)。

我现在删除了那些die语句并将订单保存代码更改为:

$event = $observer->getEvent();
$order = $event->getOrder();
$order->setMyOrderField('Hello');

在我的管理员后端部分,我添加了一行来检索数据:

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

然而,当我通过什么都没有显示新订单时,是否有人可以告诉我在将数据保存到自定义属性方面我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

<?php

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$attribute = array(
    'position'          => 1,
    'type'              => 'text',
    'input'             => 'varchar',
    'label'             => 'My Order Field',
    'global'            => 1,
    'visible'           => 1,
    'required'          => 0,
    'user_defined'      => 1,
    'searchable'        => 0,
    'filterable'        => 0,
    'comparable'        => 0,
    'visible_on_front'  => 1,
    'visible_in_advanced_search' => 0,
    'unique'            => 0,
    'is_configurable'   => 0,
    'position'          => 1,
);


$setup->addAttribute('order', 'my_order_field', $attribute);
$setup->addAttribute('quote', 'my_order_field', $attribute);

$installer->endSetup();

$event = $observer->getEvent();
$order = $event->getOrder();
$order->setMyOrderField('Hello');
$order->save();