更新时间:2023-11-30 11:59:04
此处需要执行许多步骤才能获得整个解决方案.
There are a number of steps required here to get the whole solution.
首先,创建一个新模块.如果需要,请使用 ModuleCreator .
Firstly, create a new module. Use the ModuleCreator if you wish.
然后,编写模块中的设置脚本,以将新字段添加到Magento的属性结构中,例如:
Then, write a setup script in your module to add the new fields to Magento's attribute structure, e.g. :
$setup = new Mage_Sales_Model_Mysql4_Setup('core_setup');
$setup->startSetup();
$setup->addAttribute('quote', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));
$setup->addAttribute('order', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));
$setup->addAttribute('invoice', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));
$setup->addAttribute('creditmemo', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));
请注意使用Mage_Sales_Model_Mysql4_Setup
将字段添加到相关的sales_flat_quote
和sales_flat_order
表中.
Note the use of the Mage_Sales_Model_Mysql4_Setup
to add the fields to the relevant sales_flat_quote
and sales_flat_order
tables.
现在,在模块的config.xml文件中插入以下值:
Now, insert the following values in your module's config.xml file:
<global>
<fieldsets>
<sales_convert_quote>
<my_attribute>
<to_order>*</to_order>
</my_attribute>
</sales_convert_quote>
<sales_convert_order>
<my_attribute>
<to_cm>*</to_cm>
<to_invoice>*</to_invoice>
</my_attribute>
</sales_convert_order>
</fieldsets>
这将指示Magento将自定义字段的值从报价复制到订单,再复制到发票和credit_memo等.
That will instruct Magento to copy the values of your custom field from quote to order to invoice and credit_memo, etc.
然后,在您自定义的块/控制器代码中,您将能够使用Magento的魔术获取器和设置器来保存这些值.
Then in your custom block/controller code, you will be able to use Magento's magic getters and setters to persist the values.
$oQuote = Mage::getSingleton('checkout/session')->getQuote();
$oQuote->setMyAttribute('some_value');
$oQuote->save();
您应该看到保存在sales_flat_quote
中的新列和值.然后,客户完成结帐后,应将相同的值保存在sales_flat_order
中.
You should see the new column and value saved in sales_flat_quote
. Then once the customer completes checkout, the same value should be saved in sales_flat_order
.
请注意,只需将quote
更改为quote_item
等,即可将上述代码扩展为适用于quote_item
和order_item
,但是,如果要保存在产品上设置的属性值,那么还需要一些额外的工作.
Note that the above code can be extended to work for quote_item
and order_item
by just changing quote
to quote_item
etc, however, if you wish to save attribute values that have been set on your products, then some extra work is required.
将新的XML块插入config.xml中(同样在全局节点中):
Insert a new block of XML into your config.xml (again inside the global node):
<sales>
<quote>
<item>
<product_attributes>
<my_attribute />
</product_attributes>
</item>
</quote>
</sales>
其中my_attribute
是产品型号上的属性代码.这将使my_attribute在链接的产品上可用,因此您可以通过
Where my_attribute
is the attribute code on the product model. That will make the my_attribute available on the linked product, so you can access it via
$oQuoteItem->getProduct()->getMyAttribute()
,无需执行完整的Mage::getModel('catalog/product')->load($oQuoteItem->getProductId())
.这样效率更高.
without needing to perform a full Mage::getModel('catalog/product')->load($oQuoteItem->getProductId())
. This is much more efficient.
然后,您将需要一个观察者来将值从产品对象复制到quote_item对象.因此,在config.xml中声明您的观察者:
Then, you will need an observer to copy the values from the product object to the quote_item object. So, declare your observer in the config.xml:
<events>
<sales_quote_item_set_product>
<observers>
<quoteitem_set_custom_data>
<type>singleton</type>
<class>mymodule/observer</class>
<method>setCustomDataOnQuoteItem</method>
</quoteitem_set_custom_data>
</observers>
</sales_quote_item_set_product>
<events>
并在您的观察者类中编写代码,如下所示:
and write code in your observer class like this:
public function setCustomDataOnQuoteItem($oObserver){
$oProduct = $oObserver->getProduct();
$oQuoteItem = $oObserver->getQuoteItem();
foreach(array('my_attribute') as $vAttributeCode){
$oQuoteItem->setData($vAttributeCode,$oProduct->getData($vAttributeCode));
}
}