Skip to content

Commit

Permalink
MAGETWO-83410: Fix #10438: Potential error on order edit page when ad…
Browse files Browse the repository at this point in the history
…dress has extension attributes #11787

 - Merge Pull Request #11787 from joni-jones/magento2:G#10438
 - Merged commits:
   1. 2f0898a
  • Loading branch information
omiroshnichenko committed Nov 20, 2017
2 parents b05a957 + 2f0898a commit 069282b
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 446 deletions.
40 changes: 36 additions & 4 deletions app/code/Magento/Sales/Model/AdminOrder/Create.php
Expand Up @@ -10,9 +10,12 @@

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Model\Metadata\Form as CustomerForm;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\App\ObjectManager;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Item;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\Order;

/**
* Order create model
Expand Down Expand Up @@ -235,6 +238,11 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
*/
private $serializer;

/**
* @var ExtensibleDataObjectConverter
*/
private $dataObjectConverter;

/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param \Magento\Framework\Event\ManagerInterface $eventManager
Expand Down Expand Up @@ -265,6 +273,7 @@ class Create extends \Magento\Framework\DataObject implements \Magento\Checkout\
* @param \Magento\Quote\Model\QuoteFactory $quoteFactory
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @param ExtensibleDataObjectConverter|null $dataObjectConverter
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand Down Expand Up @@ -296,7 +305,8 @@ public function __construct(
\Magento\Sales\Api\OrderManagementInterface $orderManagement,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
ExtensibleDataObjectConverter $dataObjectConverter = null
) {
$this->_objectManager = $objectManager;
$this->_eventManager = $eventManager;
Expand Down Expand Up @@ -328,6 +338,8 @@ public function __construct(
$this->serializer = $serializer ?: ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
parent::__construct($data);
$this->dataObjectConverter = $dataObjectConverter ?: ObjectManager::getInstance()
->get(ExtensibleDataObjectConverter::class);
}

/**
Expand Down Expand Up @@ -514,9 +526,7 @@ public function initFromOrder(\Magento\Sales\Model\Order $order)

$shippingAddress = $order->getShippingAddress();
if ($shippingAddress) {
$addressDiff = array_diff_assoc($shippingAddress->getData(), $order->getBillingAddress()->getData());
unset($addressDiff['address_type'], $addressDiff['entity_id']);
$shippingAddress->setSameAsBilling(empty($addressDiff));
$shippingAddress->setSameAsBilling($this->isAddressesAreEqual($order));
}

$this->_initBillingAddressFromOrder($order);
Expand Down Expand Up @@ -2010,4 +2020,26 @@ protected function _getNewCustomerEmail()

return $email;
}

/**
* Checks id shipping and billing addresses are equal.
*
* @param Order $order
* @return bool
*/
private function isAddressesAreEqual(Order $order)
{
$shippingAddress = $order->getShippingAddress();
$billingAddress = $order->getBillingAddress();
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], OrderAddressInterface::class);
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], OrderAddressInterface::class);
unset(
$shippingData['address_type'],
$shippingData['entity_id'],
$billingData['address_type'],
$billingData['entity_id']
);

return $shippingData == $billingData;
}
}

0 comments on commit 069282b

Please sign in to comment.