Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #10438: Potential error on order edit page when address has extension attributes #11787

Merged
merged 1 commit into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions app/code/Magento/Sales/Model/AdminOrder/Create.php
Original file line number Diff line number Diff line change
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, update docblock accordingly with the parameters that you accept.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

) {
$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;
}
}
Loading