Skip to content

Commit

Permalink
2.4.5 - always get customer address from default billing address unle…
Browse files Browse the repository at this point in the history
…ss selected otherwise on store settings
  • Loading branch information
bnayalivne committed Dec 6, 2020
1 parent 2891810 commit 3cded20
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 50 deletions.
4 changes: 4 additions & 0 deletions Block/Adminhtml/Settings/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions Controller/Adminhtml/Settings/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
26 changes: 26 additions & 0 deletions Helper/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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){
Expand Down
34 changes: 33 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
38 changes: 8 additions & 30 deletions Model/Api/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'][] = [
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1369,7 +1347,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue)
*/
public function getVersion()
{
return '2.4.4';
return '2.4.5';
}

/**
Expand Down
15 changes: 11 additions & 4 deletions Observer/TriggerCustomerAddressUpdateObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions Serializer/CustomerSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ class CustomerSerializer
private $configHelper;
private $pos_id_attribute_code;
private $dataOverride;
private $dataHelper;
public function __construct(
Subscriber $subscriber,
AddressSerializer $addressSerializer,
CustomerGroupRepository $customerGroupRepository,
RequestInterface $request,
\Psr\Log\LoggerInterface $logger = null,
ConfigHelper $configHelper,
DataOverride $dataOverride
DataOverride $dataOverride,
Data $dataHelper
)
{
$this->subscriber = $subscriber;
Expand All @@ -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){
Expand Down Expand Up @@ -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 = [
Expand All @@ -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(),
Expand Down
20 changes: 14 additions & 6 deletions Serializer/OrderSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +22,7 @@ class OrderSerializer
private $customerSerializer;
private $subscriber;
private $dataOverride;
private $configHelper;

public function __construct(
CustomerRepository $customerRepository,
Expand All @@ -29,7 +31,8 @@ public function __construct(
AddressSerializer $addressSerializer,
CustomerSerializer $customerSerializer,
Subscriber $subscriber,
DataOverride $dataOverride
DataOverride $dataOverride,
ConfigHelper $configHelper
)
{
$this->customerRepository = $customerRepository;
Expand All @@ -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){
Expand Down Expand Up @@ -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)
];
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lib-libxml": "*"
},
"type": "magento2-module",
"version": "2.4.4",
"version": "2.4.5",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Remarkety_Mgconnector" setup_version="2.4.4">
<module name="Remarkety_Mgconnector" setup_version="2.4.5">
</module>
</config>
16 changes: 16 additions & 0 deletions view/adminhtml/templates/settings.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@
</div>
</div>
</div>
<div class="admin__field">
<label for="with_fpt" class="admin__field-label">
<span><?php echo __('Customer address field') ?></span>
</label>

<div class="admin__field-control _with-tooltip">
<select id="customer_address" name="customer_address" class="admin__control-select">
<option value="billing" <?php if('billing' == $this->getCustomerAddress()){ echo 'selected="selected"'; } ?>>Billing Address</option>
<option value="shipping" <?php if('shipping' == $this->getCustomerAddress()){ echo 'selected="selected"'; } ?>>Shipping Address</option>
</select>
<div class="admin__field-tooltip">
<span class="admin__field-tooltip-action action-help"></span>
<div class="admin__field-tooltip-content">Which default customer address should be sent to Remarkety (default: Billing address).</div>
</div>
</div>
</div>
<?php if($this->isIsAwPointsPluginExists()): ?>
<div class="admin__field">
<label for="aw_rewards_integrate" class="admin__field-label">
Expand Down

0 comments on commit 3cded20

Please sign in to comment.