diff --git a/modules.local/ppcp-wc-gateway/services.php b/modules.local/ppcp-wc-gateway/services.php index cfe1fcdf6..76f610db1 100644 --- a/modules.local/ppcp-wc-gateway/services.php +++ b/modules.local/ppcp-wc-gateway/services.php @@ -9,6 +9,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Entity\ApplicationContext; use Inpsyde\PayPalCommerce\Onboarding\State; use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; +use Inpsyde\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset; use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways; use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway; use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; @@ -949,4 +950,11 @@ return [ ], ]; }, + + 'wcgateway.checkout.address-preset' => static function(ContainerInterface $container): CheckoutPayPalAddressPreset { + + return new CheckoutPayPalAddressPreset( + $container->get('session.handler') + ); + }, ]; diff --git a/modules.local/ppcp-wc-gateway/src/Checkout/CheckoutPayPalAddressPreset.php b/modules.local/ppcp-wc-gateway/src/Checkout/CheckoutPayPalAddressPreset.php new file mode 100644 index 000000000..8eb51f4d7 --- /dev/null +++ b/modules.local/ppcp-wc-gateway/src/Checkout/CheckoutPayPalAddressPreset.php @@ -0,0 +1,114 @@ +sessionHandler = $sessionHandler; + } + + /** + * @wp-hook woocommerce_checkout_get_value + * @param string|null + * @param string $fieldId + * + * @return string|null + */ + public function filterCheckoutFiled($defaultValue, $fieldId): ?string + { + if(! is_string($defaultValue)) { + $defaultValue = null; + } + + if(!is_string($fieldId)) { + return $defaultValue; + } + + return $this->readPresetForField($fieldId) ?? $defaultValue; + } + + private function readPresetForField(string $fieldId): ?string + { + if(!$order = $this->sessionHandler->order()) { + return null; + } + + $shipping = $this->readShippingFromOrder(); + $payer = $order->payer(); + + $addressMap = [ + 'billing_address_1' => 'addressLine1', + 'billing_address_2' => 'addressLine2', + 'billing_postcode' => 'postalCode', + 'billing_country' => 'countryCode', + 'billing_city' => 'adminArea2', + 'billing_state' => 'adminArea1' + ]; + $payerNameMap = [ + 'billing_last_name' => 'surname', + 'billing_first_name' => 'givenName', + ]; + $payerMap = [ + 'billing_email' => 'emailAddress', + 'billing_phone' => 'phone', + ]; + + if(array_key_exists($fieldId, $addressMap) && $shipping) { + return $shipping->address()->{$addressMap[$fieldId]}() ?: null; + } + + if(array_key_exists($fieldId, $payerNameMap) && $payer) { + return $payer->name()->{$payerNameMap[$fieldId]}() ?: null; + } + + if(array_key_exists($fieldId, $payerMap) && $payer) { + return $payer->{$payerMap[$fieldId]}() ?: null; + } + + return null; + } + + private function readShippingFromOrder(): ?Shipping + { + if(!$order = $this->sessionHandler->order()) { + return null; + } + + if(array_key_exists($order->id(), $this->shippingCache)) { + return $this->shippingCache[$order->id()]; + } + + $shipping = null; + foreach($this->sessionHandler->order()->purchaseUnits() as $unit) { + if($shipping = $unit->shipping()) { + break; + } + } + + $this->shippingCache[$order->id()] = $shipping; + + return $shipping; + } +} diff --git a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php index 81106a634..1c0bf0714 100644 --- a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php +++ b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php @@ -10,9 +10,9 @@ use Inpsyde\PayPalCommerce\AdminNotices\Repository\Repository; use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderDetail; use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; +use Inpsyde\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset; use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways; use Inpsyde\PayPalCommerce\WcGateway\Gateway\WcGateway; -use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings; use Inpsyde\PayPalCommerce\WcGateway\Settings\SettingsRenderer; @@ -34,6 +34,7 @@ class WcGatewayModule implements ModuleInterface $this->registerPaymentGateway($container); $this->registerOrderFunctionality($container); $this->registerColumns($container); + $this->registerCheckoutAddressPreset($container); add_filter( Repository::NOTICES_FILTER, @@ -201,4 +202,21 @@ class WcGatewayModule implements ModuleInterface 2 ); } + + private function registerCheckoutAddressPreset(ContainerInterface $container): void + { + add_filter( + 'woocommerce_checkout_get_value', + static function (...$args) use ($container) { + // It's important to not instantiate the service to early + // as it depends on SessionHandler and WooCommerce Session + /* @var CheckoutPayPalAddressPreset $service */ + $service = $container->get('wcgateway.checkout.address-preset'); + + return $service->filterCheckoutFiled(...$args); + }, + 10, + 2 + ); + } }