1 <?php
2 3 4 5 6 7 8 9 10 11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15
16 class WC_API_Coupons extends WC_API_Resource {
17
18
19 protected $base = '/coupons';
20
21 22 23 24 25 26 27 28 29 30 31
32 public function register_routes( $routes ) {
33
34
35 $routes[ $this->base ] = array(
36 array( array( $this, 'get_coupons' ), WC_API_Server::READABLE ),
37 );
38
39
40 $routes[ $this->base . '/count'] = array(
41 array( array( $this, 'get_coupons_count' ), WC_API_Server::READABLE ),
42 );
43
44
45 $routes[ $this->base . '/(?P<id>\d+)' ] = array(
46 array( array( $this, 'get_coupon' ), WC_API_Server::READABLE ),
47 );
48
49
50 $routes[ $this->base . '/code/(?P<code>\w[\w\s\-]*)' ] = array(
51 array( array( $this, 'get_coupon_by_code' ), WC_API_Server::READABLE ),
52 );
53
54 return $routes;
55 }
56
57 58 59 60 61 62 63 64 65
66 public function get_coupons( $fields = null, $filter = array(), $page = 1 ) {
67
68 $filter['page'] = $page;
69
70 $query = $this->query_coupons( $filter );
71
72 $coupons = array();
73
74 foreach( $query->posts as $coupon_id ) {
75
76 if ( ! $this->is_readable( $coupon_id ) )
77 continue;
78
79 $coupons[] = current( $this->get_coupon( $coupon_id, $fields ) );
80 }
81
82 $this->server->add_pagination_headers( $query );
83
84 return array( 'coupons' => $coupons );
85 }
86
87 88 89 90 91 92 93 94
95 public function get_coupon( $id, $fields = null ) {
96 global $wpdb;
97
98 $id = $this->validate_request( $id, 'shop_coupon', 'read' );
99
100 if ( is_wp_error( $id ) )
101 return $id;
102
103
104 $code = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $id ) );
105
106 if ( is_null( $code ) )
107 return new WP_Error( 'woocommerce_api_invalid_coupon_id', __( 'Invalid coupon ID', 'woocommerce' ), array( 'status' => 404 ) );
108
109 $coupon = new WC_Coupon( $code );
110
111 $coupon_post = get_post( $coupon->id );
112
113 $coupon_data = array(
114 'id' => $coupon->id,
115 'code' => $coupon->code,
116 'type' => $coupon->type,
117 'created_at' => $this->server->format_datetime( $coupon_post->post_date_gmt ),
118 'updated_at' => $this->server->format_datetime( $coupon_post->post_modified_gmt ),
119 'amount' => wc_format_decimal( $coupon->amount, 2 ),
120 'individual_use' => ( 'yes' === $coupon->individual_use ),
121 'product_ids' => array_map( 'absint', (array) $coupon->product_ids ),
122 'exclude_product_ids' => array_map( 'absint', (array) $coupon->exclude_product_ids ),
123 'usage_limit' => ( ! empty( $coupon->usage_limit ) ) ? $coupon->usage_limit : null,
124 'usage_limit_per_user' => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
125 'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items,
126 'usage_count' => (int) $coupon->usage_count,
127 'expiry_date' => $this->server->format_datetime( $coupon->expiry_date ),
128 'apply_before_tax' => $coupon->apply_before_tax(),
129 'enable_free_shipping' => $coupon->enable_free_shipping(),
130 'product_category_ids' => array_map( 'absint', (array) $coupon->product_categories ),
131 'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->exclude_product_categories ),
132 'exclude_sale_items' => $coupon->exclude_sale_items(),
133 'minimum_amount' => wc_format_decimal( $coupon->minimum_amount, 2 ),
134 'customer_emails' => $coupon->customer_email,
135 );
136
137 return array( 'coupon' => apply_filters( 'woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server ) );
138 }
139
140 141 142 143 144 145 146
147 public function get_coupons_count( $filter = array() ) {
148
149 $query = $this->query_coupons( $filter );
150
151 if ( ! current_user_can( 'read_private_shop_coupons' ) )
152 return new WP_Error( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), array( 'status' => 401 ) );
153
154 return array( 'count' => (int) $query->found_posts );
155 }
156
157 158 159 160 161 162 163 164
165 public function get_coupon_by_code( $code, $fields = null ) {
166 global $wpdb;
167
168 $id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $code ) );
169
170 if ( is_null( $id ) )
171 return new WP_Error( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), array( 'status' => 404 ) );
172
173 return $this->get_coupon( $id, $fields );
174 }
175
176 177 178 179 180 181 182
183 public function create_coupon( $data ) {
184
185 return array();
186 }
187
188 189 190 191 192 193 194 195
196 public function edit_coupon( $id, $data ) {
197
198 $id = $this->validate_request( $id, 'shop_coupon', 'edit' );
199
200 if ( is_wp_error( $id ) )
201 return $id;
202
203 return $this->get_coupon( $id );
204 }
205
206 207 208 209 210 211 212 213
214 public function delete_coupon( $id, $force = false ) {
215
216 $id = $this->validate_request( $id, 'shop_coupon', 'delete' );
217
218 if ( is_wp_error( $id ) )
219 return $id;
220
221 return $this->delete( $id, 'shop_coupon', ( 'true' === $force ) );
222 }
223
224 225 226 227 228 229 230
231 private function query_coupons( $args ) {
232
233
234 $query_args = array(
235 'fields' => 'ids',
236 'post_type' => 'shop_coupon',
237 'post_status' => 'publish',
238 );
239
240 $query_args = $this->merge_query_args( $query_args, $args );
241
242 return new WP_Query( $query_args );
243 }
244
245 }
246