Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::update_customer_from_request()privateWC 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 9.4.2

private function update_customer_from_request( \WP_REST_Request $request ) {
	$customer = wc()->customer;

	// Billing address is a required field.
	foreach ( $request['billing_address'] as $key => $value ) {
		$callback = "set_billing_$key";
		if ( is_callable( [ $customer, $callback ] ) ) {
			$customer->$callback( $value );
		} elseif ( $this->additional_fields_controller->is_field( $key ) ) {
			$this->additional_fields_controller->persist_field_for_customer( $key, $value, $customer, 'billing' );
		}
	}

	// If shipping address (optional field) was not provided, set it to the given billing address (required field).
	$shipping_address_values = $request['shipping_address'] ?? $request['billing_address'];

	foreach ( $shipping_address_values as $key => $value ) {
		$callback = "set_shipping_$key";
		if ( is_callable( [ $customer, $callback ] ) ) {
			$customer->$callback( $value );
		} elseif ( $this->additional_fields_controller->is_field( $key ) ) {
			$this->additional_fields_controller->persist_field_for_customer( $key, $value, $customer, 'shipping' );
		}
	}

	// Persist contact fields to session.
	$contact_fields = $this->additional_fields_controller->get_contact_fields_keys();

	if ( ! empty( $contact_fields ) ) {
		foreach ( $contact_fields as $key ) {
			if ( isset( $request['additional_fields'], $request['additional_fields'][ $key ] ) ) {
				$this->additional_fields_controller->persist_field_for_customer( $key, $request['additional_fields'][ $key ], $customer );
			}
		}
	}

	/**
	 * 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();
}