1 <?php
  2 /**
  3  * WooCommerce API Coupons Class
  4  *
  5  * Handles requests to the /coupons endpoint
  6  *
  7  * @author      WooThemes
  8  * @category    API
  9  * @package     WooCommerce/API
 10  * @since       2.1
 11  */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 14 
 15 
 16 class WC_API_Coupons extends WC_API_Resource {
 17 
 18     /** @var string $base the route base */
 19     protected $base = '/coupons';
 20 
 21     /**
 22      * Register the routes for this class
 23      *
 24      * GET /coupons
 25      * GET /coupons/count
 26      * GET /coupons/<id>
 27      *
 28      * @since 2.1
 29      * @param array $routes
 30      * @return array
 31      */
 32     public function register_routes( $routes ) {
 33 
 34         # GET /coupons
 35         $routes[ $this->base ] = array(
 36             array( array( $this, 'get_coupons' ),     WC_API_Server::READABLE ),
 37         );
 38 
 39         # GET /coupons/count
 40         $routes[ $this->base . '/count'] = array(
 41             array( array( $this, 'get_coupons_count' ), WC_API_Server::READABLE ),
 42         );
 43 
 44         # GET /coupons/<id>
 45         $routes[ $this->base . '/(?P<id>\d+)' ] = array(
 46             array( array( $this, 'get_coupon' ),  WC_API_Server::READABLE ),
 47         );
 48 
 49         # GET /coupons/code/<code>, note that coupon codes can contain spaces, dashes and underscores
 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      * Get all coupons
 59      *
 60      * @since 2.1
 61      * @param string $fields
 62      * @param array $filter
 63      * @param int $page
 64      * @return array
 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      * Get the coupon for the given ID
 89      *
 90      * @since 2.1
 91      * @param int $id the coupon ID
 92      * @param string $fields fields to include in response
 93      * @return array|WP_Error
 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         // get the coupon code
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      * Get the total number of coupons
142      *
143      * @since 2.1
144      * @param array $filter
145      * @return array
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      * Get the coupon for the given code
159      *
160      * @since 2.1
161      * @param string $code the coupon code
162      * @param string $fields fields to include in response
163      * @return int|WP_Error
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      * Create a coupon
178      *
179      * @TODO implement in 2.2
180      * @param array $data
181      * @return array
182      */
183     public function create_coupon( $data ) {
184 
185         return array();
186     }
187 
188     /**
189      * Edit a coupon
190      *
191      * @TODO implement in 2.2
192      * @param int $id the coupon ID
193      * @param array $data
194      * @return array
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      * Delete a coupon
208      *
209      * @TODO enable along with PUT/POST in 2.2
210      * @param int $id the coupon ID
211      * @param bool $force true to permanently delete coupon, false to move to trash
212      * @return array
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      * Helper method to get coupon post objects
226      *
227      * @since 2.1
228      * @param array $args request arguments for filtering query
229      * @return WP_Query
230      */
231     private function query_coupons( $args ) {
232 
233         // set base query arguments
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 
WooCommerce API documentation generated by ApiGen 2.8.0