我有以下Magento观察者,当' checkout_controller_onepage_save_shipping_method '执行例如当用户结帐并点击提交的运输方式'
时我的观察者功能如下所示:
public function afterShippingMethod($observer)
{
$org_id = Mage::getSingleton('customer/session')->getCustomerOrganisationId(); // always a number eg. 12345
Mage::getSingleton('customer/session')->getCustomer()->setOrganisationId($org_id)->save();
}
此函数在正确的时间运行,但是给我一个Magento错误:
There has been an error processing your request
Customer email is required
显然,错误需要提供电子邮件地址,但由于客户已在结算信息'中输入了电子邮件地址。阶段我无法理解为什么我会得到这个?
这是堆栈跟踪:
Trace:
#0 /vagrant/site.com/public_html/app/code/core/Mage/Customer/Model/Resource/Customer.php(76): Mage::exception('Mage_Customer', 'Customer email ...')
#1 /vagrant/site.com/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1122): Mage_Customer_Model_Resource_Customer->_beforeSave(Object(Mage_Customer_Model_Customer))
#2 /vagrant/site.com/public_html/app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Customer_Model_Customer))
#3 /vagrant/site.com/public_html/app/code/local/site/Raptor/Model/Raptor/Observer.php(25): Mage_Core_Model_Abstract->save()
#4 /vagrant/site.com/public_html/app/code/core/Mage/Core/Model/App.php(1338): site_Raptor_Model_Raptor_Observer->afterShippingMethod(Object(Varien_Event_Observer))
#5 /vagrant/site.com/public_html/app/code/core/Mage/Core/Model/App.php(1317): Mage_Core_Model_App->_callObserverMethod(Object(site_Raptor_Model_Raptor_Observer), 'afterShippingMe...', Object(Varien_Event_Observer))
#6 /vagrant/site.com/public_html/app/Mage.php(447): Mage_Core_Model_App->dispatchEvent('checkout_contro...', Array)
#7 /vagrant/site.com/public_html/app/code/core/Mage/Checkout/controllers/OnepageController.php(389): Mage::dispatchEvent('checkout_contro...', Array)
#8 /vagrant/site.com/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(419): Mage_Checkout_OnepageController->saveShippingMethodAction()
#9 /vagrant/site.com/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('saveShippingMet...')
#10 /vagrant/site.com/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#11 /vagrant/site.com/public_html/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#12 /vagrant/site.com/public_html/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#13 /vagrant/site.com/public_html/en-us/index.php(87): Mage::run('en_us', 'store')
堆栈跟踪中的实际错误在Customer \ Model \ Resource \ Customer.php
中如下 if (!$customer->getEmail()) {
throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
}
一切都是错误的自我解释,但我无法弄清楚如何能够将organisation_id保存到这个客户对象 - 任何想法?
任何想法为什么会发生这种情况&如何修复错误?
答案 0 :(得分:1)
我认为这仅适用于新客户或来宾。看看如何在Mage_Customer_Model_Session::getCustomer()
中检索客户。如果客户没有ID,那么您将获得一个没有数据的空客户。来自结算信息步骤的数据尚未保存到数据库,因此此时没有电子邮件地址。
由于在上一个结帐步骤中生成了新客户,因此在创建订单时,必须在请求之间存储客户信息。这在报价表中完成。因此,您必须为organisation_id添加另一个引用属性,以便可以存储直到生成客户。要将organisation_id从quote引用转换为customer,需要在模块config.xml中添加一些额外的XML配置:
<global>
<fieldsets>
<checkout_onepage_quote>
<organisation_id>
<to_customer>organisation_id</to_customer>
</organisation_id>
</checkout_onepage_quote>
</fieldsets>
</global>
有关详细信息,请参阅Mage_Checkout_Model_Type_Onepage::saveOrder()
:
/**
* Create order based on checkout type. Create customer if necessary.
*
* @return Mage_Checkout_Model_Type_Onepage
*/
public function saveOrder()
{
$this->validate();
$isNewCustomer = false;
switch ($this->getCheckoutMethod()) {
case self::METHOD_GUEST:
$this->_prepareGuestQuote();
break;
case self::METHOD_REGISTER:
$this->_prepareNewCustomerQuote();
$isNewCustomer = true;
break;
default:
$this->_prepareCustomerQuote();
break;
}
对于新客户,字段转换在
中进行 Mage_Checkout_Model_Type_Onepage::_prepareNewCustomerQuote()
这一行:
Mage::helper('core')->copyFieldset('checkout_onepage_quote', 'to_customer', $quote, $customer);
答案 1 :(得分:0)
试试这个:
public function afterShippingMethod($observer){
$customer=Mage::getSingleton('customer/session');
$orgId=$customer->getCustomerOrganisationId();
$customer->getCustomer()->
setOrganisationId($orgId)->save();
}
答案 2 :(得分:0)
结帐时会触发一个事件:&#39; sales_convert_quote_to_order &#39;
可以在任何模块的 config.xml 中配置此事件:
<events>
<sales_convert_quote_to_order>
<observers>
<MODULE_sales_convert_quote_to_order>
<type>singleton</type>
<class>MODULE_Model_Observer</class>
<method>saveCohorts</method>
</MODULE_sales_convert_quote_to_order>
</observers>
</sales_convert_quote_to_order>
</events>
检查模块,也许你可以找到问题