如何检查账单和送货地址是否相等?


16

我正在尝试在创建发票PDF时检查账单和收货地址是否相等。

到目前为止,我尝试过的是:

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

要么

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

但都行不通。我也试图通过使用来获取报价,$order->getQuote()但这也不起作用。

有什么方法可以检查账单地址和送货地址是否相等?

Answers:


10

使用array_diff。

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

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

参考:http : //us3.php.net/array_diff

您可能需要在比较之前去除每个数组的一些数据。我相信你可以解决;)


2
尽管这在几乎所有实际情况下都行得通,但这应该是array_diff_assoc因为array_diff仅比较值,因此,例如,如果您切换名字和姓氏,则地址仍将被视为相等
Fabian Schmengler,2015年

7

即使已经有一个可接受的答案,我还是希望在第三方模块中分享我看到的(类似)此解决方案:

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,...
            )
        );
}

然后称为:

$shippingAddress = $invoice->getShippingAddress();

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

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

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

5

好的,这是我遵循ProxiBlue的建议的尝试:

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

基本上,我是通过使用$excludeKeys数组剥离一些键的,因此array_diff将仅比较相关数据。为了去除几个键而不必创建循环,我array_diff_key结合使用array_flip来摆脱不必要的数组键。

改进和反馈欢迎。:)


1
尽管这在几乎所有实际情况下都可行,但这是array_diff_assoc因为array_diff仅比较值,因此,例如,如果您切换名字和姓氏,则地址仍将被视为相等。
Fabian Schmengler,2015年

3

您需要使用获取报价

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

然后,您可以从报价单中获取送货地址,并检查其是否标记为与帐单地址相同:

if($quote->getShippingAddress()->getSameAsBilling()){
    // do stuff
}

它在哪里标记为相同?似乎它对我不起作用,因为即使默认地址明显不同,客户也总是将其标记为相同。
TheKitMurkit '19

0

必须比较地址-其中一个是刚刚创建的(未保存)。也许它可以帮助某人:

基于@Alphawolf的答案:

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

编辑20 01 2016

自从我发布上面的版本以来,我使用以下方法,它对我有用-以为它可能对某人有帮助:

/**
 * 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);
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.