【问题标题】:Magento - Add custom attribute to orderMagento - 为订单添加自定义属性
【发布时间】:2013-07-11 17:17:12
【问题描述】:

我正在尝试向我的订单添加自定义字段。此时,我发现下面的帖子帮助我在我的数据库中创建了这样的属性: 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();

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

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

谢谢!

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

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

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

    【讨论】:

    • 我现在的问题是:它只是因为我添加了你的 xml 行还是我的也贡献了?
    • 当然...您通过安装程序脚本添加了该属性。我给你的 xml 行通过报价到订单的转换自动推送属性。由于报价也保存在数据库中,您也应该在报价中创建属性!只需检查销售订单报价数据库表即可。
    • 正如你所说,在 sales_order_sales_after 观察者中 get 和 set 都可以正常工作,但是当我尝试这个 $orders = Mage::getResourceModel('sales/order_collection')->addFieldToSelect('*')->addAttributeToFilter ("your_special_attribute", array("finset"=>$customer_id))->setOrder('created_at', 'desc');它不工作。
    • @JeevaRathinam:正确的代码是什么意思?这是关于通过配置 xml 将报价对象中的属性自动传输到订单对象 - 因此不需要进一步的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多