From c78ee10df8e51183714134b2fd065abd2b283d05 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Wed, 5 Jul 2023 09:44:49 +0100 Subject: [PATCH 1/3] Add primary checkboxs to address editing in example templates #2940 --- .../dist/shop/_private/address/fields.twig | 26 +++++++++++++++++++ .../dist/shop/customer/addresses/edit.twig | 1 + .../src/shop/_private/address/fields.twig | 26 +++++++++++++++++++ .../src/shop/customer/addresses/edit.twig | 1 + 4 files changed, 54 insertions(+) diff --git a/example-templates/dist/shop/_private/address/fields.twig b/example-templates/dist/shop/_private/address/fields.twig index d17b516d64..9f04efa630 100644 --- a/example-templates/dist/shop/_private/address/fields.twig +++ b/example-templates/dist/shop/_private/address/fields.twig @@ -2,6 +2,7 @@ {# Outputs address form fields for editing an address. #} +{% set showPrimaryCheckboxes = showPrimaryCheckboxes is defined ? showPrimaryCheckboxes : false %} {% set addressFieldLayout = craft.app.getAddresses().getLayout() %} {% set addressCustomFields = addressFieldLayout.getCustomFields()|filter(f => className(f) == 'craft\\fields\\PlainText') %} {# @var address \craft\elements\Address #} @@ -193,9 +194,34 @@ Outputs address form fields for editing an address. {% endfor %} {% endif %} + + {% if showPrimaryCheckboxes %} +
+
+ {{ input('text', 'isPrimaryBilling', address.isPrimarybilling ? 1 : 0) }} + +
+
+ {{ input('text', 'isPrimaryShipping', address.isPrimaryShipping ? 1 : 0) }} + +
+ {% endif %} {% js %} +{% if showPrimaryCheckboxes %} +document.querySelectorAll('input[type=checkbox][data-primary-input]').forEach(el => { + el.addEventListener('change', ev => { + let primaryInput = document.querySelector(`input[name="${ev.target.dataset.primaryInput}"]`); + if (ev.target.checked) { + primaryInput.value = 1; + } else { + primaryInput.value = 0; + } + }); +}); +{% endif %} + document.querySelector('select#{{ 'countryCode'|namespaceInputId(addressName) }}').addEventListener('change', ev => { const countryCode = ev.target.value; const stateSelect = document.querySelector('select#{{ 'administrativeArea'|namespaceInputId(addressName) }}'); diff --git a/example-templates/dist/shop/customer/addresses/edit.twig b/example-templates/dist/shop/customer/addresses/edit.twig index 24fea2362d..dabbcebd41 100644 --- a/example-templates/dist/shop/customer/addresses/edit.twig +++ b/example-templates/dist/shop/customer/addresses/edit.twig @@ -46,6 +46,7 @@ {{ include('shop/_private/address/fields', { address: address, showLabelField: true, + showPrimaryCheckboxes: true, }) }} {{ hiddenInput('ownerId', currentUser.id) }} diff --git a/example-templates/src/shop/_private/address/fields.twig b/example-templates/src/shop/_private/address/fields.twig index 2021085cf8..2ae32496a0 100755 --- a/example-templates/src/shop/_private/address/fields.twig +++ b/example-templates/src/shop/_private/address/fields.twig @@ -2,6 +2,7 @@ {# Outputs address form fields for editing an address. #} +{% set showPrimaryCheckboxes = showPrimaryCheckboxes is defined ? showPrimaryCheckboxes : false %} {% set addressFieldLayout = craft.app.getAddresses().getLayout() %} {% set addressCustomFields = addressFieldLayout.getCustomFields()|filter(f => className(f) == 'craft\\fields\\PlainText') %} {# @var address \craft\elements\Address #} @@ -193,9 +194,34 @@ Outputs address form fields for editing an address. {% endfor %} {% endif %} + + {% if showPrimaryCheckboxes %} +
+
+ {{ input('text', 'isPrimaryBilling', address.isPrimarybilling ? 1 : 0) }} + +
+
+ {{ input('text', 'isPrimaryShipping', address.isPrimaryShipping ? 1 : 0) }} + +
+ {% endif %} {% js %} +{% if showPrimaryCheckboxes %} +document.querySelectorAll('input[type=checkbox][data-primary-input]').forEach(el => { + el.addEventListener('change', ev => { + let primaryInput = document.querySelector(`input[name="${ev.target.dataset.primaryInput}"]`); + if (ev.target.checked) { + primaryInput.value = 1; + } else { + primaryInput.value = 0; + } + }); +}); +{% endif %} + document.querySelector('select#{{ 'countryCode'|namespaceInputId(addressName) }}').addEventListener('change', ev => { const countryCode = ev.target.value; const stateSelect = document.querySelector('select#{{ 'administrativeArea'|namespaceInputId(addressName) }}'); diff --git a/example-templates/src/shop/customer/addresses/edit.twig b/example-templates/src/shop/customer/addresses/edit.twig index 1108ee7c51..c0b771ef37 100644 --- a/example-templates/src/shop/customer/addresses/edit.twig +++ b/example-templates/src/shop/customer/addresses/edit.twig @@ -46,6 +46,7 @@ {{ include('[[folderName]]/_private/address/fields', { address: address, showLabelField: true, + showPrimaryCheckboxes: true, }) }} {{ hiddenInput('ownerId', currentUser.id) }} From fd56ea73faea66bb8d4f09bbd99fb7e33f0ff371 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Wed, 5 Jul 2023 09:50:43 +0100 Subject: [PATCH 2/3] TIdy `makePrimaryX` checkboxes in example templates #2940 --- .../dist/shop/checkout/addresses.twig | 44 +++++++++---------- .../src/shop/checkout/addresses.twig | 44 +++++++++---------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/example-templates/dist/shop/checkout/addresses.twig b/example-templates/dist/shop/checkout/addresses.twig index f7ab646ba8..e22bceb778 100644 --- a/example-templates/dist/shop/checkout/addresses.twig +++ b/example-templates/dist/shop/checkout/addresses.twig @@ -43,6 +43,17 @@ Outputs a form for collecting an order’s shipping and billing address. sourceIdName: 'sourceShippingAddressId', }) }} + {% if currentUser and addresses|length %} +
+ +
+ {% endif %} +
{{ hiddenInput('billingAddressSameAsShipping', 0) }} @@ -57,17 +68,6 @@ Outputs a form for collecting an order’s shipping and billing address. - {% if currentUser %} -
- -
- {% endif %} -
{{ include('shop/_private/address/fieldset', { title: 'Billing Address'|t, @@ -78,19 +78,19 @@ Outputs a form for collecting an order’s shipping and billing address. }) }}
-
- - {% if currentUser %} -
- -
+ {% if currentUser and addresses|length %} +
+ +
{% endif %} +
+
{{ tag('button', { type: 'submit', diff --git a/example-templates/src/shop/checkout/addresses.twig b/example-templates/src/shop/checkout/addresses.twig index aa242e3f8f..30e395057d 100755 --- a/example-templates/src/shop/checkout/addresses.twig +++ b/example-templates/src/shop/checkout/addresses.twig @@ -43,6 +43,17 @@ Outputs a form for collecting an order’s shipping and billing address. sourceIdName: 'sourceShippingAddressId', }) }} + {% if currentUser and addresses|length %} +
+ +
+ {% endif %} +
{{ hiddenInput('billingAddressSameAsShipping', 0) }} @@ -57,17 +68,6 @@ Outputs a form for collecting an order’s shipping and billing address.
- {% if currentUser %} -
- -
- {% endif %} -
{{ include('[[folderName]]/_private/address/fieldset', { title: 'Billing Address'|t, @@ -78,19 +78,19 @@ Outputs a form for collecting an order’s shipping and billing address. }) }}
-
- - {% if currentUser %} -
- -
+ {% if currentUser and addresses|length %} +
+ +
{% endif %} +
+
{{ tag('button', { type: 'submit', From 082cc2f08a1b6863745e1a0dc8d31115cb84d4bd Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Wed, 5 Jul 2023 10:16:36 +0100 Subject: [PATCH 3/3] Fix cs --- src/services/PaymentSources.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/PaymentSources.php b/src/services/PaymentSources.php index 530f2ed175..62925919fc 100644 --- a/src/services/PaymentSources.php +++ b/src/services/PaymentSources.php @@ -284,7 +284,7 @@ public function createPaymentSource(int $customerId, GatewayInterface $gateway, throw new PaymentSourceException(Craft::t('commerce', 'Could not create the payment source.')); } - if($makePrimarySource) { + if ($makePrimarySource) { Plugin::getInstance()->getCustomers()->savePrimaryPaymentSourceId($source->getCustomer(), $source->id); }