From 3e3e834101f8b39bb173912fd7dad6e1c7f2228f Mon Sep 17 00:00:00 2001 From: Marvin Besselsen Date: Thu, 25 May 2023 09:30:31 +0200 Subject: [PATCH 1/2] Fixed issue on profiles without default addresses --- Api/Profile/RepositoryInterface.php | 2 +- Model/Profile/Repository.php | 55 ++++++++++++++++++----------- Observer/SaveAddress.php | 5 +-- etc/db_schema.xml | 6 +++- etc/db_schema_whitelist.json | 5 +-- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/Api/Profile/RepositoryInterface.php b/Api/Profile/RepositoryInterface.php index 50bbd84..411cf33 100755 --- a/Api/Profile/RepositoryInterface.php +++ b/Api/Profile/RepositoryInterface.php @@ -116,7 +116,7 @@ public function save(ProfileData $entity) : ProfileData; * @param bool $forceUpdate * @return mixed */ - public function prepareProfileData($customer, bool $forceUpdate = false); + public function prepareProfileData($customer, bool $forceUpdate = false, $address = null); /** * @param Order $order diff --git a/Model/Profile/Repository.php b/Model/Profile/Repository.php index 5b56009..cddb43b 100755 --- a/Model/Profile/Repository.php +++ b/Model/Profile/Repository.php @@ -181,7 +181,7 @@ public function delete(ProfileData $entity): bool /** * @inheritDoc */ - public function prepareProfileData($customer, bool $forceUpdate = false) + public function prepareProfileData($customer, bool $forceUpdate = false, $address = null) { $customer = $this->customer->load($customer->getId()); $storeId = (int)$customer->getStoreId(); @@ -218,7 +218,7 @@ public function prepareProfileData($customer, bool $forceUpdate = false) $profile->setData( array_merge( $profile->getData(), - $this->collectAddressData($customer) + $this->collectAddressData($customer, $address) ) ); /** @phpstan-ignore-next-line */ @@ -248,28 +248,41 @@ public function create() * @param DataCustomer|Customer $customer * @return array */ - private function collectAddressData($customer): array + private function collectAddressData($customer, $address): array { /** @phpstan-ignore-next-line */ - if (!$customer->getDefaultBilling() || !$customer->getDefaultBillingAddress()) { - return []; + if (!$address && (!$customer->getDefaultBilling() || !$customer->getDefaultBillingAddress())) { + $data = [ + 'firstname' => $customer->getFirstname(), + 'lastname' => $customer->getLastname(), + 'prefix' => $customer->getPrefix(), + 'email' => $customer->getEmail(), + 'gender' => $customer->getAttribute('gender') + ->getSource()->getOptionText($customer->getGender()) + ]; + } else { + if ($customer->getDefaultBillingAddress()) { + $address = $customer->getDefaultBillingAddress(); + } + $data = [ + 'firstname' => $address->getFirstname() ?? $customer->getFirstname(), + 'lastname' => $address->getLastname() ?? $customer->getLastname(), + 'prefix' => $address->getPrefix() ?? $customer->getPrefix(), + 'name' => $this->formatName($address), + 'email' => $customer->getEmail(), + 'company' => $address->getCompany(), + 'country' => $address->getCountry(), + 'city' => $address->getCity(), + 'zip' => $address->getPostcode(), + 'phone' => $address->getTelephone(), + 'region' => $address->getRegion(), + 'street' => $address->getStreetFull(), + 'address' => $this->formatAddress($address), + 'gender' => $customer->getAttribute('gender') + ->getSource()->getOptionText($customer->getGender()) + ]; } - $address = $customer->getDefaultBillingAddress(); - $data = [ - 'firstname' => $address->getFirstname(), - 'lastname' => $address->getLastname(), - 'prefix' => $address->getPrefix(), - 'name' => $this->formatName($address), - 'email' => $customer->getEmail(), - 'company' => $address->getCompany(), - 'country' => $address->getCountry(), - 'city' => $address->getCity(), - 'zip' => $address->getPostcode(), - 'phone' => $address->getTelephone(), - 'region' => $address->getRegion(), - 'street' => $address->getStreetFull(), - 'address' => $this->formatAddress($address) - ]; + return $data; } diff --git a/Observer/SaveAddress.php b/Observer/SaveAddress.php index ced97c9..0e47908 100755 --- a/Observer/SaveAddress.php +++ b/Observer/SaveAddress.php @@ -58,11 +58,12 @@ public function __construct( */ public function execute(Observer $observer) { - $customerId = $observer->getEvent()->getCustomerAddress()->getParentId(); + $address = $observer->getEvent()->getCustomerAddress(); + $customerId = $address->getParentId(); if ($customerId) { try { $customer = $this->customerRepository->getById($customerId); - $this->profileRepository->prepareProfileData($customer, true); + $this->profileRepository->prepareProfileData($customer, true, $address); } catch (\Exception $e) { return; } diff --git a/etc/db_schema.xml b/etc/db_schema.xml index e5f6788..329da7c 100755 --- a/etc/db_schema.xml +++ b/etc/db_schema.xml @@ -296,7 +296,11 @@ length="255" nullable="true" comment="Street"/> - + Date: Thu, 25 May 2023 09:31:36 +0200 Subject: [PATCH 2/2] Version bump --- composer.json | 2 +- etc/config.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 594bb75..246255e 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "datatrics/magento2-integration", "description": "Datatrics Connect extension for Magento 2", "type": "magento2-module", - "version": "1.6.6", + "version": "1.6.7", "license": [ "BSD-2-Clause" ], diff --git a/etc/config.xml b/etc/config.xml index b9eaf8c..cf9292e 100755 --- a/etc/config.xml +++ b/etc/config.xml @@ -10,7 +10,7 @@ - v1.6.6 + v1.6.7 0 Magento 2 0