diff --git a/README.md b/README.md index a46fe76..253b371 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,12 @@ In your Magento 2 root directory run: Module settings are found in the Magento 2 admin panel under Stores->Configuration->Sales->Sales SMS +## Customer Address Templates +If the system HTML Address Template has been edited the SMS notification value +will not appear in the customer dashboard address book. Add the following to +Stores->Configuration->Customer->Customer Configuration->Address Templates->HTML +where you wish this data to appear: +`{{depend sms_alert}}
SMS Enabled: {{var sms_alert}}{{/depend}}` + ## License GNU GENERAL PUBLIC LICENSE Version 3 \ No newline at end of file diff --git a/etc/config.xml b/etc/config.xml new file mode 100644 index 0000000..7e8a667 --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,38 @@ + + + + + + + {{/depend}} +{{depend company}}{{var company}}
{{/depend}} +{{if street1}}{{var street1}}
{{/if}} +{{depend street2}}{{var street2}}
{{/depend}} +{{depend street3}}{{var street3}}
{{/depend}} +{{depend street4}}{{var street4}}
{{/depend}} +{{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
+{{var country}}
+{{depend telephone}}T: {{var telephone}}{{/depend}} +{{depend sms_alert}}
SMS Enabled: {{var sms_alert}}{{/depend}} +{{depend fax}}
F: {{var fax}}{{/depend}} +{{depend vat_id}}
VAT: {{var vat_id}}{{/depend}}]]> +
+
+
+
diff --git a/view/frontend/requirejs-config.js b/view/frontend/requirejs-config.js index 138e9e8..530c09e 100644 --- a/view/frontend/requirejs-config.js +++ b/view/frontend/requirejs-config.js @@ -4,11 +4,17 @@ var config = { 'Magento_Checkout/js/action/set-shipping-information': { 'Pmclain_Twilio/js/action/set-shipping-information-mixin': true }, + 'Magento_Checkout/js/action/create-shipping-address': { + 'Pmclain_Twilio/js/action/create-shipping-address-mixin': true + }, 'Magento_Checkout/js/action/create-billing-address': { 'Pmclain_Twilio/js/action/create-billing-address-mixin': true }, 'Magento_Checkout/js/action/place-order': { 'Pmclain_Twilio/js/action/place-order-mixin': true + }, + 'Magento_Customer/js/model/customer/address': { + 'Pmclain_Twilio/js/model/customer/address-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 index f2defba..51d3ac7 100644 --- a/view/frontend/web/js/action/create-billing-address-mixin.js +++ b/view/frontend/web/js/action/create-billing-address-mixin.js @@ -15,21 +15,25 @@ */ define([ - 'jquery', - 'mage/utils/wrapper', - 'Magento_Checkout/js/model/quote' -], function($, wrapper, quote) { + 'mage/utils/wrapper' +], function(wrapper) { 'use strict'; return function (createBillingAddressAction) { return wrapper.wrap(createBillingAddressAction, function(originalAction, addressData) { - var result = originalAction(); - if(result['customAttributes'] === undefined) { - result['customAttributes'] = {}; + if (addressData.custom_attributes === undefined) { + return originalAction(); } - result.customAttributes['sms_alert'] = addressData.sms_alert; - return result; + if (addressData.custom_attributes['sms_alert']) { + addressData.custom_attributes['sms_alert'] = { + 'attribute_code': 'sms_alert', + 'value': 'SMS Enabled', + 'status': 1 + } + } + + return originalAction(); }); }; }); \ No newline at end of file diff --git a/view/frontend/web/js/action/create-shipping-address-mixin.js b/view/frontend/web/js/action/create-shipping-address-mixin.js new file mode 100644 index 0000000..000fc6e --- /dev/null +++ b/view/frontend/web/js/action/create-shipping-address-mixin.js @@ -0,0 +1,40 @@ +/** + * Pmclain_Twilio extension + * + * NOTICE OF LICENSE + * + * This source file is subject to the GPL v3 License + * that is bundled with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * https://www.gnu.org/licenses/gpl.txt + * + * @category Pmclain + * @package Twilio + * @copyright Copyright (c) 2017 + * @license https://www.gnu.org/licenses/gpl.txt GPL v3 License + */ + +define([ + 'jquery', + 'mage/utils/wrapper' +], function($, wrapper) { + 'use strict'; + + return function (createShippingAddressAction) { + return wrapper.wrap(createShippingAddressAction, function(originalAction, addressData) { + if (addressData.custom_attributes === undefined) { + return originalAction(); + } + + if (addressData.custom_attributes['sms_alert']) { + addressData.custom_attributes['sms_alert'] = { + 'attribute_code': 'sms_alert', + 'value': 'SMS Enabled', + 'status': 1 + } + } + + return originalAction(); + }); + }; +}); \ No newline at end of file diff --git a/view/frontend/web/js/action/place-order-mixin.js b/view/frontend/web/js/action/place-order-mixin.js index bbe0e34..a906544 100644 --- a/view/frontend/web/js/action/place-order-mixin.js +++ b/view/frontend/web/js/action/place-order-mixin.js @@ -24,11 +24,32 @@ define([ return function (placeOrderAction) { return wrapper.wrap(placeOrderAction, function(originalAction) { var billingAddress = quote.billingAddress(); + + if(billingAddress.customAttributes === undefined) { + billingAddress.customAttributes = {}; + } + if(billingAddress['extension_attributes'] === undefined) { billingAddress['extension_attributes'] = {}; } - billingAddress['extension_attributes']['sms_alert'] = billingAddress.customAttributes['sms_alert'] ? 1 : 0; + try { + var smsStatus = billingAddress.customAttributes['sms_alert'].status; + var smsValue = billingAddress.customAttributes['sms_alert'].value; + + //TODO: this smells. the check for status stems from an attempt to + // non-invasively display a user friendly custom attribute value in + // the customer address-mixin. + if(smsStatus == true) { + billingAddress['extension_attributes']['sms_alert'] = smsStatus; + }else if(smsValue == true) { + billingAddress['extension_attributes']['sms_alert'] = smsValue + }else { + billingAddress['extension_attributes']['sms_alert'] = false; + } + }catch (e) { + return originalAction(); + } return originalAction(); }); 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 c89ec92..914d8d4 100644 --- a/view/frontend/web/js/action/set-shipping-information-mixin.js +++ b/view/frontend/web/js/action/set-shipping-information-mixin.js @@ -24,11 +24,32 @@ define([ return function (setShippingInformationAction) { return wrapper.wrap(setShippingInformationAction, function(originalAction) { var shippingAddress = quote.shippingAddress(); + + if(shippingAddress.customAttributes === undefined) { + shippingAddress.customAttributes = {}; + } + if(shippingAddress['extension_attributes'] === undefined) { shippingAddress['extension_attributes'] = {}; } - shippingAddress['extension_attributes']['sms_alert'] = shippingAddress.customAttributes['sms_alert'] ? 1 : 0; + try { + var smsStatus = shippingAddress.customAttributes['sms_alert'].status; + var smsValue = shippingAddress.customAttributes['sms_alert'].value; + + //TODO: this smells. the check for status stems from an attempt to + // non-invasively display a user friendly custom attribute value in + // the customer address-mixin. + if(smsStatus == true) { + shippingAddress['extension_attributes']['sms_alert'] = smsStatus; + }else if(smsValue == true) { + shippingAddress['extension_attributes']['sms_alert'] = smsValue + }else { + shippingAddress['extension_attributes']['sms_alert'] = false; + } + }catch (e) { + return originalAction(); + } return originalAction(); }); diff --git a/view/frontend/web/js/model/customer/address-mixin.js b/view/frontend/web/js/model/customer/address-mixin.js new file mode 100644 index 0000000..b13faa0 --- /dev/null +++ b/view/frontend/web/js/model/customer/address-mixin.js @@ -0,0 +1,39 @@ +/** + * Pmclain_Twilio extension + * + * NOTICE OF LICENSE + * + * This source file is subject to the GPL v3 License + * that is bundled with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * https://www.gnu.org/licenses/gpl.txt + * + * @category Pmclain + * @package Twilio + * @copyright Copyright (c) 2017 + * @license https://www.gnu.org/licenses/gpl.txt GPL v3 License + */ + +define([ + 'jquery', + 'mage/utils/wrapper', + 'mage/translate' +], function($, wrapper) { + 'use strict'; + + return function (addressModel) { + return wrapper.wrap(addressModel, function(originalAction) { + var address = originalAction(); + + if(address.customAttributes !== undefined) { + if(address.customAttributes['sms_alert']) { + var enabled = address.customAttributes['sms_alert'].value; + address.customAttributes['sms_alert'].value = $.mage.__(enabled ? 'SMS Enabled' : 'SMS Disabled'); + address.customAttributes['sms_alert'].status = enabled ? 1 : 0; + } + } + + return address; + }); + }; +}); \ No newline at end of file