Fix address merge

This commit is contained in:
Alex P 2023-10-27 09:59:55 +03:00
parent 14f54e1df1
commit 7f072be611
No known key found for this signature in database
GPG key ID: 54487A734A204D71
4 changed files with 29 additions and 12 deletions

View file

@ -124,3 +124,29 @@ export const paypalOrderToWcAddresses = (order) => {
return {billingAddress, shippingAddress};
}
/**
* Merges two WC addresses.
* The objects can contain either the WC form fields or billingAddress, shippingAddress objects.
*
* @param {Object} address1
* @param {Object} address2
* @returns {any}
*/
export const mergeWcAddress = (address1, address2) => {
if ('billingAddress' in address1) {
return {
billingAddress: mergeWcAddress(address1.billingAddress, address2.billingAddress),
shippingAddress: mergeWcAddress(address1.shippingAddress, address2.shippingAddress),
}
}
let address2WithoutEmpty = {...address2};
Object.keys(address2).forEach(key => {
if (address2[key] === '') {
delete address2WithoutEmpty[key];
}
});
return {...address1, ...address2WithoutEmpty};
}

View file

@ -1,9 +1,8 @@
import {useEffect, useState} from '@wordpress/element';
import {registerExpressPaymentMethod, registerPaymentMethod} from '@woocommerce/blocks-registry';
import {paypalAddressToWc, paypalOrderToWcAddresses} from "./Helper/Address";
import {mergeWcAddress, paypalAddressToWc, paypalOrderToWcAddresses} from "./Helper/Address";
import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'
import buttonModuleWatcher from "../../../ppcp-button/resources/js/modules/ButtonModuleWatcher";
import merge from "deepmerge";
const config = wc.wcSettings.getSetting('ppcp-gateway_data');
@ -32,9 +31,7 @@ const PayPalComponent = ({
}
const paypalAddresses = paypalOrderToWcAddresses(config.scriptData.continuation.order);
const wcAddresses = wp.data.select('wc/store/cart').getCustomerData();
const addresses = merge(wcAddresses, paypalAddresses, {
customMerge: key => (a, b) => a ? a : b, // overwrite empty strings
});
const addresses = mergeWcAddress(wcAddresses, paypalAddresses);
wp.data.dispatch('wc/store/cart').setBillingAddress(addresses.billingAddress);
if (shippingData.needsShipping) {
wp.data.dispatch('wc/store/cart').setShippingAddress(addresses.shippingAddress);