Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::update_customer_from_requestprivateWC 1.0

Updates the current customer session using data from the request (e.g. address data).

Address session data is synced to the order itself later on by OrderController::update_order_from_cart()

Метод класса: Checkout{}

Возвращает

null. Ничего (null).

Использование

// private - только в коде основоного (родительского) класса
$result = $this->update_customer_from_request( $request );
$request(WP_REST_Request) (обязательный)
Full details about the request.

Код Checkout::update_customer_from_request() WC 10.3.4

private function update_customer_from_request( \WP_REST_Request $request ) {
	$customer                  = WC()->customer;
	$additional_field_contexts = [
		'shipping_address' => [
			'group'    => 'shipping',
			'location' => 'address',
			'param'    => 'shipping_address',
		],
		'billing_address'  => [
			'group'    => 'billing',
			'location' => 'address',
			'param'    => 'billing_address',
		],
		'contact'          => [
			'group'    => 'other',
			'location' => 'contact',
			'param'    => 'additional_fields',
		],
	];

	foreach ( $additional_field_contexts as $context => $context_data ) {

		$document_object = $this->get_document_object_from_rest_request( $request );
		$document_object->set_context( $context );
		$additional_fields = $this->additional_fields_controller->get_contextual_fields_for_location( $context_data['location'], $document_object );

		if ( 'shipping_address' === $context_data['param'] ) {
			$field_values = (array) $request['shipping_address'] ?? ( $request['billing_address'] ?? [] );

			if ( ! WC()->cart->needs_shipping() ) {
				$field_values = $request['billing_address'] ?? [];
			}
		} else {
			$field_values = (array) $request[ $context_data['param'] ] ?? [];
		}

		if ( 'address' === $context_data['location'] ) {
			$persist_keys = array_merge( $this->additional_fields_controller->get_address_fields_keys(), [ 'email' ], array_keys( $additional_fields ) );
		} else {
			$persist_keys = array_keys( $additional_fields );
		}

		foreach ( $field_values as $key => $value ) {
			if ( in_array( $key, $persist_keys, true ) ) {
				$this->update_customer_address_field( $customer, $key, $value, $context_data['group'] );
			}
		}
		wc_log_order_step( '[Store API #3::update_customer_from_request] Persisted ' . $context . ' fields' );
	}

	/**
	 * Fires when the Checkout Block/Store API updates a customer from the API request data.
	 *
	 * @since 8.2.0
	 *
	 * @param \WC_Customer $customer Customer object.
	 * @param \WP_REST_Request $request Full details about the request.
	 */
	do_action( 'woocommerce_store_api_checkout_update_customer_from_request', $customer, $request );

	$customer->save();
}