Checkout.php
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 4 | |
| 5 | use Automattic\WooCommerce\StoreApi\Payments\PaymentResult; |
| 6 | use Automattic\WooCommerce\StoreApi\Exceptions\InvalidCartException; |
| 7 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 8 | use Automattic\WooCommerce\StoreApi\Utilities\DraftOrderTrait; |
| 9 | use Automattic\WooCommerce\Checkout\Helpers\ReserveStockException; |
| 10 | use Automattic\WooCommerce\StoreApi\Utilities\CheckoutTrait; |
| 11 | |
| 12 | /** |
| 13 | * Checkout class. |
| 14 | */ |
| 15 | class Checkout extends AbstractCartRoute { |
| 16 | use DraftOrderTrait; |
| 17 | use CheckoutTrait; |
| 18 | |
| 19 | /** |
| 20 | * The route identifier. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | const IDENTIFIER = 'checkout'; |
| 25 | |
| 26 | /** |
| 27 | * The routes schema. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | const SCHEMA_TYPE = 'checkout'; |
| 32 | |
| 33 | /** |
| 34 | * Holds the current order being processed. Null until `create_or_update_draft_order()` |
| 35 | * materialises it (either by reusing the session's pending/failed order or by creating |
| 36 | * a new one from the cart). |
| 37 | * |
| 38 | * @var \WC_Order|null |
| 39 | */ |
| 40 | private $order = null; |
| 41 | |
| 42 | /** |
| 43 | * Get the path of this REST route. |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | public function get_path() { |
| 48 | return self::get_path_regex(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the path of this rest route. |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | public static function get_path_regex() { |
| 57 | return '/checkout'; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Checks if a nonce is required for the route. |
| 62 | * |
| 63 | * @param \WP_REST_Request $request Request. |
| 64 | * @return bool |
| 65 | */ |
| 66 | protected function requires_nonce( \WP_REST_Request $request ) { |
| 67 | return ! $this->has_cart_token( $request ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get method arguments for this REST route. |
| 72 | * |
| 73 | * @return array An array of endpoints. |
| 74 | */ |
| 75 | public function get_args() { |
| 76 | return [ |
| 77 | [ |
| 78 | 'methods' => \WP_REST_Server::READABLE, |
| 79 | 'callback' => [ $this, 'get_response' ], |
| 80 | 'permission_callback' => '__return_true', |
| 81 | 'args' => [ |
| 82 | 'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
| 83 | ], |
| 84 | ], |
| 85 | [ |
| 86 | 'methods' => \WP_REST_Server::CREATABLE, |
| 87 | 'callback' => [ $this, 'get_response' ], |
| 88 | 'permission_callback' => '__return_true', |
| 89 | 'args' => array_merge( |
| 90 | [ |
| 91 | 'payment_data' => [ |
| 92 | 'description' => __( 'Data to pass through to the payment method when processing payment.', 'woocommerce' ), |
| 93 | 'type' => 'array', |
| 94 | 'items' => [ |
| 95 | 'type' => 'object', |
| 96 | 'properties' => [ |
| 97 | 'key' => [ |
| 98 | 'type' => 'string', |
| 99 | ], |
| 100 | 'value' => [ |
| 101 | 'type' => [ 'string', 'boolean' ], |
| 102 | ], |
| 103 | ], |
| 104 | ], |
| 105 | ], |
| 106 | 'customer_password' => [ |
| 107 | 'description' => __( 'Customer password for new accounts, if applicable.', 'woocommerce' ), |
| 108 | 'type' => 'string', |
| 109 | ], |
| 110 | ], |
| 111 | $this->schema->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ) |
| 112 | ), |
| 113 | ], |
| 114 | [ |
| 115 | 'methods' => \WP_REST_Server::EDITABLE, |
| 116 | 'callback' => [ $this, 'get_response' ], |
| 117 | 'permission_callback' => '__return_true', |
| 118 | 'args' => array_merge( |
| 119 | [ |
| 120 | 'additional_fields' => [ |
| 121 | 'description' => __( 'Additional fields related to the order.', 'woocommerce' ), |
| 122 | 'type' => 'object', |
| 123 | ], |
| 124 | 'payment_method' => [ |
| 125 | 'description' => __( 'Selected payment method for the order.', 'woocommerce' ), |
| 126 | 'type' => 'string', |
| 127 | ], |
| 128 | 'order_notes' => [ |
| 129 | 'description' => __( 'Order notes.', 'woocommerce' ), |
| 130 | 'type' => 'string', |
| 131 | ], |
| 132 | ], |
| 133 | $this->schema->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ) |
| 134 | ), |
| 135 | ], |
| 136 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 137 | 'allow_batch' => [ 'v1' => true ], |
| 138 | ]; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get the route response based on the type of request. |
| 143 | * |
| 144 | * @param \WP_REST_Request $request Request object. |
| 145 | * |
| 146 | * @return \WP_REST_Response |
| 147 | */ |
| 148 | public function get_response( \WP_REST_Request $request ) { |
| 149 | $this->load_cart_session( $request ); |
| 150 | |
| 151 | $response = null; |
| 152 | $nonce_check = $this->requires_nonce( $request ) ? $this->check_nonce( $request ) : null; |
| 153 | |
| 154 | if ( is_wp_error( $nonce_check ) ) { |
| 155 | $response = $nonce_check; |
| 156 | } |
| 157 | |
| 158 | if ( ! $response ) { |
| 159 | try { |
| 160 | $response = $this->get_response_by_request_method( $request ); |
| 161 | } catch ( InvalidCartException $error ) { |
| 162 | $response = $this->get_route_error_response_from_object( $error->getError(), $error->getCode(), $error->getAdditionalData() ); |
| 163 | } catch ( RouteException $error ) { |
| 164 | $response = $this->get_route_error_response( $error->getErrorCode(), $error->getMessage(), $error->getCode(), $error->getAdditionalData() ); |
| 165 | } catch ( \Exception $error ) { |
| 166 | $response = $this->get_route_error_response( 'woocommerce_rest_unknown_server_error', $error->getMessage(), 500 ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if ( is_wp_error( $response ) ) { |
| 171 | $response = $this->error_to_response( $response ); |
| 172 | |
| 173 | // If we encountered an exception, free up stock and release held coupons. |
| 174 | if ( $this->order ) { |
| 175 | wc_release_stock_for_order( $this->order ); |
| 176 | wc_release_coupons_for_order( $this->order ); |
| 177 | } |
| 178 | |
| 179 | if ( $request->get_method() === \WP_REST_Server::CREATABLE ) { |
| 180 | // Step logs the exception. If nothing abnormal occurred during the place order POST request, flow the log is removed. |
| 181 | wc_log_order_step( |
| 182 | '[Store API #FAIL] Placing Order failed', |
| 183 | array( |
| 184 | 'status' => $response->get_status(), |
| 185 | 'data' => $response->get_data(), |
| 186 | ), |
| 187 | true |
| 188 | ); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return $this->add_response_headers( $response ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Return a checkout response for GET requests. |
| 197 | * |
| 198 | * If a `pending`/`failed` order from a previous payment attempt is in the customer |
| 199 | * session, reuse it (the failed-payment retry path). Otherwise build a no-order |
| 200 | * response directly from cart + customer + request. |
| 201 | * |
| 202 | * @throws RouteException On error. |
| 203 | * @param \WP_REST_Request $request Request object. |
| 204 | * @return \WP_REST_Response |
| 205 | */ |
| 206 | protected function get_route_response( \WP_REST_Request $request ) { |
| 207 | $this->order = $this->get_draft_order(); |
| 208 | |
| 209 | if ( $this->order ) { |
| 210 | $this->create_or_update_draft_order( $request ); |
| 211 | |
| 212 | return $this->prepare_item_for_response( |
| 213 | (object) [ |
| 214 | 'order' => $this->order, |
| 215 | 'payment_result' => new PaymentResult(), |
| 216 | ], |
| 217 | $request |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | return $this->build_draft_route_response( $request ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Validation callback for the checkout route. |
| 226 | * |
| 227 | * This runs after individual field validation_callbacks have been called. |
| 228 | * |
| 229 | * @param \WP_REST_Request $request Request object. |
| 230 | * @return true|\WP_Error |
| 231 | */ |
| 232 | public function validate_callback( $request ) { |
| 233 | $validate_contexts = [ |
| 234 | 'shipping_address' => [ |
| 235 | 'group' => 'shipping', |
| 236 | 'location' => 'address', |
| 237 | 'param' => 'shipping_address', |
| 238 | ], |
| 239 | 'billing_address' => [ |
| 240 | 'group' => 'billing', |
| 241 | 'location' => 'address', |
| 242 | 'param' => 'billing_address', |
| 243 | ], |
| 244 | 'contact' => [ |
| 245 | 'group' => 'other', |
| 246 | 'location' => 'contact', |
| 247 | 'param' => 'additional_fields', |
| 248 | ], |
| 249 | 'order' => [ |
| 250 | 'group' => 'other', |
| 251 | 'location' => 'order', |
| 252 | 'param' => 'additional_fields', |
| 253 | ], |
| 254 | ]; |
| 255 | |
| 256 | if ( ! WC()->cart->needs_shipping() ) { |
| 257 | unset( $validate_contexts['shipping_address'] ); |
| 258 | } |
| 259 | |
| 260 | $invalid_groups = []; |
| 261 | $invalid_details = []; |
| 262 | $is_partial = in_array( $request->get_method(), [ 'PUT', 'PATCH' ], true ); |
| 263 | |
| 264 | foreach ( $validate_contexts as $context => $context_data ) { |
| 265 | $errors = new \WP_Error(); |
| 266 | |
| 267 | $document_object = $this->get_document_object_from_rest_request( $request ); |
| 268 | $document_object->set_context( $context ); |
| 269 | $additional_fields = $this->additional_fields_controller->get_contextual_fields_for_location( $context_data['location'], $document_object ); |
| 270 | |
| 271 | // These values are used to validate custom rules and generate the document object. |
| 272 | $field_values = (array) $request->get_param( $context_data['param'] ) ?? []; |
| 273 | |
| 274 | foreach ( $additional_fields as $field_key => $field ) { |
| 275 | // Skip values that were not posted if the request is partial or the field is not required. |
| 276 | if ( ! isset( $field_values[ $field_key ] ) && ( $is_partial || true !== $field['required'] ) ) { |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | // Clean the field value to trim whitespace. Request body is JSON-decoded and never magic-quoted, so no wp_unslash(). |
| 281 | $field_value = wc_clean( $field_values[ $field_key ] ?? '' ); |
| 282 | |
| 283 | if ( empty( $field_value ) ) { |
| 284 | if ( true === $field['required'] ) { |
| 285 | /* translators: %s: is the field label */ |
| 286 | $error_message = sprintf( __( '%s is required', 'woocommerce' ), $field['label'] ); |
| 287 | if ( 'shipping_address' === $context ) { |
| 288 | /* translators: %s: is the field error message */ |
| 289 | $error_message = sprintf( __( 'There was a problem with the provided shipping address: %s', 'woocommerce' ), $error_message ); |
| 290 | } elseif ( 'billing_address' === $context ) { |
| 291 | /* translators: %s: is the field error message */ |
| 292 | $error_message = sprintf( __( 'There was a problem with the provided billing address: %s', 'woocommerce' ), $error_message ); |
| 293 | } |
| 294 | $errors->add( 'woocommerce_required_checkout_field', $error_message, [ 'key' => $field_key ] ); |
| 295 | } |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | $valid_check = $this->additional_fields_controller->validate_field( $field, $field_value ); |
| 300 | |
| 301 | if ( is_wp_error( $valid_check ) && $valid_check->has_errors() ) { |
| 302 | foreach ( $valid_check->get_error_codes() as $code ) { |
| 303 | $valid_check->add_data( |
| 304 | array( |
| 305 | 'location' => $context_data['location'], |
| 306 | 'key' => $field_key, |
| 307 | ), |
| 308 | $code |
| 309 | ); |
| 310 | } |
| 311 | $errors->merge_from( $valid_check ); |
| 312 | continue; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Validate all fields for this location (this runs custom validation callbacks). |
| 317 | $valid_location_check = $this->additional_fields_controller->validate_fields_for_location( $field_values, $context_data['location'], $context_data['group'] ); |
| 318 | |
| 319 | if ( is_wp_error( $valid_location_check ) && $valid_location_check->has_errors() ) { |
| 320 | foreach ( $valid_location_check->get_error_codes() as $code ) { |
| 321 | $valid_location_check->add_data( |
| 322 | array( |
| 323 | 'location' => $context_data['location'], |
| 324 | ), |
| 325 | $code |
| 326 | ); |
| 327 | } |
| 328 | $errors->merge_from( $valid_location_check ); |
| 329 | } |
| 330 | |
| 331 | if ( $errors->has_errors() ) { |
| 332 | $invalid_groups[ $context_data['param'] ] = $errors->get_error_message(); |
| 333 | $invalid_details[ $context_data['param'] ] = rest_convert_error_to_response( $errors )->get_data(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if ( $invalid_groups ) { |
| 338 | return new \WP_Error( |
| 339 | 'rest_invalid_param', |
| 340 | /* translators: %s: List of invalid parameters. */ |
| 341 | esc_html( sprintf( __( 'Invalid parameter(s): %s', 'woocommerce' ), implode( ', ', array_keys( $invalid_groups ) ) ) ), |
| 342 | array( |
| 343 | 'status' => 400, |
| 344 | 'params' => $invalid_groups, |
| 345 | 'details' => $invalid_details, |
| 346 | ) |
| 347 | ); |
| 348 | } |
| 349 | |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Get route response for PUT/PATCH requests. |
| 355 | * |
| 356 | * Branches on whether a pending/failed order already exists in the customer's |
| 357 | * session: |
| 358 | * |
| 359 | * - Order in session (failed-payment retry): update the existing order via |
| 360 | * `create_or_update_draft_order()` + `update_order_from_request()`. Same |
| 361 | * shape as the POST flow. |
| 362 | * - No order in session (fresh checkout form interaction): persist request |
| 363 | * state to the customer session via `update_session_from_request()` and |
| 364 | * return a no-order response built from cart + customer + request. |
| 365 | * |
| 366 | * Draft order creation is deferred to POST (place-order time) to avoid |
| 367 | * orphaned `wc-checkout-draft` rows from form interactions that never |
| 368 | * complete. POSTs do not flow through this method — see |
| 369 | * `get_route_post_response()`. |
| 370 | * |
| 371 | * @param \WP_REST_Request $request Request object. |
| 372 | * @throws RouteException On error. |
| 373 | * @return \WP_REST_Response|\WP_Error |
| 374 | */ |
| 375 | protected function get_route_update_response( \WP_REST_Request $request ) { |
| 376 | $validation_callback = $this->validate_callback( $request ); |
| 377 | |
| 378 | if ( is_wp_error( $validation_callback ) ) { |
| 379 | return $validation_callback; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Create (or update) Draft Order and process request data. |
| 384 | */ |
| 385 | $this->order = $this->get_draft_order(); |
| 386 | |
| 387 | if ( $this->order ) { |
| 388 | $this->create_or_update_draft_order( $request ); |
| 389 | // Order save-point: 1. |
| 390 | |
| 391 | /** |
| 392 | * Persist additional fields, order notes and payment method for order. |
| 393 | */ |
| 394 | $this->update_order_from_request( $request ); |
| 395 | // Order save-point: 2. |
| 396 | } else { |
| 397 | $this->update_session_from_request( $request ); |
| 398 | } |
| 399 | |
| 400 | if ( $request->get_param( '__experimental_calc_totals' ) ) { |
| 401 | /** |
| 402 | * Before triggering validation, ensure totals are current and in turn, things such as shipping costs are present. |
| 403 | * This is so plugins that validate other cart data (e.g. conditional shipping and payments) can access this data. |
| 404 | */ |
| 405 | $this->cart_controller->calculate_totals(); |
| 406 | /** |
| 407 | * Validate that the cart is not empty. |
| 408 | */ |
| 409 | $this->cart_controller->validate_cart_not_empty(); |
| 410 | |
| 411 | /** |
| 412 | * Validate items and fix violations before the order is processed. |
| 413 | */ |
| 414 | $this->cart_controller->validate_cart(); |
| 415 | } |
| 416 | |
| 417 | if ( $this->order ) { |
| 418 | return $this->prepare_item_for_response( |
| 419 | (object) [ |
| 420 | 'order' => wc_get_order( $this->order ), |
| 421 | 'cart' => $this->cart_controller->get_cart_instance(), |
| 422 | ], |
| 423 | $request |
| 424 | ); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Fires after a Store API checkout PATCH request has been validated and live |
| 429 | * customer/session state has been updated, before the response is returned. |
| 430 | * |
| 431 | * Hook this action when an extension needs to observe live checkout state — e.g. |
| 432 | * abandoned-cart trackers, side-panel previews, conditional shipping or payment |
| 433 | * validators, or anything else that needs to react to every customer interaction |
| 434 | * with the form. |
| 435 | * |
| 436 | * No `WC_Order` exists at this point under deferred draft order creation. Read |
| 437 | * checkout state from `WC()->cart`, `WC()->customer`, and the supplied |
| 438 | * `$request`, and persist any extension-owned state to `WC()->session`. To |
| 439 | * apply that state to the real order at place-order time, hook |
| 440 | * `woocommerce_store_api_checkout_update_order_meta` or |
| 441 | * `woocommerce_store_api_checkout_update_order_from_request` — both fire |
| 442 | * against the real, persisted order at POST exactly as they always have. |
| 443 | * |
| 444 | * @since 10.8.0 |
| 445 | * |
| 446 | * @param \WP_REST_Request $request The current PATCH request. |
| 447 | */ |
| 448 | do_action( 'woocommerce_store_api_checkout_update_draft', $request ); |
| 449 | |
| 450 | return $this->build_draft_route_response( $request ); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Persist the PATCH request's payment method and additional fields to the customer |
| 455 | * session. Counterpart to `update_order_from_request` for the no-order PATCH path. |
| 456 | * |
| 457 | * @phpstan-param \WP_REST_Request<array<string, mixed>> $request |
| 458 | * |
| 459 | * @param \WP_REST_Request $request Request object. |
| 460 | * @throws RouteException If the supplied payment method id is unknown or disabled. |
| 461 | */ |
| 462 | private function update_session_from_request( \WP_REST_Request $request ): void { |
| 463 | $payment_method = $this->get_request_payment_method( $request ); |
| 464 | if ( null !== $payment_method ) { |
| 465 | WC()->session->set( 'chosen_payment_method', $payment_method->id ); |
| 466 | } |
| 467 | if ( isset( $request['order_notes'] ) ) { |
| 468 | WC()->session->set( 'store_api_customer_note', wc_sanitize_textarea( $request['order_notes'] ) ); |
| 469 | } |
| 470 | $this->persist_additional_fields_for_customer( $request ); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Build a checkout response for a session with no order in flight. |
| 475 | * |
| 476 | * @phpstan-param \WP_REST_Request<array<string, mixed>> $request |
| 477 | * |
| 478 | * @param \WP_REST_Request $request Request object. |
| 479 | * @return \WP_REST_Response |
| 480 | */ |
| 481 | private function build_draft_route_response( \WP_REST_Request $request ) { |
| 482 | /** |
| 483 | * Narrow the parent-declared schema property to the checkout subclass for phpstan. |
| 484 | * |
| 485 | * @var \Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema $schema |
| 486 | */ |
| 487 | $schema = $this->schema; |
| 488 | |
| 489 | return new \WP_REST_Response( |
| 490 | $schema->get_draft_response( |
| 491 | $this->cart_controller->get_cart_instance(), |
| 492 | wc()->customer |
| 493 | ) |
| 494 | ); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Process an order. |
| 499 | * |
| 500 | * @throws RouteException On error. |
| 501 | * @param \WP_REST_Request<array<string, mixed>> $request Request object. |
| 502 | * @return \WP_REST_Response|\WP_Error |
| 503 | */ |
| 504 | protected function get_route_post_response( \WP_REST_Request $request ) { // phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint |
| 505 | try { |
| 506 | return $this->process_order( $request ); |
| 507 | } catch ( \Throwable $exception ) { |
| 508 | if ( $this->order ) { |
| 509 | // The optimistic order save bounced back, persist the order as it is to preserve the intermediate state. |
| 510 | try { |
| 511 | $this->order->save(); |
| 512 | } catch ( \Throwable $save_exception ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 513 | // Ignore the save exception, the root cause will be bubbled up via re-throwing $exception. |
| 514 | } |
| 515 | } |
| 516 | throw $exception; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Process an order based on optimistic save approach to minimize the number of order saves. |
| 522 | * |
| 523 | * 1. Obtain Draft Order |
| 524 | * 2. Process Request |
| 525 | * 3. Process Customer |
| 526 | * 4. Validate Order |
| 527 | * 5. Process Payment |
| 528 | * |
| 529 | * @throws RouteException On error. |
| 530 | * @param \WP_REST_Request<array<string, mixed>> $request Request object. |
| 531 | * @return \WP_REST_Response|\WP_Error |
| 532 | */ |
| 533 | private function process_order( \WP_REST_Request $request ) { // phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint |
| 534 | wc_log_order_step( '[Store API #1] Place Order flow initiated', null, false, true ); |
| 535 | |
| 536 | $validation_callback = $this->validate_callback( $request ); |
| 537 | |
| 538 | if ( is_wp_error( $validation_callback ) ) { |
| 539 | return $validation_callback; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Ensure required permissions based on store settings are valid to place the order. |
| 544 | */ |
| 545 | $this->validate_user_can_place_order(); |
| 546 | |
| 547 | /** |
| 548 | * Before triggering validation, ensure totals are current and in turn, things such as shipping costs are present. |
| 549 | * This is so plugins that validate other cart data (e.g. conditional shipping and payments) can access this data. |
| 550 | */ |
| 551 | $this->cart_controller->calculate_totals(); |
| 552 | |
| 553 | /** |
| 554 | * Validate that the cart is not empty. |
| 555 | */ |
| 556 | $this->cart_controller->validate_cart_not_empty(); |
| 557 | wc_log_order_step( '[Store API #2] Cart validated' ); |
| 558 | |
| 559 | /** |
| 560 | * Validate items and fix violations before the order is processed. |
| 561 | */ |
| 562 | $this->cart_controller->validate_cart(); |
| 563 | |
| 564 | /** |
| 565 | * Persist customer session data from the request first so that OrderController::update_addresses_from_cart |
| 566 | * uses the up-to-date customer address. |
| 567 | */ |
| 568 | $this->update_customer_from_request( $request ); |
| 569 | // Customer save-point: 1 (session-stored). |
| 570 | wc_log_order_step( '[Store API #3] Updated customer data from request' ); |
| 571 | |
| 572 | /** |
| 573 | * Create (or update) Draft Order and process request data. |
| 574 | */ |
| 575 | $this->create_or_update_draft_order( $request ); |
| 576 | // Order save-point: 1. |
| 577 | wc_log_order_step( '[Store API #4] Created/Updated draft order', array( 'order_object' => $this->order ) ); |
| 578 | $this->update_order_from_request( $request, false ); |
| 579 | wc_log_order_step( '[Store API #5] Updated order with posted data', array( 'order_object' => $this->order ) ); |
| 580 | $this->process_customer( $request ); |
| 581 | // Customer save-point: 2 (db-stored; optional, guest -> customer transition or customer data has changed). |
| 582 | wc_log_order_step( '[Store API #6] Created and/or persisted customer data from order', array( 'order_object' => $this->order ) ); |
| 583 | |
| 584 | /** |
| 585 | * Validate updated order before payment is attempted. |
| 586 | */ |
| 587 | $this->order_controller->validate_order_before_payment( $this->order ); |
| 588 | wc_log_order_step( '[Store API #7] Validated order data', array( 'order_object' => $this->order ) ); |
| 589 | |
| 590 | /** |
| 591 | * Hold coupons for the order as soon as the draft order is created. |
| 592 | */ |
| 593 | try { |
| 594 | // $this->order->get_billing_email() is already validated by validate_order_before_payment() |
| 595 | $this->order->hold_applied_coupons( $this->order->get_billing_email() ); |
| 596 | } catch ( \Exception $e ) { |
| 597 | // Turn the Exception into a RouteException for the API. |
| 598 | throw new RouteException( |
| 599 | 'woocommerce_rest_coupon_reserve_failed', |
| 600 | esc_html( $e->getMessage() ), |
| 601 | 400 |
| 602 | ); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Reserve stock for the order. |
| 607 | * |
| 608 | * In the shortcode based checkout, when POSTing the checkout form the order would be created and fire the |
| 609 | * `woocommerce_checkout_order_created` action. This in turn would trigger the `wc_reserve_stock_for_order` |
| 610 | * function so that stock would be held pending payment. |
| 611 | * |
| 612 | * Via the block based checkout and Store API we already have a draft order, but when POSTing to the /checkout |
| 613 | * endpoint we do the same; reserve stock for the order to allow time to process payment. |
| 614 | * |
| 615 | * Note, stock is only "held" while the order has the status wc-checkout-draft or pending. Stock is freed when |
| 616 | * the order changes status, or there is an exception. |
| 617 | * |
| 618 | * @see ReserveStock::get_query_for_reserved_stock() |
| 619 | * |
| 620 | * @since 9.2 Stock is no longer held for all draft orders, nor on non-POST requests. See https://github.com/woocommerce/woocommerce/issues/44231 |
| 621 | * @since 9.2 Uses wc_reserve_stock_for_order() instead of using the ReserveStock class directly. |
| 622 | */ |
| 623 | try { |
| 624 | wc_reserve_stock_for_order( $this->order ); |
| 625 | } catch ( ReserveStockException $e ) { |
| 626 | throw new RouteException( |
| 627 | esc_html( $e->getErrorCode() ), |
| 628 | esc_html( $e->getMessage() ), |
| 629 | esc_html( $e->getCode() ) |
| 630 | ); |
| 631 | } |
| 632 | wc_log_order_step( '[Store API #8] Reserved stock for order', array( 'order_object' => $this->order ) ); |
| 633 | |
| 634 | wc_do_deprecated_action( |
| 635 | '__experimental_woocommerce_blocks_checkout_order_processed', |
| 636 | array( |
| 637 | $this->order, |
| 638 | ), |
| 639 | '6.3.0', |
| 640 | 'woocommerce_store_api_checkout_order_processed', |
| 641 | 'This action was deprecated in WooCommerce Blocks version 6.3.0. Please use woocommerce_store_api_checkout_order_processed instead.' |
| 642 | ); |
| 643 | |
| 644 | wc_do_deprecated_action( |
| 645 | 'woocommerce_blocks_checkout_order_processed', |
| 646 | array( |
| 647 | $this->order, |
| 648 | ), |
| 649 | '7.2.0', |
| 650 | 'woocommerce_store_api_checkout_order_processed', |
| 651 | 'This action was deprecated in WooCommerce Blocks version 7.2.0. Please use woocommerce_store_api_checkout_order_processed instead.' |
| 652 | ); |
| 653 | |
| 654 | // Set initial status to 'pending'; woocommerce_store_api_checkout_order_processed (fired below) can override it. |
| 655 | // Custom statuses are preserved when no payment is needed; payment gateway statuses take precedence otherwise. |
| 656 | $this->order->update_status( 'pending' ); |
| 657 | // Order save-point: 2. |
| 658 | |
| 659 | /** |
| 660 | * Fires before an order is processed by the Checkout Block/Store API. |
| 661 | * |
| 662 | * This hook informs extensions that $order has completed processing and is ready for payment. |
| 663 | * |
| 664 | * This is similar to existing core hook woocommerce_checkout_order_processed. We're using a new action: |
| 665 | * - To keep the interface focused (only pass $order, not passing request data). |
| 666 | * - This also explicitly indicates these orders are from checkout block/StoreAPI. |
| 667 | * |
| 668 | * @since 7.2.0 |
| 669 | * |
| 670 | * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3238 |
| 671 | * @example See docs/examples/checkout-order-processed.md |
| 672 | |
| 673 | * @param \WC_Order $order Order object. |
| 674 | */ |
| 675 | do_action( 'woocommerce_store_api_checkout_order_processed', $this->order ); |
| 676 | |
| 677 | /** |
| 678 | * Process the payment and return the results. |
| 679 | */ |
| 680 | $payment_result = new PaymentResult(); |
| 681 | |
| 682 | if ( $this->order->needs_payment() ) { |
| 683 | $this->process_payment( $request, $payment_result ); |
| 684 | } else { |
| 685 | $this->process_without_payment( $request, $payment_result ); |
| 686 | } |
| 687 | |
| 688 | wc_log_order_step( |
| 689 | '[Store API #9] Order processed', |
| 690 | array( |
| 691 | 'order_object' => $this->order, |
| 692 | 'processed_with_payment' => $this->order->needs_payment() ? 'yes' : 'no', |
| 693 | 'payment_status' => $payment_result->status, |
| 694 | ), |
| 695 | true |
| 696 | ); |
| 697 | |
| 698 | return $this->prepare_item_for_response( |
| 699 | (object) [ |
| 700 | 'order' => wc_get_order( $this->order ), |
| 701 | 'payment_result' => $payment_result, |
| 702 | ], |
| 703 | $request |
| 704 | ); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Get route response when something went wrong. |
| 709 | * |
| 710 | * @param string $error_code String based error code. |
| 711 | * @param string $error_message User facing error message. |
| 712 | * @param int $http_status_code HTTP status. Defaults to 500. |
| 713 | * @param array $additional_data Extra data (key value pairs) to expose in the error response. |
| 714 | * @return \WP_Error WP Error object. |
| 715 | */ |
| 716 | protected function get_route_error_response( $error_code, $error_message, $http_status_code = 500, $additional_data = [] ) { |
| 717 | $error_from_message = new \WP_Error( |
| 718 | $error_code, |
| 719 | $error_message |
| 720 | ); |
| 721 | // 409 is when there was a conflict, so we return the cart so the client can resolve it. |
| 722 | if ( 409 === $http_status_code ) { |
| 723 | return $this->add_data_to_error_object( $error_from_message, $additional_data, $http_status_code, true ); |
| 724 | } |
| 725 | return $this->add_data_to_error_object( $error_from_message, $additional_data, $http_status_code ); |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Get route response when something went wrong. |
| 730 | * |
| 731 | * @param \WP_Error $error_object User facing error message. |
| 732 | * @param int $http_status_code HTTP status. Defaults to 500. |
| 733 | * @param array $additional_data Extra data (key value pairs) to expose in the error response. |
| 734 | * @return \WP_Error WP Error object. |
| 735 | */ |
| 736 | protected function get_route_error_response_from_object( $error_object, $http_status_code = 500, $additional_data = [] ) { |
| 737 | // 409 is when there was a conflict, so we return the cart so the client can resolve it. |
| 738 | if ( 409 === $http_status_code ) { |
| 739 | return $this->add_data_to_error_object( $error_object, $additional_data, $http_status_code, true ); |
| 740 | } |
| 741 | return $this->add_data_to_error_object( $error_object, $additional_data, $http_status_code ); |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * Adds additional data to the \WP_Error object. |
| 746 | * |
| 747 | * @param \WP_Error $error The error object to add the cart to. |
| 748 | * @param array $data The data to add to the error object. |
| 749 | * @param int $http_status_code The HTTP status code this error should return. |
| 750 | * @param bool $include_cart Whether the cart should be included in the error data. |
| 751 | * @returns \WP_Error The \WP_Error with the cart added. |
| 752 | */ |
| 753 | private function add_data_to_error_object( $error, $data, $http_status_code, bool $include_cart = false ) { |
| 754 | $data = array_merge( $data, [ 'status' => $http_status_code ] ); |
| 755 | if ( $include_cart ) { |
| 756 | $data = array_merge( $data, [ 'cart' => $this->cart_schema->get_item_response( $this->cart_controller->get_cart_for_response() ) ] ); |
| 757 | } |
| 758 | $error->add_data( $data ); |
| 759 | return $error; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Create or update a draft order based on the cart. |
| 764 | * |
| 765 | * @phpstan-assert \WC_Order $this->order |
| 766 | * |
| 767 | * @param \WP_REST_Request $request Full details about the request. |
| 768 | * @throws RouteException On error. |
| 769 | */ |
| 770 | private function create_or_update_draft_order( \WP_REST_Request $request ) { |
| 771 | // Reuse the failed/pending order from the customer's session if one exists; otherwise the POST flow would orphan it by creating a fresh order on every retry. |
| 772 | $this->order = $this->order ?? $this->get_draft_order(); |
| 773 | |
| 774 | if ( ! $this->order ) { |
| 775 | $this->order = $this->order_controller->create_order_from_cart(); |
| 776 | wc_log_order_step( '[Store API #4::create_or_update_draft_order] Created order from cart', array( 'order_object' => $this->order ) ); |
| 777 | |
| 778 | /** |
| 779 | * Fires once when the Store API checkout draft order is first materialised. |
| 780 | * |
| 781 | * Use this hook for first-touch logic that should only run when the draft |
| 782 | * order is initially created (e.g. analytics, abandoned-cart trackers). As |
| 783 | * of WooCommerce 10.8.0 the Store API defers draft order creation to |
| 784 | * place-order time, so this action fires once at POST rather than on the |
| 785 | * first PATCH. |
| 786 | * |
| 787 | * @since 10.8.0 |
| 788 | * |
| 789 | * @param \WC_Order $order Order object. |
| 790 | */ |
| 791 | do_action( 'woocommerce_store_api_checkout_order_created', $this->order ); |
| 792 | } else { |
| 793 | $this->order_controller->update_order_from_cart( $this->order, true ); |
| 794 | wc_log_order_step( '[Store API #4::create_or_update_draft_order] Updated order from cart', array( 'order_object' => $this->order ) ); |
| 795 | } |
| 796 | |
| 797 | wc_do_deprecated_action( |
| 798 | '__experimental_woocommerce_blocks_checkout_update_order_meta', |
| 799 | array( |
| 800 | $this->order, |
| 801 | ), |
| 802 | '6.3.0', |
| 803 | 'woocommerce_store_api_checkout_update_order_meta', |
| 804 | 'This action was deprecated in WooCommerce Blocks version 6.3.0. Please use woocommerce_store_api_checkout_update_order_meta instead.' |
| 805 | ); |
| 806 | |
| 807 | wc_do_deprecated_action( |
| 808 | 'woocommerce_blocks_checkout_update_order_meta', |
| 809 | array( |
| 810 | $this->order, |
| 811 | ), |
| 812 | '7.2.0', |
| 813 | 'woocommerce_store_api_checkout_update_order_meta', |
| 814 | 'This action was deprecated in WooCommerce Blocks version 7.2.0. Please use woocommerce_store_api_checkout_update_order_meta instead.' |
| 815 | ); |
| 816 | |
| 817 | /** |
| 818 | * Fires when the Checkout Block/Store API updates an order's meta data. |
| 819 | * |
| 820 | * This hook gives extensions the chance to add or update meta data on the $order. |
| 821 | * Throwing an exception from a callback attached to this action will make the Checkout Block render in a warning state, effectively preventing checkout. |
| 822 | * |
| 823 | * This is similar to existing core hook woocommerce_checkout_update_order_meta. |
| 824 | * We're using a new action: |
| 825 | * - To keep the interface focused (only pass $order, not passing request data). |
| 826 | * - This also explicitly indicates these orders are from checkout block/StoreAPI. |
| 827 | * |
| 828 | * @since 7.2.0 |
| 829 | * |
| 830 | * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3686 |
| 831 | * |
| 832 | * @param \WC_Order $order Order object. |
| 833 | */ |
| 834 | do_action( 'woocommerce_store_api_checkout_update_order_meta', $this->order ); |
| 835 | |
| 836 | // Confirm order is valid before proceeding further. |
| 837 | if ( ! $this->order instanceof \WC_Order ) { |
| 838 | throw new RouteException( |
| 839 | 'woocommerce_rest_checkout_missing_order', |
| 840 | esc_html__( 'Unable to create order', 'woocommerce' ), |
| 841 | 500 |
| 842 | ); |
| 843 | } |
| 844 | |
| 845 | // Store order ID to session. |
| 846 | $this->set_draft_order_id( $this->order->get_id() ); |
| 847 | wc_log_order_step( '[Store API #4::create_or_update_draft_order] Set order draft id', array( 'order_object' => $this->order ) ); |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Updates a customer address field. |
| 852 | * |
| 853 | * @param \WC_Customer $customer The customer to update. |
| 854 | * @param string $key The key of the field to update. |
| 855 | * @param mixed $value The value to update the field to. |
| 856 | * @param string $address_type The type of address to update (billing|shipping). |
| 857 | */ |
| 858 | private function update_customer_address_field( $customer, $key, $value, $address_type ) { |
| 859 | $callback = "set_{$address_type}_{$key}"; |
| 860 | |
| 861 | if ( is_callable( [ $customer, $callback ] ) ) { |
| 862 | $customer->$callback( $value ); |
| 863 | return; |
| 864 | } |
| 865 | |
| 866 | if ( $this->additional_fields_controller->is_field( $key ) ) { |
| 867 | $this->additional_fields_controller->persist_field_for_customer( $key, $value, $customer, $address_type ); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * Updates the current customer session using data from the request (e.g. address data). |
| 873 | * |
| 874 | * Address session data is synced to the order itself later on by OrderController::update_order_from_cart() |
| 875 | * |
| 876 | * @param \WP_REST_Request $request Full details about the request. |
| 877 | */ |
| 878 | private function update_customer_from_request( \WP_REST_Request $request ) { |
| 879 | $customer = WC()->customer; |
| 880 | $additional_field_contexts = [ |
| 881 | 'shipping_address' => [ |
| 882 | 'group' => 'shipping', |
| 883 | 'location' => 'address', |
| 884 | 'param' => 'shipping_address', |
| 885 | ], |
| 886 | 'billing_address' => [ |
| 887 | 'group' => 'billing', |
| 888 | 'location' => 'address', |
| 889 | 'param' => 'billing_address', |
| 890 | ], |
| 891 | 'contact' => [ |
| 892 | 'group' => 'other', |
| 893 | 'location' => 'contact', |
| 894 | 'param' => 'additional_fields', |
| 895 | ], |
| 896 | ]; |
| 897 | |
| 898 | foreach ( $additional_field_contexts as $context => $context_data ) { |
| 899 | |
| 900 | $document_object = $this->get_document_object_from_rest_request( $request ); |
| 901 | $document_object->set_context( $context ); |
| 902 | $additional_fields = $this->additional_fields_controller->get_contextual_fields_for_location( $context_data['location'], $document_object ); |
| 903 | |
| 904 | if ( 'shipping_address' === $context_data['param'] ) { |
| 905 | $field_values = (array) $request['shipping_address'] ?? ( $request['billing_address'] ?? [] ); |
| 906 | |
| 907 | if ( ! WC()->cart->needs_shipping() ) { |
| 908 | $field_values = $request['billing_address'] ?? []; |
| 909 | } |
| 910 | } else { |
| 911 | $field_values = (array) $request[ $context_data['param'] ] ?? []; |
| 912 | } |
| 913 | |
| 914 | if ( 'address' === $context_data['location'] ) { |
| 915 | $persist_keys = array_merge( $this->additional_fields_controller->get_address_fields_keys(), [ 'email' ], array_keys( $additional_fields ) ); |
| 916 | } else { |
| 917 | $persist_keys = array_keys( $additional_fields ); |
| 918 | } |
| 919 | |
| 920 | foreach ( $field_values as $key => $value ) { |
| 921 | if ( in_array( $key, $persist_keys, true ) ) { |
| 922 | $this->update_customer_address_field( $customer, $key, $value, $context_data['group'] ); |
| 923 | } |
| 924 | } |
| 925 | wc_log_order_step( '[Store API #3::update_customer_from_request] Persisted ' . $context . ' fields' ); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * Fires when the Checkout Block/Store API updates a customer from the API request data. |
| 930 | * |
| 931 | * @since 8.2.0 |
| 932 | * |
| 933 | * @param \WC_Customer $customer Customer object. |
| 934 | * @param \WP_REST_Request $request Full details about the request. |
| 935 | */ |
| 936 | do_action( 'woocommerce_store_api_checkout_update_customer_from_request', $customer, $request ); |
| 937 | |
| 938 | $customer->save(); |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * Gets the chosen payment method from the request. |
| 943 | * |
| 944 | * @throws RouteException On error. |
| 945 | * @param \WP_REST_Request $request Request object. |
| 946 | * @return \WC_Payment_Gateway|null |
| 947 | */ |
| 948 | private function get_request_payment_method( \WP_REST_Request $request ) { |
| 949 | $request_payment_method = wc_clean( wp_unslash( $request['payment_method'] ?? '' ) ); |
| 950 | |
| 951 | if ( empty( $request_payment_method ) ) { |
| 952 | return null; |
| 953 | } |
| 954 | |
| 955 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 956 | |
| 957 | if ( ! isset( $available_gateways[ $request_payment_method ] ) ) { |
| 958 | $all_payment_gateways = WC()->payment_gateways->payment_gateways(); |
| 959 | $gateway_title = isset( $all_payment_gateways[ $request_payment_method ] ) ? $all_payment_gateways[ $request_payment_method ]->get_title() : $request_payment_method; |
| 960 | throw new RouteException( |
| 961 | 'woocommerce_rest_checkout_payment_method_disabled', |
| 962 | sprintf( |
| 963 | // Translators: %s Payment method ID. |
| 964 | esc_html__( '%s is not available for this order—please choose a different payment method', 'woocommerce' ), |
| 965 | esc_html( $gateway_title ) |
| 966 | ), |
| 967 | 400 |
| 968 | ); |
| 969 | } |
| 970 | |
| 971 | return $available_gateways[ $request_payment_method ]; |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * Order processing relating to customer account. |
| 976 | * |
| 977 | * Creates a customer account as needed (based on request & store settings) and updates the order with the new customer ID. |
| 978 | * Updates the order with user details (e.g. address). |
| 979 | * |
| 980 | * @throws RouteException API error object with error details. |
| 981 | * @param \WP_REST_Request $request Request object. |
| 982 | */ |
| 983 | private function process_customer( \WP_REST_Request $request ) { |
| 984 | $order = $this->get_order_or_throw(); |
| 985 | |
| 986 | if ( $this->should_create_customer_account( $request ) ) { |
| 987 | $customer_id = wc_create_new_customer( |
| 988 | $request['billing_address']['email'], |
| 989 | '', |
| 990 | $request['customer_password'], |
| 991 | [ |
| 992 | 'first_name' => $request['billing_address']['first_name'], |
| 993 | 'last_name' => $request['billing_address']['last_name'], |
| 994 | 'source' => 'store-api', |
| 995 | ] |
| 996 | ); |
| 997 | |
| 998 | if ( is_wp_error( $customer_id ) ) { |
| 999 | throw new RouteException( |
| 1000 | esc_html( $customer_id->get_error_code() ), |
| 1001 | esc_html( $customer_id->get_error_message() ), |
| 1002 | 400 |
| 1003 | ); |
| 1004 | } |
| 1005 | |
| 1006 | // Associate customer with the order. |
| 1007 | $order->set_customer_id( $customer_id ); |
| 1008 | |
| 1009 | // Set the customer auth cookie. |
| 1010 | wc_set_customer_auth_cookie( $customer_id ); |
| 1011 | wc_log_order_step( '[Store API #6::process_customer] Created new customer', array( 'customer_id' => $customer_id ) ); |
| 1012 | } |
| 1013 | |
| 1014 | // Persist customer address data to account. |
| 1015 | $this->order_controller->sync_customer_data_with_order( $order ); |
| 1016 | wc_log_order_step( '[Store API #6::process_customer] Synced customer data from order', array( 'customer_id' => $order->get_customer_id() ) ); |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * Check request options and store (shop) config to determine if a user account should be created as part of order |
| 1021 | * processing. |
| 1022 | * |
| 1023 | * @param \WP_REST_Request $request The current request object being handled. |
| 1024 | * @return boolean True if a new user account should be created. |
| 1025 | */ |
| 1026 | private function should_create_customer_account( \WP_REST_Request $request ) { |
| 1027 | if ( is_user_logged_in() ) { |
| 1028 | return false; |
| 1029 | } |
| 1030 | |
| 1031 | // Return false if registration is not enabled for the store. |
| 1032 | if ( false === filter_var( WC()->checkout()->is_registration_enabled(), FILTER_VALIDATE_BOOLEAN ) ) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
| 1036 | // Return true if the store requires an account for all purchases. Note - checkbox is not displayed to shopper in this case. |
| 1037 | if ( true === filter_var( WC()->checkout()->is_registration_required(), FILTER_VALIDATE_BOOLEAN ) ) { |
| 1038 | return true; |
| 1039 | } |
| 1040 | |
| 1041 | // Create an account if requested via the endpoint. |
| 1042 | if ( true === filter_var( $request['create_account'], FILTER_VALIDATE_BOOLEAN ) ) { |
| 1043 | // User has requested an account as part of checkout processing. |
| 1044 | return true; |
| 1045 | } |
| 1046 | |
| 1047 | return false; |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * This validates if the order can be placed regarding settings in WooCommerce > Settings > Accounts & Privacy |
| 1052 | * If registration during checkout is disabled, guest checkout is disabled and the user is not logged in, prevent checkout. |
| 1053 | * |
| 1054 | * @throws RouteException If user cannot place order. |
| 1055 | */ |
| 1056 | private function validate_user_can_place_order() { |
| 1057 | if ( |
| 1058 | // "woocommerce_enable_signup_and_login_from_checkout" === no. |
| 1059 | false === filter_var( WC()->checkout()->is_registration_enabled(), FILTER_VALIDATE_BOOLEAN ) && |
| 1060 | // "woocommerce_enable_guest_checkout" === no. |
| 1061 | true === filter_var( WC()->checkout()->is_registration_required(), FILTER_VALIDATE_BOOLEAN ) && |
| 1062 | ! is_user_logged_in() |
| 1063 | ) { |
| 1064 | throw new RouteException( |
| 1065 | 'woocommerce_rest_guest_checkout_disabled', |
| 1066 | esc_html( |
| 1067 | /** |
| 1068 | * Filter to customize the checkout message when a user must be logged in. |
| 1069 | * |
| 1070 | * @since 9.4.3 |
| 1071 | * |
| 1072 | * @param string $message Message to display when a user must be logged in to check out. |
| 1073 | */ |
| 1074 | apply_filters( |
| 1075 | 'woocommerce_checkout_must_be_logged_in_message', |
| 1076 | __( 'You must be logged in to checkout.', 'woocommerce' ) |
| 1077 | ) |
| 1078 | ), |
| 1079 | 403 |
| 1080 | ); |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 |