Skip to content
Open
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
4 changes: 2 additions & 2 deletions controllers/front/addresseslist.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ protected function handleOpcRequest(): array
'checkout/_partials/one-page-checkout/address-list',
[
'customer' => $response['customer'] ?? [],
'prefix' => '',
'address_type' => 'delivery',
'selected_address' => (int) ($response['selected_delivery_address'] ?? 0),
]
),
'billing_html' => $this->render(
'checkout/_partials/one-page-checkout/address-list',
[
'customer' => $response['customer'] ?? [],
'prefix' => 'invoice_',
'address_type' => 'invoice',
'selected_address' => (int) ($response['selected_invoice_address'] ?? 0),
]
),
Expand Down
22 changes: 10 additions & 12 deletions src/Form/OnePageCheckoutForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ public function submit()
} else {
$invoiceAddress = $this->buildAddressFromGroup(
$fieldsByGroup['invoiceFields'],
null,
'invoice_'
null
);
$invoiceAddress->id_customer = $customer->id;
$invoiceAddress->alias = $invoiceAddress->alias ?: $this->translator->trans(
Expand Down Expand Up @@ -619,7 +618,7 @@ public function getInvoiceAddress()
{
$fieldsByGroup = $this->mapFieldsByGroup();

return $this->buildAddressFromGroup($fieldsByGroup['invoiceFields'], null, 'invoice_');
return $this->buildAddressFromGroup($fieldsByGroup['invoiceFields'], null);
}

public function getTemplateVariables()
Expand Down Expand Up @@ -716,7 +715,10 @@ private function mapFieldsByGroup(): array
}

if ($this->isInvoiceField($key)) {
$fieldsByGroup['invoiceFields'][$key] = $field;
$strippedKey = substr($key, strlen('invoice_'));
$clonedField = clone $field;
$clonedField->setName($strippedKey);
$fieldsByGroup['invoiceFields'][$strippedKey] = $clonedField;
continue;
}

Expand Down Expand Up @@ -838,22 +840,18 @@ private function buildCustomerFromFields(array $fields): \Customer
* @param array<string, \FormField> $fields
* @param int|null $idAddress
*/
private function buildAddressFromGroup(array $fields, $idAddress, string $prefix = ''): \Address
private function buildAddressFromGroup(array $fields, $idAddress): \Address
{
$address = new \Address($idAddress ? (int) $idAddress : null, $this->language->id);

foreach ($fields as $formField) {
$fieldName = $formField->getName();
$baseName = $prefix && strpos($fieldName, $prefix) === 0
? substr($fieldName, strlen($prefix))
: $fieldName;

if (property_exists($address, $baseName)) {
$address->{$baseName} = $formField->getValue();
if (property_exists($address, $fieldName)) {
$address->{$fieldName} = $formField->getValue();
}
}

if (!isset($fields[$prefix . 'id_state'])) {
if (!isset($fields['id_state'])) {
$address->id_state = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/php/Unit/Checkout/CheckoutOnePageStepRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getTemplateVarConfiguration(): array
'additionalCustomerFields' => [],
'useSameAddressField' => ['value' => true],
'deliveryFields' => ['firstname' => ['name' => 'firstname']],
'invoiceFields' => ['invoice_firstname' => ['name' => 'invoice_firstname']],
'invoiceFields' => ['firstname' => ['name' => 'firstname']],
'invoiceMetaFields' => [],
'errors' => ['' => []],
'token' => 'token',
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Unit/Form/OnePageCheckoutFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public function testItSeparatesTemplateVariablesByBusinessOrigin(): void
self::assertSame('opcinvariantprobe_customer_text', $templateVariables['additionalCustomerFields']['customer_probe_text']['name']);
self::assertSame('use_same_address', $templateVariables['formFields']['use_same_address']['name']);
self::assertSame('firstname', $templateVariables['deliveryFields']['firstname']['name']);
self::assertSame('invoice_address1', $templateVariables['invoiceFields']['invoice_address1']['name']);
self::assertSame('address1', $templateVariables['invoiceFields']['address1']['name']);
self::assertSame('id_address_invoice', $templateVariables['invoiceMetaFields']['id_address_invoice']['name']);
}

Expand Down
6 changes: 2 additions & 4 deletions views/js/opc-address-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ const BILLING_FIELDS_SELECTOR = OPC_SELECTORS.opc.billingFields;
const DISABLED_BY_SAME_ADDRESS_ATTRIBUTE = 'data-opc-disabled-by-same-address';
const SERVER_MANAGED_FIELDS = new Set([
'id_country',
'invoice_id_country',
'id_state',
'invoice_id_state',
'use_same_address',
]);
const NON_PRESERVABLE_FIELD_TYPES = new Set(['hidden', 'file', 'submit', 'button', 'image', 'reset']);
Expand Down Expand Up @@ -759,7 +757,7 @@ function refreshAddressesSection(options = {}) {
payload.id_address_delivery = getAddressSectionFieldValue($addressForm, DELIVERY_SECTION_SELECTOR, DELIVERY_FIELDS_SELECTOR, 'id_address_delivery');
payload.id_address_invoice = getAddressSectionFieldValue($addressForm, BILLING_SECTION_SELECTOR, BILLING_FIELDS_SELECTOR, 'id_address_invoice');
payload.id_country = getAddressSectionFieldValue($addressForm, DELIVERY_SECTION_SELECTOR, DELIVERY_FIELDS_SELECTOR, 'id_country');
payload.invoice_id_country = getAddressSectionFieldValue($addressForm, BILLING_SECTION_SELECTOR, BILLING_FIELDS_SELECTOR, 'invoice_id_country');
payload.invoice_id_country = getAddressSectionFieldValue($addressForm, BILLING_SECTION_SELECTOR, BILLING_FIELDS_SELECTOR, 'id_country');
}

const useSameAddress = payload.use_same_address !== '0';
Expand All @@ -776,7 +774,7 @@ function refreshAddressesSection(options = {}) {
syncBillingSectionConstraints($addressForm, useSameAddress);
if (!resetInlineAddressState) {
setAddressSectionFieldValue($addressForm, DELIVERY_SECTION_SELECTOR, DELIVERY_FIELDS_SELECTOR, 'id_country', payload.id_country);
setAddressSectionFieldValue($addressForm, BILLING_SECTION_SELECTOR, BILLING_FIELDS_SELECTOR, 'invoice_id_country', payload.invoice_id_country);
setAddressSectionFieldValue($addressForm, BILLING_SECTION_SELECTOR, BILLING_FIELDS_SELECTOR, 'id_country', payload.invoice_id_country);
}

syncAllSavedAddressItemStyles();
Expand Down
18 changes: 8 additions & 10 deletions views/js/opc-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ const DISABLED_BY_SAME_ADDRESS_ATTRIBUTE = 'data-opc-disabled-by-same-address';

const SERVER_MANAGED_FIELDS = new Set([
'id_country',
'invoice_id_country',
'id_state',
'invoice_id_state',
'use_same_address',
]);

Expand Down Expand Up @@ -303,7 +301,7 @@ function seedBillingFromDelivery(addressContainer) {
addressContainer,
BILLING_SECTION_SELECTOR,
BILLING_FIELDS_SELECTOR
).find('[name="invoice_id_country"]').first();
).find('[name="id_country"]').first();
const previousBillingCountryValue = billingCountryField.length
? String(billingCountryField.val() || '')
: '';
Expand All @@ -312,7 +310,7 @@ function seedBillingFromDelivery(addressContainer) {
addressContainer,
BILLING_SECTION_SELECTOR,
BILLING_FIELDS_SELECTOR,
'invoice_id_country',
'id_country',
deliveryCountryValue
);

Expand All @@ -325,7 +323,7 @@ function seedBillingFromDelivery(addressContainer) {
addressContainer,
BILLING_SECTION_SELECTOR,
BILLING_FIELDS_SELECTOR,
'invoice_id_state',
'id_state',
deliveryStateValue
);
}
Expand Down Expand Up @@ -383,13 +381,13 @@ function refreshOpcAddressFormForCountryChange(target, selectors) {
}

// Send both country values so backend rebuilds delivery and billing sections consistently.
const targetName = String(target.attr('name') || '');
const deliveryCountryValue = targetName === 'id_country'
const isBillingTarget = target.closest(BILLING_SECTION_SELECTOR).length > 0;
const deliveryCountryValue = !isBillingTarget
? String(target.val() || '')
: getAddressSectionFieldValue(addressContainer, DELIVERY_SECTION_SELECTOR, DELIVERY_FIELDS_SELECTOR, 'id_country');
const invoiceCountryValue = targetName === 'invoice_id_country'
const invoiceCountryValue = isBillingTarget
? String(target.val() || '')
: getAddressSectionFieldValue(addressContainer, BILLING_SECTION_SELECTOR, BILLING_FIELDS_SELECTOR, 'invoice_id_country');
: getAddressSectionFieldValue(addressContainer, BILLING_SECTION_SELECTOR, BILLING_FIELDS_SELECTOR, 'id_country');
const requestData = {
id_address_delivery: getAddressSectionFieldValue(addressContainer, DELIVERY_SECTION_SELECTOR, DELIVERY_FIELDS_SELECTOR, 'id_address_delivery'),
id_address_invoice: getAddressSectionFieldValue(addressContainer, BILLING_SECTION_SELECTOR, BILLING_FIELDS_SELECTOR, 'id_address_invoice'),
Expand Down Expand Up @@ -439,7 +437,7 @@ function refreshOpcAddressFormForCountryChange(target, selectors) {
}

function handleOpcCountryChange(selectors) {
$('body').on('change', `${OPC_SELECTORS.opc.deliveryFields} select[name="id_country"], ${OPC_SELECTORS.opc.billingFields} select[name="invoice_id_country"]`, (event) => {
$('body').on('change', `${OPC_SELECTORS.opc.deliveryFields} select[name="id_country"], ${OPC_SELECTORS.opc.billingFields} select[name="id_country"]`, (event) => {
refreshOpcAddressFormForCountryChange($(event.target), selectors);
});
}
Expand Down
3 changes: 2 additions & 1 deletion views/js/opc-payment-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ function buildPaymentMethodsUrl(baseUrl) {
const idCountry = form.querySelector('[name="id_country"]')?.value
|| form.querySelector('[name="delivery_id_country"]')?.value
|| '';
const invoiceIdCountry = form.querySelector('[name="invoice_id_country"]')?.value || '';
const billingSection = form.querySelector(OPC_SELECTORS.opc.billingFields);
const invoiceIdCountry = (billingSection ? billingSection.querySelector('[name="id_country"]') : null)?.value || '';
const deliveryAddressId = getSelectedSavedAddressId(OPC_SELECTORS.opc.deliveryList, 'id_address_delivery')
|| form.querySelector('[name="id_address_delivery"]')?.value
|| '';
Expand Down
Loading