Magento 1.7 – How to Save Customer Name for Guest Checkout

checkoutmagento-1.7onepage-checkout

Customer name is not being saved in onestepcheckout module. Is there a step I missed in saving order?

$shippingInfo = array(
        'city'=> (string)$shippingAddress->City,
        'country_id' => (string)$shippingAddress->CountryCode,
        'email' => (string)$customerInfo->Email,
        'firstname' => (string)$firstname,
        'lastname' => (string)$lastname,
        'postcode' => (string)$shippingAddress->PostalCode,
        'street' => array(   (string)$shippingAddress->AddressLine1, ),
        'telephone' => (string)$shippingAddress->Phone,
        'use_for_shipping' => '1',
        'name'=>'hello there'
    );
    if(!empty($regionId)){
        $shippingInfo['region_id'] = $regionId;
    }
    else{
        $shippingInfo['region'] = $regionCode;
    }

$quote = $this->getOnepage()->getQuote();  
$quote->collectTotals()->save();
$quote->getBillingAddress()
    ->addData($shippingInfo);

$quote->getShippingAddress()
    ->addData($shippingInfo);
$quote->setCheckoutMethod('guest')
        ->setCustomerId(null)
        ->setCustomerEmail('test@example.com')
        ->setCustomerIsGuest(true)
        ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)->save();
    $this->getOnepage()->saveOrder();
    $this->getOnepage()->getQuote()->save();

enter image description here

Best Answer

Sadly you have not missed a step. In the admin section template /app/design/adminhtml/default/default/template/sales/order/view/info.phtml you can see the call to getCustomerName on the order.

In the order class Mage_Sales_Model_Order the getCustomerName function is as follows:

public function getCustomerName()
{
    if ($this->getCustomerFirstname()) {
        $customerName = $this->getCustomerFirstname() . ' ' . $this->getCustomerLastname();
    }
    else {
        $customerName = Mage::helper('sales')->__('Guest');
    }
    return $customerName;
}

When a guest quote is prepared by the system it never sets the first and last name and so it never gets saved to the order:

protected function _prepareGuestQuote()
{
    $quote = $this->getQuote();
    $quote->setCustomerId(null)
        ->setCustomerEmail($quote->getBillingAddress()->getEmail())
        ->setCustomerIsGuest(true)
        ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
    return $this;
}

So sadly all guest orders will be displayed as guest in the admin section. You could do one of the following things here.

  1. If the code is not in the standard process just use the following $quote->setCustomerFirstname($firstname) and $quote->setCustomerLastname($lastname).
  2. Mirror the front end order email display and take the name set in the billing address. $customerName = $this->getBillingAddress()->getName();
  3. Set the customer first and last name (from the address) on the action sales_order_place_before or sales_convert_quote_to_order