WC_Gateway_Paypal::update_addresses_in_orderpublicWC 1.0

Update the shipping and billing information for the order. Hooked on woocommerce_before_thankyou.

Method of the class: WC_Gateway_Paypal{}

No Hooks.

Returns

null. Nothing (null).

Usage

$WC_Gateway_Paypal = new WC_Gateway_Paypal();
$WC_Gateway_Paypal->update_addresses_in_order( $order_id );
$order_id(int) (required)
The order ID.

WC_Gateway_Paypal::update_addresses_in_order() code WC 10.7.0

public function update_addresses_in_order( $order_id ) {
	$order = wc_get_order( $order_id );

	// Bail early if the order is not a PayPal order.
	if ( ! $order instanceof WC_Order || $order->get_payment_method() !== $this->id ) {
		return;
	}

	// Bail early if not on Orders v2.
	if ( ! $this->should_use_orders_v2() ) {
		return;
	}

	$paypal_order_id = $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_ORDER_ID );
	if ( empty( $paypal_order_id ) ) {
		return;
	}

	/**
	 * Bail early if the addresses update already have been attempted (whether successful or not).
	 * Prevent duplicate address update attempts from the thankyou page.
	 *
	 * Address updates are primarily handled by the PayPal webhook when the order is approved.
	 * This method serves as a fallback if the webhook hasn't fired yet,
	 * but we want to show the correct addresses to the customer on the thankyou page.
	 * Once an attempt is made (meta exists), we skip to prevent repeated API calls on page reloads.
	 * The webhook handler will always update the addresses.
	 */
	$addresses_update_attempted = $order->meta_exists( PayPalConstants::PAYPAL_ORDER_META_ADDRESSES_UPDATED );
	if ( $addresses_update_attempted ) {
		return;
	}

	try {
		$paypal_request       = new PayPalRequest( $this );
		$paypal_order_details = $paypal_request->get_paypal_order_details( $paypal_order_id );

		// Update the addresses in the order with the addresses from the PayPal order details.
		PayPalHelper::update_addresses_in_order( $order, $paypal_order_details );
	} catch ( Exception $e ) {
		self::log( 'Error updating addresses for order #' . $order_id . ': ' . $e->getMessage(), 'error' );
		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_ADDRESSES_UPDATED, 'no' );
		$order->save();
	}
}