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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Blocks / Domain / Services / CheckoutLink.php
woocommerce / src / Blocks / Domain / Services Last commit date
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