diff --git a/Observer/QuoteToOrder.php b/Observer/QuoteToOrder.php index 5179f06..b6322b5 100644 --- a/Observer/QuoteToOrder.php +++ b/Observer/QuoteToOrder.php @@ -2,6 +2,8 @@ namespace SystemCode\BrazilCustomerAttributes\Observer; +use \Magento\Customer\Api\AddressRepositoryInterface; + /** * * Observer to copy quote fields to order @@ -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 @@ -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); + } + } \ No newline at end of file diff --git a/Plugin/Quote/BillingAddressManagement.php b/Plugin/Quote/BillingAddressManagement.php index 5a1ccce..fc165f5 100644 --- a/Plugin/Quote/BillingAddressManagement.php +++ b/Plugin/Quote/BillingAddressManagement.php @@ -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()); } - } } } \ No newline at end of file diff --git a/Plugin/Quote/ShippingAddressManagement.php b/Plugin/Quote/ShippingAddressManagement.php index bc16fbe..71038ca 100644 --- a/Plugin/Quote/ShippingAddressManagement.php +++ b/Plugin/Quote/ShippingAddressManagement.php @@ -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) { diff --git a/composer.json b/composer.json index f138d98..31abd7d 100755 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "systemcode/base": "*" }, "type": "magento2-module", - "version": "1.0.3", + "version": "1.0.4", "license": [ "proprietary" ], diff --git a/view/frontend/requirejs-config.js b/view/frontend/requirejs-config.js index 7e09ab4..870486c 100755 --- a/view/frontend/requirejs-config.js +++ b/view/frontend/requirejs-config.js @@ -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 } } } diff --git a/view/frontend/web/js/action/create-billing-address-mixin.js b/view/frontend/web/js/action/create-billing-address-mixin.js new file mode 100644 index 0000000..a21e8dd --- /dev/null +++ b/view/frontend/web/js/action/create-billing-address-mixin.js @@ -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); + }); + }; +}); diff --git a/view/frontend/web/js/action/create-shipping-address-mixin.js b/view/frontend/web/js/action/create-shipping-address-mixin.js index 94d375e..66bfdb4 100644 --- a/view/frontend/web/js/action/create-shipping-address-mixin.js +++ b/view/frontend/web/js/action/create-shipping-address-mixin.js @@ -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; }); } diff --git a/view/frontend/web/js/action/place-order-mixin.js b/view/frontend/web/js/action/place-order-mixin.js new file mode 100644 index 0000000..51334c9 --- /dev/null +++ b/view/frontend/web/js/action/place-order-mixin.js @@ -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); + }); + }; +}); \ No newline at end of file diff --git a/view/frontend/web/js/action/set-billing-address-mixin.js b/view/frontend/web/js/action/set-billing-address-mixin.js deleted file mode 100644 index b6df19c..0000000 --- a/view/frontend/web/js/action/set-billing-address-mixin.js +++ /dev/null @@ -1,35 +0,0 @@ -define([ - 'jquery', - 'mage/utils/wrapper', - 'Magento_Checkout/js/model/quote' -], function ($, wrapper,quote) { - 'use strict'; - - return function (setBillingAddressAction) { - return wrapper.wrap(setBillingAddressAction, function (originalAction, messageContainer) { - - var billingAddress = quote.billingAddress(); - - if(billingAddress != undefined) { - - if (billingAddress['extension_attributes'] === undefined) { - billingAddress['extension_attributes'] = {}; - } - - if (billingAddress.customAttributes != undefined) { - $.each(billingAddress.customAttributes, function (key, value) { - - if($.isPlainObject(value)){ - value = value['value']; - } - - billingAddress['extension_attributes'][key] = value; - }); - } - - } - - return originalAction(messageContainer); - }); - }; -}); \ No newline at end of file diff --git a/view/frontend/web/js/action/set-shipping-information-mixin.js b/view/frontend/web/js/action/set-shipping-information-mixin.js index 5043a43..b3d7a9a 100644 --- a/view/frontend/web/js/action/set-shipping-information-mixin.js +++ b/view/frontend/web/js/action/set-shipping-information-mixin.js @@ -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; }); }