16

I'm trying to check if billing and shipping address are equal when creating invoice PDFs.

What I've tried so far was:

$order = $invoice->getOrder();
if( $order->getBillingAddress()->getData() != $order->getShippingAddress()->getData() )

or

$order = $invoice->getOrder();
if( $order->getShippingAddress()->getData('same_as_billing')!='1' )

but neither works. I was also trying to get the quote by using $order->getQuote() but that didn't work either.

Is there any way to check if billing and shipping address are equal?

10

Use array_diff.

$order = $invoice->getOrder();
$billing = $order->getBillingAddress()->getData();
$shipping = $order->getShippingAddress()->getData();

$diff = array_diff($billing,$shipping);

ref: http://us3.php.net/array_diff

you may have to strip out some of the data of each array, before the diff. I am sure you can work it out ;)

  • 2
    While this will work in almost all practical cases, it should be array_diff_assoc because array_diff only compares the values, so if you for example switch first name and last name, the addresses will still be treated as equal – Fabian Schmengler Aug 10 '15 at 14:37
7

Even though there is already an accepted answer, I'd like to share this solution I saw (similar) once in a 3rd party module:

function serializeAddress(Mage_Sales_Model_Quote_Address $address)  {  
        return serialize(
            array(
                 'firstname' => $address->getFirstname(),
                 'lastname'  => $address->getLastname(),
                 'street'    => $address->getStreet(),
                 'city'      => $address->getCity(),
                 'postcode'  => $address->getPostcode(),
                 //add the attributes you want to check for here for ex. company,...
            )
        );
}

Which was then called:

$shippingAddress = $invoice->getShippingAddress();

if (!$shippingAddress->getSameAsBilling()) {

     $shippingData = $this->serializeAddress($shippingAddress);
     $billingData = $this->serializeAddress($invoice->getBillingAddress());

     if (strcmp($shippingData, $billingData) != 0) {
        return false;
     }
}
5

Okay, so here's my attempt following ProxiBlue's suggestion:

$excludeKeys = array('entity_id', 'customer_address_id', 'quote_address_id', 'region_id', 'customer_id', 'address_type');
$oBillingAddress = $order->getBillingAddress()->getData();
$oShippingAddress = $order->getShippingAddress()->getData();
$oBillingAddressFiltered = array_diff_key($oBillingAddress, array_flip($excludeKeys));
$oShippingAddressFiltered = array_diff_key($oShippingAddress, array_flip($excludeKeys));

$addressDiff = array_diff($oBillingAddressFiltered, $oShippingAddressFiltered);

if( $addressDiff ) { // billing and shipping addresses are different
    // Print stuff
}

Basically I'm stripping out some keys by using an $excludeKeys array, so array_diff will be comparing only the relevant data. To strip out several keys without having to create a loop, I'm using array_diff_key in combination with array_flip to get rid of the unnecessary array keys.

Improvements and feedback welcome. :)

  • 1
    While this will work in almost all practical cases, it should be array_diff_assoc because array_diff only compares the values, so if you for example switch first name and last name, the addresses will still be treated as equal. – Fabian Schmengler Aug 10 '15 at 14:32
3

You need to get the quote using

$order = $invoice->getOrder();
$quote = Mage::getModel('sales/quote')->load($order->getQuoteId());

Then you can get the shipping address from the quote and check if it is marked as being the same as the billing address:

if($quote->getShippingAddress()->getSameAsBilling()){
    // do stuff
}
  • Where is it being marked as the same? It seems like it is not working for me, because customer always has it marked the same even if default addresses are clearly different. – TheKitMurkit Feb 14 at 11:49
0

had to compare addresses - one of them was just created (not saved). Perhaps it helps somebody:

based on @Alphawolf answer:

public function isDifferentAddresses(Mage_Customer_Model_Address $adr1, Mage_Customer_Model_Address $adr2, array $excludeKeys = array())
{
    if (!count($excludeKeys)) {
        $excludeKeys = array(
            'entity_id',
            'entity_type_id',
            'attribute_set_id',
            'is_active',
            'increment_id',
            'parent_id',
            'created_at',
            'updated_at',
            'customer_id',
            'customer_address_id',
            'quote_address_id',
            'region_id',
            'address_type',
            'is_default_billing',
            'is_default_shipping',
            'save_in_address_book'
        );
    }
    $excludeKeys = array_flip($excludeKeys);
    $adr1Filtered = array_diff_key($adr1->getData(), $excludeKeys);
    $adr2Filtered = array_diff_key($adr2->getData(), $excludeKeys);
    $diff = array_diff_assoc($adr1Filtered, $adr2Filtered); 
    return !empty($diff);
}

EDIT 20 01 2016

im using the following method since i posted the version above and it works for me -- thought it may help somebody:

/**
 * returns if address 1 is different to address 2
 *
 * @param Mage_Customer_Model_Address $adr1
 * @param Mage_Customer_Model_Address $adr2
 * @param array                       $excludeKeys
 *
 * @return bool
 */
public function isDifferentAddresses(Mage_Customer_Model_Address $adr1,
    Mage_Customer_Model_Address $adr2, array $excludeKeys = array()
) {
    if (!count($excludeKeys)) {
        $excludeKeys = array(
            'prefix',
            'suffix',
            //'region',
            //'region_id',
            'entity_id',
            'vat_id',
            'entity_type_id',
            'attribute_set_id',
            'is_active',
            'increment_id',
            'parent_id',
            'created_at',
            'updated_at',
            'customer_id',
            'customer_address_id',
            'quote_address_id',
            'address_type',
            'is_default_billing',
            'is_default_shipping',
            'save_in_address_book'
        );
    }
    $excludeKeys = array_flip($excludeKeys);
    $adr1Filtered = array_diff_key($adr1->getData(), $excludeKeys);
    $adr2Filtered = array_diff_key($adr2->getData(), $excludeKeys);
    $diff = array_diff_assoc($adr1Filtered, $adr2Filtered);
    return !empty($diff);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.