我正在尝试在Magento中为我的引用添加一个属性,但我遇到了一些麻烦。我已经对另一个模块做了同样的事情,它就像一个魅力,所以我真的很无奈这次错了。
在我的Magento页面上,当客户点击“继续结帐”时,将调用静态CMS页面。在这个CMS页面上,他可以选择添加一些信息,这些信息应该添加到报价中,然后再订购。
表单提交后,此代码将被执行:
$quote = Mage::getModel('checkout/session')->getQuote();
$quote->setfirstname($firstname);
$quote->setlastname($lastname);
$quote->setlocation($location);
$quote->save();
表sales_flat_quote和sales_flat_order已针对字段firstname,lastname,location进行了扩展。
同样在我的模块config.xml中添加了此代码:
<global>
<fieldsets>
<sales_convert_quote>
<firstname>
<to_order>*</to_order>
</firstname>
</sales_convert_quote>
<sales_convert_quote>
<lastname>
<to_order>*</to_order>
</lastname>
</sales_convert_quote>
<sales_convert_quote>
<location>
<to_order>*</to_order>
</location>
</sales_convert_quote>
</fieldsets>
但是,在下订单时,其他字段不会出现在sales_flat_quote或sales_flat_order中。缺少什么或我做错了什么?
答案 0 :(得分:0)
您必须编写一些事件观察器来捕获报价保存和加载事件。因此,打开您的模块config.xml
文件并在以下代码中添加的标记内。
<events>
<sales_quote_save_before> <!-- Event Called Before Quote Object is saved -->
<observers>
<save_before>
<type>singleton</type>
<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
<method>saveQuoteBefore</method> <!-- name of function -->
</save_before>
</observers>
</sales_quote_save_before>
<sales_quote_save_after> <!-- Event called After Quote Object is saved -->
<observers>
<save_after>
<type>singleton</type>
<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
<method>saveQuoteAfter</method> <!-- name of function -->
</save_after>
</observers>
</sales_quote_save_after>
<sales_quote_load_after> <!-- Event called when Quote Object is loaded -->
<observers>
<load_after>
<type>singleton</type>
<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
<method>loadQuoteAfter</method> <!-- name of function -->
</load_after>
</observers>
</sales_quote_load_after>
<sales_model_service_quote_submit_after> <!-- Event called after order placed -->
<observers>
<sales_model_service_quote_submit_after>
<type>singleton</type>
<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
<method>saveOrderAfter</method> <!-- name of function -->
</sales_model_service_quote_submit_after>
</observers>
</sales_model_service_quote_submit_after>
<sales_order_load_after> <!-- Event called after Order Object is loaded -->
<observers>
<sales_order_load_after>
<type>singleton</type>
<class>Excellence_Custom_Model_Observer</class> <!-- Over Model Class -->
<method>loadOrderAfter</method> <!-- name of function -->
</sales_order_load_after>
</observers>
</sales_order_load_after>
</events>
接下来,您需要在Model文件夹中创建Observer类。因此,在模块模型文件夹中创建Observer.php
。
<?php
class Excellence_Custom_Model_Observer{
public function saveQuoteBefore($evt){
$quote = $evt->getQuote();
$post = Mage::app()->getFrontController()->getRequest()->getPost();
if(isset($post['custom']['ssn'])){
$var = $post['custom']['ssn'];
$quote->setSsn($var);
}
}
public function saveQuoteAfter($evt){
$quote = $evt->getQuote();
if($quote->getSsn()){
$var = $quote->getSsn();
if(!empty($var)){
$model = Mage::getModel('custom/custom_quote');
$model->deteleByQuote($quote->getId(),'ssn');
$model->setQuoteId($quote->getId());
$model->setKey('ssn');
$model->setValue($var);
$model->save();
}
}
}
public function loadQuoteAfter($evt){
$quote = $evt->getQuote();
$model = Mage::getModel('custom/custom_quote');
$data = $model->getByQuote($quote->getId());
foreach($data as $key => $value){
$quote->setData($key,$value);
}
}
public function saveOrderAfter($evt){
$order = $evt->getOrder();
$quote = $evt->getQuote();
if($quote->getSsn()){
$var = $quote->getSsn();
if(!empty($var)){
$model = Mage::getModel('custom/custom_order');
$model->deleteByOrder($order->getId(),'ssn');
$model->setOrderId($order->getId());
$model->setKey('ssn');
$model->setValue($var);
$order->setSsn($var);
$model->save();
}
}
}
public function loadOrderAfter($evt){
$order = $evt->getOrder();
$model = Mage::getModel('custom/custom_order');
$data = $model->getByOrder($order->getId());
foreach($data as $key => $value){
$order->setData($key,$value);
}
}
}