Automattic\WooCommerce\Blocks\Domain\Services

CheckoutLink::get_checkout_link │ protected │ WC 1.0

Process the query params and return the checkout link to redirect to complete with session token.

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

Хуков нет.

Возвращает

string. The checkout link.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_checkout_link();

Код CheckoutLink::get_checkout_link() WC 11.3.0

protected function get_checkout_link() {
	$controller    = new CartController();
	$products      = $this->get_products_from_checkout_link();
	$errors        = new \WP_Error();
	$needs_options = [];
	$options_url   = '';

	foreach ( $products as $product_data ) {
		try {
			$controller->add_to_cart( $product_data );
		} catch ( \Exception $e ) {
			// Variations with an "Any" attribute need the shopper to choose a value, so send them to the product page.
			$product             = $this->is_missing_variation_data_error( $e ) ? wc_get_product( $product_data['id'] ) : null;
			$product_options_url = $product ? $this->get_product_options_url( $product, $product_data['variation'] ) : '';

			if ( ! $product || ! $product_options_url ) {
				$errors->add( 'error', $e->getMessage() );
				continue;
			}

			/* translators: %s: product name */
			$needs_options[] = sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), esc_html( $product->get_name() ) );

			if ( '' === $options_url ) {
				$options_url = $product_options_url;
			}
		}
	}

	if ( $needs_options ) {
		$errors->add(
			'checkout_link_needs_options',
			sprintf(
				/* translators: %s: comma-separated list of product names */
				_n( 'Choose options for %s to add it to your cart.', 'Choose options for %s to add them to your cart.', count( $needs_options ), 'woocommerce' ),
				wc_format_list_of_items( $needs_options )
			)
		);
	}

	// Nothing was added to the cart. Redirect to the product page if options are needed, otherwise the cart page, with
	// an error notice. Since guests may not have a session, add the notice in the query string.
	if ( wc()->cart->is_empty() ) {
		if ( $options_url ) {
			$redirect_url = $options_url;
			$notice_code  = 'checkout_link_needs_options';
		} else {
			$redirect_url = wc_get_cart_url();
			$notice_code  = 'error';
			$errors->add( 'error', __( 'The provided checkout link was out of date or invalid. No products were added to the cart.', 'woocommerce' ) );
		}

		if ( ! wc()->session->has_session() ) {
			return add_query_arg( 'wc_error', rawurlencode( $errors->get_error_message( $notice_code ) ), $redirect_url );
		}

		$this->add_error_notices( $errors );

		return $redirect_url;
	}

	// Apply coupon if provided.
	$coupon = wc_format_coupon_code( wp_unslash( $_GET['coupon'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	if ( wc_coupons_enabled() && ! empty( $coupon ) ) {
		try {
			$controller->apply_coupon( $coupon );
		} catch ( \Exception $e ) {
			$errors->add( 'error', $e->getMessage() );
		}
	}

	// Add error notices to the cart. This requires a session otherwise the notices will not be displayed.
	$this->add_error_notices( $errors );

	$redirect_url = $options_url ? $options_url : wc_get_checkout_url();

	// Preserve the query string--pass it to the checkout page.
	if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
		$redirect_url = remove_query_arg(
			[
				'products',
				'coupon',
				'checkout-link',
			],
			add_query_arg( wp_unslash( $_SERVER['QUERY_STRING'] ), '', $redirect_url ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		);
	}

	// If the user is logged in, the session is tied to the user ID. Do not use a cart token.
	if ( ! is_user_logged_in() ) {
		$session_token = CartTokenUtils::get_cart_token( (string) wc()->session->get_customer_id() );
		$redirect_url  = add_query_arg( 'session', $session_token, $redirect_url );
	}

	return $redirect_url;
}