Home / Admin / WP Simple Pay: Add Customer Shipping Address to Payment Metadata
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Add Customer Shipping Address to Payment Metadata

Maps the customer's shipping address to the one-time payment record's metadata.

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/snippet/m5ljwkl2/
 */
add_filter(
	'simpay_get_paymentintent_args_from_payment_form_request',
	function( $args, $form ) {
		$customer = \SimplePay\Core\API\Customers\retrieve( $args['customer'], $form->get_api_request_args() );
		$shipping_address = $customer->shipping->address;
		$shipping_name = $customer->shipping->name;
		$shipping_phone = $customer->shipping->phone;
		$args['metadata']['Shipping-name'] = $shipping_name;
		$args['metadata']['Shipping-phone'] = $shipping_phone;
		foreach ( $shipping_address->toArray() as $key => $value ) {
			$args['metadata'][ 'Shipping-' . $key ] = $value;
		}
		return $args;
	},
	10,
	2
);

Comments

Add a Comment