سؤال

I have the following Magento observer which is active when the 'checkout_controller_onepage_save_shipping_method' is executed e.g When a user checkouts and clicks submit on the 'Shipping Method'

My observer functions looks the following:

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

This functions runs at the correct time but gives me an Magento error:

There has been an error processing your request
Customer email is required

Clearly the email address is required from the error, but as the email address has already been entered by the customer at the 'Billing Information' stage I cannot understand why I would get this?

Here is stack trace :

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

The actual error from the stack trace is the following in Customer\Model\Resource\Customer.php

   if (!$customer->getEmail()) {
        throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
    }

Everything is pretty self explanatory with the error, but I cannot work out how to be able to save the organisation_id to this customer object - any ideas?

Any ideas why this would happen & how to fix the error?

هل كانت مفيدة؟

المحلول

I think this applies only for new customers or guests. Have a look at how you retrieve the customer in Mage_Customer_Model_Session::getCustomer(). If the customer has no id, you get an empty customer objcet wihtout data. The data from the billing information step wasn't saved to database yet, so there is no email address at this point.

As a new customer is generated in the last checkout step, when the order is created, customer information has to be stored between the requests. This is done in the quote tables. So you have to add another quote attribute for organisation_id so it can be stored till the customer is generated. For the conversion of the organisation_id from quote to customer some additinal XML configuration in your module config.xml is needed:

<global>
   <fieldsets>
      <checkout_onepage_quote>
         <organisation_id>
            <to_customer>organisation_id</to_customer>
         </organisation_id>
      </checkout_onepage_quote>
   </fieldsets>
</global>

For further reference see 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;
}

For new customers the field conversion takes place in

Mage_Checkout_Model_Type_Onepage::_prepareNewCustomerQuote() with this line:

Mage::helper('core')->copyFieldset('checkout_onepage_quote', 'to_customer', $quote, $customer);

نصائح أخرى

Try this:

public function afterShippingMethod($observer){
    $customer=Mage::getSingleton('customer/session');
    $orgId=$customer->getCustomerOrganisationId();
    $customer->getCustomer()->
    setOrganisationId($orgId)->save();
}

An event is fired in the checkout: 'sales_convert_quote_to_order'

this event can be configured in the config.xml of any module like this:

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

check the modules and maybe you can found the issue

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top