Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/MC-39765' into 2.4-develop-pr115
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaVasyltsun committed Dec 18, 2020
2 parents b250801 + 92ecbbf commit 11c704d
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions app/code/Magento/Persistent/Observer/EmulateCustomerObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Persistent\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Class EmulateCustomer
Expand Down Expand Up @@ -86,9 +87,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId());
if ($defaultShipping = $customer->getDefaultShipping()) {
/** @var \Magento\Customer\Model\Data\Address $address */
$address = $this->addressRepository->getById($defaultShipping);
if ($address) {
$address = $this->getCustomerAddressById((int) $defaultShipping);

if ($address !== null) {
$this->_customerSession->setDefaultTaxShippingAddress(
[
'country_id' => $address->getCountryId(),
Expand All @@ -102,8 +103,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}

if ($defaultBilling = $customer->getDefaultBilling()) {
$address = $this->addressRepository->getById($defaultBilling);
if ($address) {
$address = $this->getCustomerAddressById((int) $defaultBilling);

if ($address !== null) {
$this->_customerSession->setDefaultTaxBillingAddress([
'country_id' => $address->getCountryId(),
'region_id' => $address->getRegion() ? $address->getRegionId() : null,
Expand All @@ -118,4 +120,19 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}
return $this;
}

/**
* Returns customer address by id
*
* @param int $addressId
* @return \Magento\Customer\Api\Data\AddressInterface|null
*/
private function getCustomerAddressById(int $addressId)
{
try {
return $this->addressRepository->getById($addressId);
} catch (NoSuchEntityException $exception) {
return null;
}
}
}

0 comments on commit 11c704d

Please sign in to comment.