WC_Checkout::process_customer()protectedWC 1.0

Create a new customer account if needed.

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

Возвращает

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

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->process_customer( $data );
$data(массив) (обязательный)
Posted data.

Код WC_Checkout::process_customer() WC 9.4.2

protected function process_customer( $data ) {
	/**
	 * This action is documented in woocommerce/includes/class-wc-checkout.php
	 *
	 * @since 3.0.0 or earlier
	 */
	$customer_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() );

	if ( ! is_user_logged_in() && ( $this->is_registration_required() || ! empty( $data['createaccount'] ) ) ) {
		$username    = ! empty( $data['account_username'] ) ? $data['account_username'] : '';
		$password    = ! empty( $data['account_password'] ) ? $data['account_password'] : '';
		$customer_id = wc_create_new_customer(
			$data['billing_email'],
			$username,
			$password,
			array(
				'first_name' => ! empty( $data['billing_first_name'] ) ? $data['billing_first_name'] : '',
				'last_name'  => ! empty( $data['billing_last_name'] ) ? $data['billing_last_name'] : '',
			)
		);

		if ( is_wp_error( $customer_id ) ) {
			if ( 'registration-error-email-exists' === $customer_id->get_error_code() ) {
				/**
				 * Filter the notice shown when a customer tries to register with an existing email address.
				 *
				 * @since 3.3.0
				 * @param string $message The notice.
				 * @param string $email   The email address.
				 */
				throw new Exception( apply_filters( 'woocommerce_registration_error_email_exists', __( 'An account is already registered with your email address. <a href="#" class="showlogin">Please log in.</a>', 'woocommerce' ), $data['billing_email'] ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
			}
			throw new Exception( $customer_id->get_error_message() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
		}

		wc_set_customer_auth_cookie( $customer_id );

		// As we are now logged in, checkout will need to refresh to show logged in data.
		WC()->session->set( 'reload_checkout', true );

		// Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering.
		WC()->cart->calculate_totals();
	}

	// On multisite, ensure user exists on current site, if not add them before allowing login.
	if ( $customer_id && is_multisite() && is_user_logged_in() && ! is_user_member_of_blog() ) {
		add_user_to_blog( get_current_blog_id(), $customer_id, 'customer' );
	}

	// Add customer info from other fields.
	if ( $customer_id && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) {
		$customer = new WC_Customer( $customer_id );

		if ( ! empty( $data['billing_first_name'] ) && '' === $customer->get_first_name() ) {
			$customer->set_first_name( $data['billing_first_name'] );
		}

		if ( ! empty( $data['billing_last_name'] ) && '' === $customer->get_last_name() ) {
			$customer->set_last_name( $data['billing_last_name'] );
		}

		// If the display name is an email, update to the user's full name.
		if ( is_email( $customer->get_display_name() ) ) {
			$customer->set_display_name( $customer->get_first_name() . ' ' . $customer->get_last_name() );
		}

		foreach ( $data as $key => $value ) {
			// Use setters where available.
			if ( is_callable( array( $customer, "set_{$key}" ) ) ) {
				$customer->{"set_{$key}"}( $value );

				// Store custom fields prefixed with wither shipping_ or billing_.
			} elseif ( 0 === stripos( $key, 'billing_' ) || 0 === stripos( $key, 'shipping_' ) ) {
				$customer->update_meta_data( $key, $value );
			}
		}

		/**
		 * Action hook to adjust customer before save.
		 *
		 * @since 3.0.0
		 */
		do_action( 'woocommerce_checkout_update_customer', $customer, $data );

		$customer->save();
	}

	do_action( 'woocommerce_checkout_update_user_meta', $customer_id, $data );
}