Skip to content
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
38 changes: 38 additions & 0 deletions Observer/QuoteToOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SystemCode\BrazilCustomerAttributes\Observer;

use \Magento\Customer\Api\AddressRepositoryInterface;

/**
*
* Observer to copy quote fields to order
Expand All @@ -17,6 +19,21 @@
*/
class QuoteToOrder implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var AddressRepositoryInterface
*/
protected $addressRepository;

/**
* QuoteToOrder constructor.
* @param AddressRepositoryInterface $addressRepository
*/
public function __construct(
AddressRepositoryInterface $addressRepository
) {
$this->addressRepository = $addressRepository;
}

/**
*
* @param \Magento\Framework\Event\Observer $observer
Expand All @@ -30,13 +47,34 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$orderShippingAdress = $observer->getOrder()->getShippingAddress();
$orderShippingAdress->setStreetPrefix($street_prefix)->save();

// copy shipping address street prefix to customer
if($addressId = $orderShippingAdress->getCustomerAddressId()){
$this->updateCustomerAddress($addressId, $street_prefix);
}

// copy billing address street prefix
$quoteBillingAddress = $observer->getQuote()->getBillingAddress();
$street_prefix = $quoteBillingAddress->getStreetPrefix();
$orderBillingAddress = $observer->getOrder()->getBillingAddress();
$orderBillingAddress->setStreetPrefix($street_prefix)->save();

// copy billing address street prefix to customer
if($addressId = $orderBillingAddress->getCustomerAddressId()){
$this->updateCustomerAddress($addressId, $street_prefix);
}

return $this;
}

/**
* Update street prefix on customer address
* @throws \Magento\Framework\Exception\LocalizedException
* @return void
*/
protected function updateCustomerAddress($addressId, $streetPrefix) {
$address = $this->addressRepository->getById($addressId);
$address->setCustomAttribute('street_prefix', $streetPrefix);
$this->addressRepository->save($address);
}

}
3 changes: 1 addition & 2 deletions Plugin/Quote/BillingAddressManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ public function beforeAssign(
$useForShipping = false
) {
$extAttributes = $address->getExtensionAttributes();
if (!empty($extAttributes)) {

if (!empty($extAttributes->getStreetPrefix())) {
try {
$address->setStreetPrefix($extAttributes->getStreetPrefix());
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}

}
}
}
2 changes: 1 addition & 1 deletion Plugin/Quote/ShippingAddressManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function beforeAssign(
AddressInterface $address
) {
$extAttributes = $address->getExtensionAttributes();
if (!empty($extAttributes)) {

if (!empty($extAttributes->getStreetPrefix())) {
try {
$address->setStreetPrefix($extAttributes->getStreetPrefix());
} catch (\Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"systemcode/base": "*"
},
"type": "magento2-module",
"version": "1.0.3",
"version": "1.0.4",
"license": [
"proprietary"
],
Expand Down
11 changes: 4 additions & 7 deletions view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ var config = {
},
config: {
mixins: {
'Magento_Checkout/js/action/set-billing-address': {
'SystemCode_BrazilCustomerAttributes/js/action/set-billing-address-mixin': true
},
'Magento_Checkout/js/action/set-shipping-information': {
'SystemCode_BrazilCustomerAttributes/js/action/set-shipping-information-mixin': true
},
'Magento_Checkout/js/action/create-shipping-address': {
'SystemCode_BrazilCustomerAttributes/js/action/create-shipping-address-mixin': true
},
'Magento_Checkout/js/action/place-order': {
'SystemCode_BrazilCustomerAttributes/js/action/set-billing-address-mixin': true
},
'Magento_Checkout/js/action/create-billing-address': {
'SystemCode_BrazilCustomerAttributes/js/action/set-billing-address-mixin': true
'SystemCode_BrazilCustomerAttributes/js/action/create-billing-address-mixin': true
},
'Magento_Checkout/js/action/place-order': {
'SystemCode_BrazilCustomerAttributes/js/action/place-order-mixin': true
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions view/frontend/web/js/action/create-billing-address-mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
define([
'jquery',
'mage/utils/wrapper',
'Magento_Checkout/js/model/quote'
], function ($, wrapper,quote) {
'use strict';

return function (setBillingInformationAction) {
return wrapper.wrap(setBillingInformationAction, function (originalAction, messageContainer) {

if (messageContainer.custom_attributes != undefined) {
$.each(messageContainer.custom_attributes , function( key, value ) {
messageContainer['custom_attributes'][key] = value;
});

}
return originalAction(messageContainer);
});
};
});
3 changes: 1 addition & 2 deletions view/frontend/web/js/action/create-shipping-address-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ define([

return function (setShippingInformationAction) {
return wrapper.wrap(setShippingInformationAction, function (originalAction, messageContainer) {

if (messageContainer.custom_attributes != undefined) {
$.each(messageContainer.custom_attributes , function( key, value ) {
messageContainer['custom_attributes'][key] = {'attribute_code':key,'value':value};
messageContainer['custom_attributes'][key] = value;
});
}

Expand Down
29 changes: 29 additions & 0 deletions view/frontend/web/js/action/place-order-mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
define([
'jquery',
'mage/utils/wrapper',
'Magento_Checkout/js/model/quote'
], function ($, wrapper,quote) {
'use strict';

return function (placeOrderAction) {
return wrapper.wrap(placeOrderAction, function (originalAction, messageContainer) {
var billingAddress = quote.billingAddress();

if (billingAddress['extension_attributes'] === undefined) {
billingAddress['extension_attributes'] = {};
}

if (billingAddress.customAttributes !== undefined) {
$.each(billingAddress.customAttributes, function (key, value) {
var attrCode = value['attribute_code'];
var attrValue = value['value'];

billingAddress['customAttributes'][attrCode] = value;
billingAddress['extension_attributes'][attrCode] = attrValue;
});
}

return originalAction(messageContainer);
});
};
});
35 changes: 0 additions & 35 deletions view/frontend/web/js/action/set-billing-address-mixin.js

This file was deleted.

14 changes: 6 additions & 8 deletions view/frontend/web/js/action/set-shipping-information-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ define([
shippingAddress['extension_attributes'] = {};
}

if (shippingAddress.customAttributes != undefined) {
$.each(shippingAddress.customAttributes , function( key, value ) {
if (shippingAddress.customAttributes !== undefined) {
$.each(shippingAddress.customAttributes, function (key, value) {

if($.isPlainObject(value)){
value = value['value'];
}

shippingAddress['customAttributes'][key] = value;
shippingAddress['extension_attributes'][key] = value;
var attrCode = value['attribute_code'];
var attrValue = value['value'];

shippingAddress['customAttributes'][attrCode] = value;
shippingAddress['extension_attributes'][attrCode] = attrValue;
});
}

Expand Down