magento 自定义订单属性

发布于 2024-12-16 17:32:34 字数 653 浏览 1 评论 0原文

我正在尝试为从后端(管理员)面板创建的订单添加一些自定义属性到 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(); 

谢谢..

i'm trying to add some custom attributes to the checkout(sales) page of magento 1.5.1.0 for orders created from the backend (administrator) panel.
I tried this code and i can see my new attribute in the eav_attribute table, but i can't see my new attribute when i make an order from the backend.
What am i missing..?

$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(); 

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2024-12-23 17:32:34

订单实体与普通实体不同,它们使用平面表而不是 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 >,如果我正确阅读源代码)。礼品选项块已在订单的

内创建了一个

,因此仅需要输入字段。

Order entities are awkwardly not like normal entities, they use a flat table instead of EAV, which means that it's not enough to just assign an attribute. You must alter the flat table (which I tried here) or create additional tables then join them to the flat table. With hindsight I see I should have taken the second option, it's safer.


I've been looking a bit closer at the order template and there might a hackish sort of workaround via a lesser used block. To start with let's make changes in a layout/local.xml file so it's safe from overwriting by upgrades;

<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>

The gift options block is constructed in an open-ended way so adding to it is relatively easy. Obviously replace YOUR/TEMPLATE.phtml with the path of a file you will create. The template being inserted needs to have input fields with names like order[testattr] and those should be copied directly to the database table (according to Mage_Adminhtml_Model_Sales_Order_Create, if I am reading the source code right). The gift options block already creates a <fieldset> inside the order's <form> so only the input fields are needed.

甜尕妞 2024-12-23 17:32:34

感谢 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:在结帐中保存自定义地址属性

Thanks clockworkgeek for your replies. I found the solution by using some extra address attributes instead because it was a lot more easier (i had to search a lot though).

so the solution is..

<?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();


?>

and then edit the files according to this post.
Magento: save custom address attribute in checkout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文