CheckoutFieldsSchema
11 months ago
Email
1 year ago
CheckoutFields.php
11 months ago
CheckoutFieldsAdmin.php
2 years ago
CheckoutFieldsFrontend.php
11 months ago
CheckoutLink.php
11 months ago
CreateAccount.php
1 year ago
DraftOrders.php
2 months ago
FeatureGating.php
1 year ago
GoogleAnalytics.php
2 years ago
Hydration.php
5 months ago
Notices.php
1 year ago
functions.php
2 years ago
CheckoutLink.php
203 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Functionality that takes a static URL, constructs a cart, and redirects to the checkout with a cart session. |
| 4 | */ |
| 5 | |
| 6 | declare(strict_types=1); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Blocks\Domain\Services; |
| 9 | |
| 10 | use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils; |
| 11 | use Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils; |
| 12 | use Automattic\WooCommerce\StoreApi\Utilities\CartController; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Checkout Link class. |
| 18 | */ |
| 19 | class CheckoutLink { |
| 20 | /** |
| 21 | * Initialize the checkout link service. |
| 22 | */ |
| 23 | public function init() { |
| 24 | add_action( 'init', array( $this, 'add_checkout_link_endpoint' ) ); |
| 25 | add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 ); |
| 26 | add_action( 'template_redirect', array( $this, 'handle_checkout_link_endpoint' ) ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Add the checkout link endpoint. |
| 31 | */ |
| 32 | public function add_checkout_link_endpoint() { |
| 33 | // get registered rewrite rules. |
| 34 | $rules = get_option( 'rewrite_rules', array() ); |
| 35 | $regex = '^checkout-link$'; |
| 36 | |
| 37 | add_rewrite_rule( $regex, 'index.php?checkout-link=true', 'top' ); |
| 38 | |
| 39 | // maybe flush rewrite rules if it was not previously in the option. |
| 40 | if ( ! isset( $rules[ $regex ] ) ) { |
| 41 | flush_rewrite_rules(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Add the checkout link query var. |
| 47 | * |
| 48 | * @param array $vars The query vars. |
| 49 | * @return array The query vars. |
| 50 | */ |
| 51 | public function add_query_vars( $vars ) { |
| 52 | $vars[] = 'checkout-link'; |
| 53 | return $vars; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Handle the checkout link endpoint. |
| 58 | * |
| 59 | * @return void |
| 60 | */ |
| 61 | public function handle_checkout_link_endpoint() { |
| 62 | if ( ! get_query_var( 'checkout-link' ) ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | if ( ! $this->validate_checkout_link() ) { |
| 67 | $redirect = add_query_arg( 'wc_error', rawurlencode( __( 'The provided checkout link was out of date or invalid. No products were added to the cart.', 'woocommerce' ) ), wc_get_cart_url() ); |
| 68 | } else { |
| 69 | wc()->cart->empty_cart(); |
| 70 | $redirect = $this->get_checkout_link(); |
| 71 | } |
| 72 | |
| 73 | wp_safe_redirect( $redirect ); |
| 74 | exit; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Validate the checkout link. |
| 79 | * |
| 80 | * @return bool True if the checkout link is valid, false otherwise. |
| 81 | */ |
| 82 | protected function validate_checkout_link() { |
| 83 | $products = $this->get_products_from_checkout_link(); |
| 84 | |
| 85 | return ! empty( $products ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the products from the checkout link. |
| 90 | * |
| 91 | * @return array The products (keys) and their quantities (values). |
| 92 | */ |
| 93 | protected function get_products_from_checkout_link() { |
| 94 | $raw_products = array_filter( explode( ',', wc_clean( wp_unslash( $_GET['products'] ?? '' ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 95 | $products = []; |
| 96 | |
| 97 | foreach ( $raw_products as $product_id_qty ) { |
| 98 | if ( strpos( $product_id_qty, ':' ) !== false ) { |
| 99 | list( $product_id, $qty ) = explode( ':', $product_id_qty ); |
| 100 | } else { |
| 101 | $product_id = $product_id_qty; |
| 102 | $qty = 1; |
| 103 | } |
| 104 | $product_id = absint( $product_id ); |
| 105 | $qty = absint( $qty ); |
| 106 | |
| 107 | if ( ! $product_id || ! $qty ) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | $products[ $product_id ] = $qty; |
| 112 | } |
| 113 | |
| 114 | return $products; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add error notices to the cart. |
| 119 | * |
| 120 | * @param \WP_Error $errors The errors. |
| 121 | * @return void |
| 122 | */ |
| 123 | protected function add_error_notices( \WP_Error $errors ) { |
| 124 | foreach ( $errors->get_error_messages() as $message ) { |
| 125 | wc_add_notice( $message, 'error' ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Process the query params and return the checkout link to redirect to complete with session token. |
| 131 | * |
| 132 | * @return string The checkout link. |
| 133 | */ |
| 134 | protected function get_checkout_link() { |
| 135 | $controller = new CartController(); |
| 136 | $products = $this->get_products_from_checkout_link(); |
| 137 | $errors = new \WP_Error(); |
| 138 | |
| 139 | foreach ( $products as $product_id => $qty ) { |
| 140 | try { |
| 141 | $controller->add_to_cart( |
| 142 | [ |
| 143 | 'id' => $product_id, |
| 144 | 'quantity' => $qty, |
| 145 | ] |
| 146 | ); |
| 147 | } catch ( \Exception $e ) { |
| 148 | $errors->add( 'error', $e->getMessage() ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Nothing was added to the cart. We need to redirect to the cart page with an error notice. Since guests may not |
| 153 | // have a session, add the notice in the query string. |
| 154 | if ( wc()->cart->is_empty() ) { |
| 155 | $errors->add( 'error', __( 'The provided checkout link was out of date or invalid. No products were added to the cart.', 'woocommerce' ) ); |
| 156 | |
| 157 | if ( ! wc()->session->has_session() ) { |
| 158 | return add_query_arg( 'wc_error', rawurlencode( $errors->get_error_message() ), wc_get_cart_url() ); |
| 159 | } else { |
| 160 | $this->add_error_notices( $errors ); |
| 161 | } |
| 162 | |
| 163 | return wc_get_cart_url(); |
| 164 | } |
| 165 | |
| 166 | // Apply coupon if provided. |
| 167 | $coupon = wc_format_coupon_code( wp_unslash( $_GET['coupon'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 168 | |
| 169 | if ( wc_coupons_enabled() && ! empty( $coupon ) ) { |
| 170 | try { |
| 171 | $controller->apply_coupon( $coupon ); |
| 172 | } catch ( \Exception $e ) { |
| 173 | $errors->add( 'error', $e->getMessage() ); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Add error notices to the cart. This requires a session otherwise the notices will not be displayed. |
| 178 | $this->add_error_notices( $errors ); |
| 179 | |
| 180 | $redirect_url = wc_get_checkout_url(); |
| 181 | |
| 182 | // Preserve the query string--pass it to the checkout page. |
| 183 | if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { |
| 184 | $redirect_url = remove_query_arg( |
| 185 | [ |
| 186 | 'products', |
| 187 | 'coupon', |
| 188 | 'checkout-link', |
| 189 | ], |
| 190 | add_query_arg( wp_unslash( $_SERVER['QUERY_STRING'] ), '', $redirect_url ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | // If the user is logged in, the session is tied to the user ID. Do not use a cart token. |
| 195 | if ( ! is_user_logged_in() ) { |
| 196 | $session_token = CartTokenUtils::get_cart_token( (string) wc()->session->get_customer_id() ); |
| 197 | $redirect_url = add_query_arg( 'session', $session_token, $redirect_url ); |
| 198 | } |
| 199 | |
| 200 | return $redirect_url; |
| 201 | } |
| 202 | } |
| 203 |