1

我正在尝试为从后端(管理员)面板创建的订单添加一些自定义属性到 magento 1.5.1.0 的结帐(销售)页面。我尝试了这段代码,我可以在 eav_attribute 表中看到我的新属性,但是当我从后端下订单时看不到我的新属性。我错过了什么..?

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('5', 'testattr', array(
 'label' => 'testlabel',
 'type' => 'varchar',
 'input' => 'text',
 'visible' => true,
 'required' => false,
 'position' => 5,
 ));


$eavConfig = Mage::getSingleton('eav/config');
$attribute = $eavConfig->getAttribute('5', 'testattr');
$attribute->setData('used_in_forms', array('checkout_register', 'adminhtml_checkout'));
$attribute->save(); 

谢谢..

4

2 回答 2

3

订单实体笨拙地不像普通实体,它们使用平面表而不是 EAV,这意味着仅仅分配一个属性是不够的。您必须更改平面表(我在这里尝试过)或创建其他表,然后将它们连接到平面表。事后看来,我认为我应该采取第二种选择,它更安全。


我一直在仔细研究订单模板,并且可能通过较少使用的块来解决问题。首先让我们在layout/local.xml文件中进行更改,这样它就不会被升级覆盖;

<layout>
    <adminhtml_sales_order_create_index>
        <reference name="gift_options">
            <block type="adminhtml/template" template="YOUR/TEMPLATE.phtml" />
        </reference>
    </adminhtml_sales_order_create_index>
</layout>

礼物选项块是以开放式方式构建的,因此添加它相对容易。显然替换YOUR/TEMPLATE.phtml为您将创建的文件的路径。插入的模板需要具有名称类似的输入字段,order[testattr]并且应将这些字段直接复制到数据库表中(根据Mage_Adminhtml_Model_Sales_Order_Create,如果我正在阅读源代码正确)。<fieldset>礼品选项块已经在订单内部创建了一个,<form>因此只需要输入字段。

于 2011-11-18T15:14:00.993 回答
1

感谢clockworkgeek 的回复。我通过使用一些额外的地址属性找到了解决方案,因为它更容易(尽管我不得不搜索很多)。

所以解决方案是..

<?php 

//Attribute to add
$newAttributeName = "rfc"; //modify this with the name of your attribute

//a) Add EAV Attributes (modify as you needed)
$attribute  = array(
    'type'          => 'varchar',
    'label'         => 'RFC',
    'visible'       => true,
    'required'      => false,
    'user_defined'  => true,
    'searchable'    => false,
    'filterable'    => false,
    'comparable'    => false,
);

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
//Add to customer
$setup->addAttribute('customer_address', $newAttributeName, $attribute);


/* this is not working for some reason. add the columns manually
//b) Add Quote attributes (one page step to step save field)
$setup = new Mage_Sales_Model_Mysql4_Setup('sales_setup');
$setup->getConnection()->addColumn(
        $setup->getTable('sales_flat_quote_address'),
        $newAttributeName,
        'text NULL DEFAULT NULL'
    );
$setup->getConnection()->addColumn(
$setup->getTable('sales_flat_order_address'),
$newAttributeName,
        'text NULL DEFAULT NULL'
);
*/
$eavConfig = Mage::getSingleton('eav/config');
$attribute = $eavConfig->getAttribute('customer_address', $newAttributeName);
$attribute->setData('used_in_forms', array('adminhtml_customer_address',
                'adminhtml_checkout_address')); //'customer_register_address', 'customer_address_edit',
$attribute->save();


?>

然后根据这篇文章编辑文件。 Magento:在结帐时保存自定义地址属性

于 2011-11-25T09:37:31.120 回答