From 7f072be611a33d2393b90d7888df2790382d093e Mon Sep 17 00:00:00 2001 From: Alex P Date: Fri, 27 Oct 2023 09:59:55 +0300 Subject: [PATCH] Fix address merge --- modules/ppcp-blocks/package.json | 3 +-- .../resources/js/Helper/Address.js | 26 +++++++++++++++++++ .../resources/js/checkout-block.js | 7 ++--- modules/ppcp-blocks/yarn.lock | 5 ---- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/modules/ppcp-blocks/package.json b/modules/ppcp-blocks/package.json index 501915e75..53c08b7bb 100644 --- a/modules/ppcp-blocks/package.json +++ b/modules/ppcp-blocks/package.json @@ -10,8 +10,7 @@ "Edge >= 14" ], "dependencies": { - "core-js": "^3.25.0", - "deepmerge": "^4.3.1" + "core-js": "^3.25.0" }, "devDependencies": { "@babel/core": "^7.19", diff --git a/modules/ppcp-blocks/resources/js/Helper/Address.js b/modules/ppcp-blocks/resources/js/Helper/Address.js index d522d208e..6a999f141 100644 --- a/modules/ppcp-blocks/resources/js/Helper/Address.js +++ b/modules/ppcp-blocks/resources/js/Helper/Address.js @@ -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}; +} diff --git a/modules/ppcp-blocks/resources/js/checkout-block.js b/modules/ppcp-blocks/resources/js/checkout-block.js index 314c06054..582fdede3 100644 --- a/modules/ppcp-blocks/resources/js/checkout-block.js +++ b/modules/ppcp-blocks/resources/js/checkout-block.js @@ -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); diff --git a/modules/ppcp-blocks/yarn.lock b/modules/ppcp-blocks/yarn.lock index 161d649ba..209adc4a4 100644 --- a/modules/ppcp-blocks/yarn.lock +++ b/modules/ppcp-blocks/yarn.lock @@ -1425,11 +1425,6 @@ debug@^4.1.0, debug@^4.1.1: dependencies: ms "2.1.2" -deepmerge@^4.3.1: - version "4.3.1" - resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.3.1.tgz#44b5f2147cd3b00d4b56137685966f26fd25dd4a" - integrity sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A== - electron-to-chromium@^1.4.251: version "1.4.284" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.284.tgz#61046d1e4cab3a25238f6bf7413795270f125592"