Amp_Cart::handle_redirect_request()

Summary

Gets the checkout URL for the current cart and redirects the user there.


Return

(void)


Source

File: src/BigCommerce/Amp/Amp_Cart.php

	public function handle_redirect_request() {
		$cart_id = $this->get_cart_id();

		if ( empty( $cart_id ) ) {
			$this->back_to_cart();
		}

		$request  = new WP_REST_Request(
			'POST',
			sprintf( '/%scarts/%s/redirect_urls', trailingslashit( $this->proxy_base ), $cart_id )
		);
		$response = rest_do_request( $request );

		if ( 200 !== $response->status || ! isset( $response->data['data']['checkout_url'] ) ) {
			$this->back_to_cart();
		}

		$url  = $response->data['data']['checkout_url'];
		$host = wp_parse_url( $url, PHP_URL_HOST );

		if ( empty( $host ) ) {
			$this->back_to_cart();
		}

		add_filter(
			'allowed_redirect_hosts',
			function( $hosts ) use ( $host ) {
				if ( false !== strpos( $host, 'bigcommerce.com' ) ) {
					$hosts[] = $host;
				}

				return $hosts;
			}
		);

		wp_safe_redirect( $response->data['data']['checkout_url'], 303 );
		die();
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.