🗃️ Store custom contact details in order-meta

This commit is contained in:
Philipp Stracker 2025-06-16 19:34:46 +02:00
parent e58532120b
commit 68e931331b
No known key found for this signature in database
2 changed files with 47 additions and 0 deletions

View file

@ -55,6 +55,8 @@ class PayPalGateway extends \WC_Payment_Gateway {
public const REFUNDS_META_KEY = '_ppcp_refunds';
public const THREE_D_AUTH_RESULT_META_KEY = '_ppcp_paypal_3DS_auth_result';
public const FRAUD_RESULT_META_KEY = '_ppcp_paypal_fraud_result';
public const CONTACT_EMAIL_META_KEY = '_ppcp_paypal_contact_email';
public const CONTACT_PHONE_META_KEY = '_ppcp_paypal_contact_phone';
/**
* List of payment sources for which we are expected to store the payer email in the WC Order metadata.

View file

@ -14,6 +14,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderTransient;
use WooCommerce\PayPalCommerce\WcGateway\Helper\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Shipping;
/**
* Trait OrderMetaTrait.
@ -57,11 +58,55 @@ trait OrderMetaTrait {
}
}
$this->add_contact_details_to_wc_order( $wc_order, $order );
$wc_order->save();
do_action( 'woocommerce_paypal_payments_woocommerce_order_created', $wc_order, $order );
}
/**
* Add order-meta entries with custom contact details
*
* @param WC_Order $wc_order The WooCommerce order to update.
* @param Order $order The PayPal order which provides the details.
* @return void
*/
private function add_contact_details_to_wc_order( WC_Order $wc_order, Order $order ) : void {
$shipping_details = $this->get_shipping_details( $order );
if ( ! $shipping_details ) {
return;
}
$contact_email = $shipping_details->email_address();
$contact_phone = $shipping_details->phone_number();
if ( $contact_email ) {
$wc_order->update_meta_data( PayPalGateway::CONTACT_EMAIL_META_KEY, $contact_email );
}
if ( $contact_phone ) {
$wc_order->update_meta_data( PayPalGateway::CONTACT_PHONE_META_KEY, $contact_phone->national_number() );
}
}
/**
* Returns the shipping address details for the order.
*
* @param Order $order The PayPal order that contains potential shipping information.
* @return ?Shipping The shipping details, or null if none present.
*/
private function get_shipping_details( Order $order ) : ?Shipping {
foreach ( $order->purchase_units() as $unit ) {
$shipping = $unit->shipping();
if ( $shipping ) {
return $shipping;
}
}
return null;
}
/**
* Returns the payment source type or null,
*