From 3cded2096cbb67ba80ab1470a698b905377ea31e Mon Sep 17 00:00:00 2001 From: Bnaya Date: Sun, 6 Dec 2020 12:05:53 +0200 Subject: [PATCH] 2.4.5 - always get customer address from default billing address unless selected otherwise on store settings --- Block/Adminhtml/Settings/Form.php | 4 ++ Controller/Adminhtml/Settings/Index.php | 4 ++ Helper/ConfigHelper.php | 26 +++++++++++++ Helper/Data.php | 34 ++++++++++++++++- Model/Api/Data.php | 38 ++++--------------- .../TriggerCustomerAddressUpdateObserver.php | 15 ++++++-- Serializer/CustomerSerializer.php | 13 +++---- Serializer/OrderSerializer.php | 20 +++++++--- composer.json | 2 +- etc/module.xml | 2 +- view/adminhtml/templates/settings.phtml | 16 ++++++++ 11 files changed, 124 insertions(+), 50 deletions(-) diff --git a/Block/Adminhtml/Settings/Form.php b/Block/Adminhtml/Settings/Form.php index fed4132..d4ff041 100644 --- a/Block/Adminhtml/Settings/Form.php +++ b/Block/Adminhtml/Settings/Form.php @@ -49,6 +49,10 @@ public function getFormKey() return $this->formKey->getFormKey(); } + public function getCustomerAddress(){ + return $this->configHelper->getCustomerAddressType(); + } + public function getPosIdOptions(){ $attribute_data = []; foreach ($this->attributesCollection as $item) { diff --git a/Controller/Adminhtml/Settings/Index.php b/Controller/Adminhtml/Settings/Index.php index e2ae1bf..ffdffd5 100644 --- a/Controller/Adminhtml/Settings/Index.php +++ b/Controller/Adminhtml/Settings/Index.php @@ -90,6 +90,10 @@ private function saveSettings($data){ $this->configHelper->setAheadworksRewardPointsEnabled($data['aw_rewards_integrate'] == 1); } + if(isset($data['customer_address'])) { + $this->configHelper->setCustomerAddressType($data['customer_address']); + } + return true; } } diff --git a/Helper/ConfigHelper.php b/Helper/ConfigHelper.php index dbb379b..e9c805f 100644 --- a/Helper/ConfigHelper.php +++ b/Helper/ConfigHelper.php @@ -29,11 +29,15 @@ class ConfigHelper const EVENT_SEARCH_VIEW_ENABLED = 'remarkety/mgconnector/event_search_view_enabled'; const EVENT_CATEGORY_VIEW_ENABLED = 'remarkety/mgconnector/event_category_view_enabled'; const ENABLE_AHEADWORKS_REWARD_POINTS = 'remarkety/mgconnector/aheadworks_reward_points'; + const CUSTOMER_ADDRESS_TO_USE = 'remarkety/mgconnector/customer_address_to_use'; const ASYNC_MODE_OFF = 0; const ASYNC_MODE_ON = 1; const ASYNC_MODE_ON_CUSTOMERS_SYNC = 2; + const CUSTOMER_ADDRESS_BILLING = 'billing'; + const CUSTOMER_ADDRESS_SHIPPING = 'shipping'; + protected $_activeStore; protected $_scopeConfig; protected $configResource; @@ -209,6 +213,28 @@ public function forceSyncWebhooks(){ return false; } + public function getCustomerAddressType(){ + $val = $this->_scopeConfig->getValue(self::CUSTOMER_ADDRESS_TO_USE); + if(!empty($val)){ + return $val; + } + return self::CUSTOMER_ADDRESS_BILLING; + } + + public function setCustomerAddressType($type){ + $val = self::CUSTOMER_ADDRESS_BILLING; + if($type === self::CUSTOMER_ADDRESS_SHIPPING){ + $val = $type; + } + $this->configResource->saveConfig( + self::CUSTOMER_ADDRESS_TO_USE, + $val, + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + 0 + ); + $this->cacheTypeList->cleanType('config'); + } + public function forceSyncCustomersWebhooks(){ $async = $this->_scopeConfig->getValue(self::FORCE_ASYNC_WEBHOOKS); if(!empty($async) && $async == self::ASYNC_MODE_ON_CUSTOMERS_SYNC){ diff --git a/Helper/Data.php b/Helper/Data.php index 6694e74..3f730db 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -6,6 +6,7 @@ use Magento\Catalog\Api\Data\ProductInterface; use Magento\TestFramework\Inspection\Exception; use \Remarkety\Mgconnector\Model\Install as InstallModel; +use Remarkety\Mgconnector\Serializer\AddressSerializer; class Data extends \Magento\Framework\App\Helper\AbstractHelper { @@ -15,6 +16,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper private $categoryMapCache = []; protected $categoryFactory; protected $configHelper; + protected $addressSerializer; public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Module\ModuleResource $moduleResource, \Magento\Integration\Model\Integration $integration, @@ -24,7 +26,8 @@ public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Catalog\Model\Product\Gallery\GalleryManagement $galleryManagement, \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable, \Magento\Catalog\Model\CategoryFactory $categoryFactory, - ConfigHelper $configHelper + ConfigHelper $configHelper, + AddressSerializer $addressSerializer ){ $this->integration = $integration; $this->moduleResource = $moduleResource; @@ -35,6 +38,7 @@ public function __construct(\Magento\Framework\App\Helper\Context $context, $this->_catalogProductTypeConfigurable = $catalogProductTypeConfigurable; $this->categoryFactory = $categoryFactory; $this->configHelper = $configHelper; + $this->addressSerializer = $addressSerializer; parent::__construct($context); } @@ -204,4 +208,32 @@ public static function toCamelCase($string, $capitalizeFirstCharacter = false){ } return $str; } + + public function getCustomerAddresses($customer){ + $addresses = $customer->getAddresses(); + $address = null; + $toUse = $this->configHelper->getCustomerAddressType(); + if(!empty($addresses)){ + $addressId = null; + if($toUse === ConfigHelper::CUSTOMER_ADDRESS_BILLING){ + $addressId = $customer->getDefaultBilling(); + } else { + $addressId = $customer->getDefaultShipping(); + } + if($addressId) { + $address = $this->findAddressById($addresses, $addressId); + } + } + + return $address ? $this->addressSerializer->serialize($address) : null; + } + + private function findAddressById($addresses, $id){ + foreach ($addresses as $address){ + if($address->getId() === $id){ + return $address; + } + } + return null; + } } diff --git a/Model/Api/Data.php b/Model/Api/Data.php index 56f76bf..417d9db 100644 --- a/Model/Api/Data.php +++ b/Model/Api/Data.php @@ -677,7 +677,7 @@ public function getCustomers( } } } - $customers['default_address'] = $this->getCustomerAddresses($customer); + $customers['default_address'] = $this->dataHelper->getCustomerAddresses($customer); $group = $this->customerGroupFactory->load($customer->getGroupId()); $customers['groups'] = array(); $customers['groups'][] = [ @@ -702,32 +702,6 @@ public function getCustomers( return $object; } - - public function getCustomerAddresses($customer){ - - $addresses = $customer->getAddresses(); - $address = null; - if(!empty($addresses)){ - $billingAddressId = $customer->getDefaultBilling(); - $shippingAddressId = $customer->getDefaultShipping(); - if($shippingAddressId) { - if (array_key_exists($shippingAddressId, $addresses)) { - $address = $addresses[$shippingAddressId]; - } - } - if(empty($address) && $billingAddressId) { - if (array_key_exists($billingAddressId, $addresses)) { - $address = $addresses[$billingAddressId]; - } - } - if(empty($address)){ - $address = array_pop($addresses); - } - } - - return $address ? $this->addressSerializer->serialize($address) : null; - } - /** * @param Order\Address $customerAddresses * @return array|null @@ -787,7 +761,7 @@ public function mapCustomer($customer, $isObject = false) } $customers['pos_id'] = $pos_id; $customers['accepts_marketing'] = $this->checkSubscriber($customerData->getEmail(), $customer_id); - $customers['default_address'] = $this->getCustomerAddresses($customerData); + $customers['default_address'] = $this->dataHelper->getCustomerAddresses($customerData); return $this->dataOverride->customer($customerData, $customers); } @@ -903,10 +877,14 @@ public function getOrders( if ($order->getCustomerId()) { $ord['customer'] = $this->getCustomerDataById($order->getCustomerId()); } else { + $addressId = $order->getBillingAddressId(); + if($this->configHelper->getCustomerAddressType() === ConfigHelper::CUSTOMER_ADDRESS_SHIPPING){ + $addressId = $order->getShippingAddressId(); + } /** * @var Order\Address $billingAddressData */ - $billingAddressData = $this->getGuestBillingAddressData($order->getBillingAddressId()); + $billingAddressData = $this->getGuestBillingAddressData($addressId); $address = $this->getAddressData($billingAddressData); $ord['customer']['email'] = $billingAddressData->getEmail(); @@ -1369,7 +1347,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue) */ public function getVersion() { - return '2.4.4'; + return '2.4.5'; } /** diff --git a/Observer/TriggerCustomerAddressUpdateObserver.php b/Observer/TriggerCustomerAddressUpdateObserver.php index 8e9a25e..aa753cc 100644 --- a/Observer/TriggerCustomerAddressUpdateObserver.php +++ b/Observer/TriggerCustomerAddressUpdateObserver.php @@ -5,6 +5,7 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Customer\Model\Address; use Magento\Customer\Model\CustomerFactory; +use Remarkety\Mgconnector\Helper\ConfigHelper; class TriggerCustomerAddressUpdateObserver extends EventMethods implements ObserverInterface { @@ -30,14 +31,20 @@ public function execute(\Magento\Framework\Event\Observer $observer) $this->customerRegistry->remove($customerId); $customer = $this->customerRepository->getById($customerId); - if($this->_coreRegistry->registry('remarkety_customer_address_updated_'.$customer->getId())) { - return $this; + $toUse = $this->configHelper->getCustomerAddressType(); + $isDefaultAddress = false; + if($toUse === ConfigHelper::CUSTOMER_ADDRESS_BILLING && $address->getIsDefaultBilling()){ + $isDefaultAddress = true; + } elseif($toUse === ConfigHelper::CUSTOMER_ADDRESS_SHIPPING && $address->getIsDefaultShipping()){ + $isDefaultAddress = true; } - - $isDefaultAddress = $address->getIsDefaultShipping() || $address->getIsDefaultBilling(); if (!$isDefaultAddress || !$customer->getId()) { return $this; } + if($this->_coreRegistry->registry('remarkety_customer_address_updated_'.$customer->getId())) { + return $this; + } + $this->_coreRegistry->register('remarkety_customer_address_updated_'.$customer->getId(),true); $this->_customerUpdate($customer); diff --git a/Serializer/CustomerSerializer.php b/Serializer/CustomerSerializer.php index 22f981f..d2aaf03 100644 --- a/Serializer/CustomerSerializer.php +++ b/Serializer/CustomerSerializer.php @@ -29,6 +29,7 @@ class CustomerSerializer private $configHelper; private $pos_id_attribute_code; private $dataOverride; + private $dataHelper; public function __construct( Subscriber $subscriber, AddressSerializer $addressSerializer, @@ -36,7 +37,8 @@ public function __construct( RequestInterface $request, \Psr\Log\LoggerInterface $logger = null, ConfigHelper $configHelper, - DataOverride $dataOverride + DataOverride $dataOverride, + Data $dataHelper ) { $this->subscriber = $subscriber; @@ -47,6 +49,7 @@ public function __construct( $this->configHelper = $configHelper; $this->pos_id_attribute_code = $configHelper->getPOSAttributeCode(); $this->dataOverride = $dataOverride; + $this->dataHelper = $dataHelper; } public function serialize(Customer $customer){ @@ -89,11 +92,7 @@ public function serialize(Customer $customer){ break; } - $addresses = $customer->getAddresses(); - if(!empty($addresses)){ - $address = array_pop($addresses); - } - + $address = $this->dataHelper->getCustomerAddresses($customer); $pos_id = $this->getPosId($customer); $customerInfo = [ @@ -106,7 +105,7 @@ public function serialize(Customer $customer){ 'created_at' => $created_at->format(\DateTime::ATOM ), 'updated_at' => $updated_at->format(\DateTime::ATOM ), 'guest' => false, - 'default_address' => empty($address) ? null : $this->addressSerializer->serialize($address), + 'default_address' => $address, 'groups' => $groups, 'gender' => $gender, 'birthdate' => $customer->getDob(), diff --git a/Serializer/OrderSerializer.php b/Serializer/OrderSerializer.php index 466831e..317b559 100644 --- a/Serializer/OrderSerializer.php +++ b/Serializer/OrderSerializer.php @@ -6,6 +6,7 @@ use Magento\Newsletter\Model\Subscriber; use Magento\Sales\Model\Order\Shipment; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; +use Remarkety\Mgconnector\Helper\ConfigHelper; use Remarkety\Mgconnector\Helper\DataOverride; class OrderSerializer @@ -21,6 +22,7 @@ class OrderSerializer private $customerSerializer; private $subscriber; private $dataOverride; + private $configHelper; public function __construct( CustomerRepository $customerRepository, @@ -29,7 +31,8 @@ public function __construct( AddressSerializer $addressSerializer, CustomerSerializer $customerSerializer, Subscriber $subscriber, - DataOverride $dataOverride + DataOverride $dataOverride, + ConfigHelper $configHelper ) { $this->customerRepository = $customerRepository; @@ -39,6 +42,7 @@ public function __construct( $this->customerSerializer = $customerSerializer; $this->subscriber = $subscriber; $this->dataOverride = $dataOverride; + $this->configHelper = $configHelper; } public function serialize(\Magento\Sales\Model\Order $order){ @@ -113,17 +117,21 @@ public function serialize(\Magento\Sales\Model\Order $order){ $customer = $this->customerRepository->getById($order->getCustomerId()); $customerInfo = $this->customerSerializer->serialize($customer); } else { - $billingAddress = $order->getBillingAddress(); + if($this->configHelper->getCustomerAddressType() === ConfigHelper::CUSTOMER_ADDRESS_BILLING){ + $address = $order->getBillingAddress(); + } else { + $address = $order->getShippingAddress(); + } $customerInfo = [ 'accepts_marketing' => $this->checkSubscriber($order->getCustomerEmail(), null), 'email' => $order->getCustomerEmail(), - 'title' => empty($billingAddress) ? null : $billingAddress->getPrefix(), - 'first_name' => empty($billingAddress) ? null : $billingAddress->getFirstname(), - 'last_name' => empty($billingAddress) ? null : $billingAddress->getLastname(), + 'title' => empty($address) ? null : $address->getPrefix(), + 'first_name' => empty($address) ? null : $address->getFirstname(), + 'last_name' => empty($address) ? null : $address->getLastname(), 'created_at' => $created_at->format(\DateTime::ATOM ), 'updated_at' => $created_at->format(\DateTime::ATOM ), 'guest' => true, - 'default_address' => empty($billingAddress) ? null : $this->addressSerializer->serialize($billingAddress) + 'default_address' => empty($address) ? null : $this->addressSerializer->serialize($address) ]; } diff --git a/composer.json b/composer.json index 99d2d3f..9673403 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "lib-libxml": "*" }, "type": "magento2-module", - "version": "2.4.4", + "version": "2.4.5", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/etc/module.xml b/etc/module.xml index 4b8cc4a..1206323 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - + diff --git a/view/adminhtml/templates/settings.phtml b/view/adminhtml/templates/settings.phtml index df51529..2a450e3 100644 --- a/view/adminhtml/templates/settings.phtml +++ b/view/adminhtml/templates/settings.phtml @@ -58,6 +58,22 @@ +
+ + +
+ +
+ +
Which default customer address should be sent to Remarkety (default: Billing address).
+
+
+
isIsAwPointsPluginExists()): ?>