+ {% 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/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/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 %}
{% 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/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',
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) }}
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);
}