AI
1 year ago
Agentic
4 months ago
AbstractCartRoute.php
4 months ago
AbstractRoute.php
3 months ago
AbstractTermsRoute.php
4 months ago
Batch.php
4 months ago
Cart.php
1 year ago
CartAddItem.php
4 months ago
CartApplyCoupon.php
1 year ago
CartCoupons.php
3 months ago
CartCouponsByCode.php
1 year ago
CartExtensions.php
2 years ago
CartItems.php
2 years ago
CartItemsByKey.php
2 years ago
CartRemoveCoupon.php
1 year ago
CartRemoveItem.php
4 months ago
CartSelectShippingRate.php
5 months ago
CartUpdateCustomer.php
4 months ago
CartUpdateItem.php
4 months ago
Checkout.php
1 month ago
CheckoutOrder.php
1 year ago
Order.php
7 months ago
Patterns.php
1 year ago
ProductAttributeTerms.php
1 month ago
ProductAttributes.php
1 year ago
ProductAttributesById.php
1 year ago
ProductBrands.php
1 year ago
ProductBrandsById.php
1 year ago
ProductCategories.php
1 year ago
ProductCategoriesById.php
1 year ago
ProductCollectionData.php
11 months ago
ProductReviews.php
4 months ago
ProductTags.php
1 year ago
Products.php
3 months ago
ProductsById.php
3 months ago
ProductsBySlug.php
3 months ago
ShopperListItems.php
1 month ago
ShopperListItemsByKey.php
1 month ago
ShopperLists.php
1 month ago
ShopperListsBySlug.php
1 month ago
ShopperListsNonceCheck.php
1 month ago
CartSelectShippingRate.php
115 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 5 | |
| 6 | /** |
| 7 | * CartSelectShippingRate class. |
| 8 | */ |
| 9 | class CartSelectShippingRate extends AbstractCartRoute { |
| 10 | /** |
| 11 | * The route identifier. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | const IDENTIFIER = 'cart-select-shipping-rate'; |
| 16 | |
| 17 | /** |
| 18 | * Get the path of this REST route. |
| 19 | * |
| 20 | * @return string |
| 21 | */ |
| 22 | public function get_path() { |
| 23 | return self::get_path_regex(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Get the path of this rest route. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public static function get_path_regex() { |
| 32 | return '/cart/select-shipping-rate'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get method arguments for this REST route. |
| 37 | * |
| 38 | * @return array An array of endpoints. |
| 39 | */ |
| 40 | public function get_args() { |
| 41 | return array( |
| 42 | array( |
| 43 | 'methods' => \WP_REST_Server::CREATABLE, |
| 44 | 'callback' => array( $this, 'get_response' ), |
| 45 | 'permission_callback' => '__return_true', |
| 46 | 'args' => array( |
| 47 | 'package_id' => array( |
| 48 | 'description' => __( 'The ID of the package being shipped. Leave blank to apply to all packages.', 'woocommerce' ), |
| 49 | 'type' => array( 'integer', 'string', 'null' ), |
| 50 | 'required' => false, |
| 51 | ), |
| 52 | 'rate_id' => array( |
| 53 | 'description' => __( 'The chosen rate ID for the package.', 'woocommerce' ), |
| 54 | 'type' => 'string', |
| 55 | 'required' => true, |
| 56 | ), |
| 57 | ), |
| 58 | ), |
| 59 | 'schema' => array( $this->schema, 'get_public_item_schema' ), |
| 60 | 'allow_batch' => array( 'v1' => true ), |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Handle the request and return a valid response for this endpoint. |
| 66 | * |
| 67 | * @throws RouteException On error. |
| 68 | * @param \WP_REST_Request $request Request object. |
| 69 | * @return \WP_REST_Response |
| 70 | */ |
| 71 | protected function get_route_post_response( \WP_REST_Request $request ) { |
| 72 | if ( ! wc_shipping_enabled() ) { |
| 73 | throw new RouteException( 'woocommerce_rest_shipping_disabled', esc_html__( 'Shipping is disabled.', 'woocommerce' ), 404 ); |
| 74 | } |
| 75 | |
| 76 | if ( ! isset( $request['rate_id'] ) ) { |
| 77 | throw new RouteException( 'woocommerce_rest_cart_missing_rate_id', esc_html__( 'Invalid Rate ID.', 'woocommerce' ), 400 ); |
| 78 | } |
| 79 | |
| 80 | $cart = $this->cart_controller->get_cart_instance(); |
| 81 | $package_id = isset( $request['package_id'] ) ? sanitize_text_field( wp_unslash( $request['package_id'] ) ) : null; |
| 82 | $rate_id = sanitize_text_field( wp_unslash( $request['rate_id'] ) ); |
| 83 | |
| 84 | try { |
| 85 | if ( ! is_null( $package_id ) ) { |
| 86 | $this->cart_controller->select_shipping_rate( $package_id, $rate_id ); |
| 87 | } else { |
| 88 | foreach ( $this->cart_controller->get_shipping_packages() as $package ) { |
| 89 | $this->cart_controller->select_shipping_rate( $package['package_id'], $rate_id ); |
| 90 | } |
| 91 | } |
| 92 | } catch ( \WC_Rest_Exception $e ) { |
| 93 | throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Fires an action after a shipping method has been chosen for package(s) via the Store API. |
| 98 | * |
| 99 | * This allows extensions to perform addition actions after a shipping method has been chosen, but before the |
| 100 | * cart totals are recalculated. |
| 101 | * |
| 102 | * @since 9.0.0 |
| 103 | * |
| 104 | * @param string|null $package_id The sanitized ID of the package being updated. Null if all packages are being updated. |
| 105 | * @param string $rate_id The sanitized chosen rate ID for the package. |
| 106 | * @param \WP_REST_Request $request Full details about the request. |
| 107 | */ |
| 108 | do_action( 'woocommerce_store_api_cart_select_shipping_rate', $package_id, $rate_id, $request ); |
| 109 | |
| 110 | $cart->calculate_totals(); |
| 111 | |
| 112 | return rest_ensure_response( $this->cart_schema->get_item_response( $cart ) ); |
| 113 | } |
| 114 | } |
| 115 |