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
CartApplyCoupon.php
81 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 5 | |
| 6 | /** |
| 7 | * CartApplyCoupon class. |
| 8 | */ |
| 9 | class CartApplyCoupon extends AbstractCartRoute { |
| 10 | /** |
| 11 | * The route identifier. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | const IDENTIFIER = 'cart-apply-coupon'; |
| 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/apply-coupon'; |
| 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 [ |
| 42 | [ |
| 43 | 'methods' => \WP_REST_Server::CREATABLE, |
| 44 | 'callback' => [ $this, 'get_response' ], |
| 45 | 'permission_callback' => '__return_true', |
| 46 | 'args' => [ |
| 47 | 'code' => [ |
| 48 | 'description' => __( 'Unique identifier for the coupon within the cart.', 'woocommerce' ), |
| 49 | 'type' => 'string', |
| 50 | ], |
| 51 | ], |
| 52 | ], |
| 53 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 54 | 'allow_batch' => [ 'v1' => true ], |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Handle the request and return a valid response for this endpoint. |
| 60 | * |
| 61 | * @throws RouteException On error. |
| 62 | * @param \WP_REST_Request $request Request object. |
| 63 | * @return \WP_REST_Response |
| 64 | */ |
| 65 | protected function get_route_post_response( \WP_REST_Request $request ) { |
| 66 | if ( ! wc_coupons_enabled() ) { |
| 67 | throw new RouteException( 'woocommerce_rest_cart_coupon_disabled', esc_html__( 'Coupons are disabled.', 'woocommerce' ), 404 ); |
| 68 | } |
| 69 | |
| 70 | $coupon_code = wc_format_coupon_code( wp_unslash( $request['code'] ) ); |
| 71 | |
| 72 | try { |
| 73 | $this->cart_controller->apply_coupon( $coupon_code ); |
| 74 | } catch ( \WC_REST_Exception $e ) { |
| 75 | throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 76 | } |
| 77 | |
| 78 | return rest_ensure_response( $this->schema->get_item_response( $this->cart_controller->get_cart_for_response() ) ); |
| 79 | } |
| 80 | } |
| 81 |