PluginProbe
WooCommerce / 11.0.0
WooCommerce v11.0.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / StoreApi / Routes / V1 / CartCoupons.php
CartCoupons.php
156 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Automattic\WooCommerce\StoreApi\Routes\V1;
3
4 use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
5
6 /**
7 * CartCoupons class.
8 */
9 class CartCoupons extends AbstractCartRoute {
10 /**
11 * The route identifier.
12 *
13 * @var string
14 */
15 const IDENTIFIER = 'cart-coupons';
16
17 /**
18 * The routes schema.
19 *
20 * @var string
21 */
22 const SCHEMA_TYPE = 'cart-coupon';
23
24 /**
25 * Get the path of this REST route.
26 *
27 * @return string
28 */
29 public function get_path() {
30 return self::get_path_regex();
31 }
32
33 /**
34 * Get the path of this rest route.
35 *
36 * @return string
37 */
38 public static function get_path_regex() {
39 return '/cart/coupons';
40 }
41
42 /**
43 * Get method arguments for this REST route.
44 *
45 * @return array An array of endpoints.
46 */
47 public function get_args() {
48 return [
49 [
50 'methods' => \WP_REST_Server::READABLE,
51 'callback' => [ $this, 'get_response' ],
52 'permission_callback' => '__return_true',
53 'args' => [
54 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
55 ],
56 ],
57 [
58 'methods' => \WP_REST_Server::CREATABLE,
59 'callback' => [ $this, 'get_response' ],
60 'permission_callback' => '__return_true',
61 'args' => $this->schema->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ),
62 ],
63 [
64 'methods' => \WP_REST_Server::DELETABLE,
65 'permission_callback' => '__return_true',
66 'callback' => [ $this, 'get_response' ],
67 ],
68 'schema' => [ $this->schema, 'get_public_item_schema' ],
69 'allow_batch' => [ 'v1' => true ],
70 ];
71 }
72
73 /**
74 * Get a collection of cart coupons.
75 *
76 * @throws RouteException On error.
77 * @param \WP_REST_Request $request Request object.
78 * @return \WP_REST_Response
79 */
80 protected function get_route_response( \WP_REST_Request $request ) {
81 $cart_coupons = $this->cart_controller->get_cart_coupons();
82 $items = [];
83
84 foreach ( $cart_coupons as $coupon_code ) {
85 $response = rest_ensure_response( $this->schema->get_item_response( $coupon_code ) );
86 $response->add_links( $this->prepare_links( $coupon_code, $request ) );
87
88 $response = $this->prepare_response_for_collection( $response );
89 $items[] = $response;
90 }
91
92 $response = rest_ensure_response( $items );
93
94 return $response;
95 }
96
97 /**
98 * Add a coupon to the cart and return the result.
99 *
100 * @throws RouteException On error.
101 * @param \WP_REST_Request $request Request object.
102 * @return \WP_REST_Response
103 */
104 protected function get_route_post_response( \WP_REST_Request $request ) {
105 if ( ! wc_coupons_enabled() ) {
106 throw new RouteException( 'woocommerce_rest_cart_coupon_disabled', __( 'Coupons are disabled.', 'woocommerce' ), 404 );
107 }
108
109 try {
110 $this->cart_controller->apply_coupon( $request['code'] );
111 } catch ( \WC_REST_Exception $e ) {
112 throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() );
113 }
114
115 $response = $this->prepare_item_for_response( $request['code'], $request );
116 $response->set_status( 201 );
117
118 return $response;
119 }
120
121 /**
122 * Deletes all coupons in the cart.
123 *
124 * @throws RouteException On error.
125 * @param \WP_REST_Request $request Request object.
126 * @return \WP_REST_Response
127 */
128 protected function get_route_delete_response( \WP_REST_Request $request ) {
129 $cart = $this->cart_controller->get_cart_instance();
130
131 $cart->remove_coupons();
132
133 return new \WP_REST_Response( [], 200 );
134 }
135
136 /**
137 * Prepare links for the request.
138 *
139 * @param string $coupon_code Coupon code.
140 * @param \WP_REST_Request $request Request object.
141 * @return array
142 */
143 protected function prepare_links( $coupon_code, $request ) {
144 $base = $this->get_namespace() . $this->get_path();
145 $links = array(
146 'self' => array(
147 'href' => rest_url( trailingslashit( $base ) . $coupon_code ),
148 ),
149 'collection' => array(
150 'href' => rest_url( $base ),
151 ),
152 );
153 return $links;
154 }
155 }
156