Php – Magento save billing address in Quote

magentomagento-1.5PHP

From the beginning I want to apologize for my poor English! I have a task to make the proper storage of information in the customer session in Magento on the page checkout. When i try to save billing address of a guest user i had rewrite billing model and all ok. But when user is logged in and has several address in his address book i saw interesting thing…. I had rewrite billing model more to save a customer address id and save selected address but when user select an option in select "New address", form data save in quote and when i try to get it with getQuote()->getBillingAddress() i took the default user address (when user not logged in all work good) How i can do this task ?? I need help because this important task for me … A lot of thx !!!

Best Answer

If I am parsing your question correctly, are you saying that getQuote()->getBillingAddress() is getting the customer's default billing address instead of the new address the customer has entered in the order?

I have this problem in Magento 1.4.0.1 as well, I worked around it by getting all the addresses from the customer and comparing every attribute with the address specified in the order to find out the real entity ID of the address.

I copied this from my code with some parts of custom business logic removed, so consider it untested, but you get the idea:

(The code is only tested on Magento 1.4.0.1 and might not apply to Magento 1.5)

$currentCustomer = Mage::getModel('customer/customer')->load($order['customer_id']);
$attributesToCompare = array('firstname', 'lastname', 'country_id', 'region', 'region_id', 'city', 'telephone', 'postcode', 'company', 'fax', 'prefix', 'middlename', 'suffix', 'street');
$orderAddresses = array(
  'billing' => $order->getBillingAddress()->getData(),
  'shipping' => $order->getShippingAddress()->getData()
  );
$foundExistingCustomerAddressEntityId = array(
  'billing' => false,
  'shipping' => false
  );
$billingSameAsShipping = false;
$currentCustomerAddresses = $currentCustomer->getAddressesCollection();
// is the billing/shipping address currently found in the customer's address book?
foreach ($orderAddresses as $orderAddressKey => $orderAddress) {
  //var_dump($orderAddress);
  foreach ($currentCustomerAddresses as $currentCustomerAddressObj) {
    $currentCustomerAddress = $currentCustomerAddressObj->getData();
    $attributesMatchCount = 0;
    foreach ($attributesToCompare as $attributeToCompare) {
      if (empty($currentCustomerAddress[$attributeToCompare])) {
        $currentCustomerAddress[$attributeToCompare] = false;
      }
      $attributesMatchCount += ($orderAddress[$attributeToCompare] == $currentCustomerAddress[$attributeToCompare])?1:0;
      //echo 'attributesMatchCount: '.$attributesMatchCount." {$orderAddress[$attributeToCompare]} {$currentCustomerAddress[$attributeToCompare]}\n";
    }
    if ($attributesMatchCount == count($attributesToCompare)) {
      $foundExistingCustomerAddressEntityId[$orderAddressKey] = $currentCustomerAddress['entity_id'];
      //echo 'foundExistingCustomerAddressEntityId['.$orderAddressKey.']: '.$foundExistingCustomerAddressEntityId[$orderAddressKey]."\n\n";
      break;
    }
  }
}
$billingShippingExactMatchCount = 0;
foreach ($attributesToCompare as $attributeToCompare) {
  $billingShippingExactMatchCount += ($orderAddresses['billing'][$attributeToCompare] == $orderAddresses['shipping'][$attributeToCompare])?1:0;
}
if ($billingShippingExactMatchCount == count($attributesToCompare)) {
  $billingSameAsShipping = true;
}
Related Topic