2013-11-04 06:36:31 +00:00
< ? php
/**
* WooCommerce API Coupons Class
*
* Handles requests to the / coupons endpoint
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
* @ since 2.1
*/
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2013-11-09 21:20:23 +00:00
class WC_API_Coupons extends WC_API_Resource {
2013-11-04 06:36:31 +00:00
/** @var string $base the route base */
protected $base = '/coupons' ;
/**
* Register the routes for this class
*
* GET | POST / coupons
* GET / coupons / count
* GET | PUT | DELETE / coupons /< id >
*
* @ since 2.1
* @ param array $routes
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function register_routes ( $routes ) {
2013-11-04 06:36:31 +00:00
# GET|POST /coupons
$routes [ $this -> base ] = array (
2013-11-11 00:29:36 +00:00
array ( array ( $this , 'get_coupons' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'create_coupon' ), WC_API_Server :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
2013-11-04 06:36:31 +00:00
);
# GET /coupons/count
$routes [ $this -> base . '/count' ] = array (
2013-11-11 00:29:36 +00:00
array ( array ( $this , 'get_coupons_count' ), WC_API_Server :: READABLE ),
2013-11-04 06:36:31 +00:00
);
# GET|PUT|DELETE /coupons/<id>
$routes [ $this -> base . '/(?P<id>\d+)' ] = array (
2013-11-11 00:29:36 +00:00
array ( array ( $this , 'get_coupon' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'edit_coupon' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
array ( array ( $this , 'delete_coupon' ), WC_API_Server :: DELETABLE ),
);
# GET /coupons/<code> TODO: should looking up coupon codes containing spaces or dashes be supported? OR all-digit coupon codes
$routes [ $this -> base . '/(?P<code>\w+)' ] = array (
array ( array ( $this , 'get_coupon_by_code' ), WC_API_Server :: READABLE ),
2013-11-04 06:36:31 +00:00
);
return $routes ;
}
/**
* Get all coupons
*
* @ since 2.1
* @ param string $fields
2013-11-11 00:29:36 +00:00
* @ param array $filter
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function get_coupons ( $fields = null , $filter = array () ) {
2013-11-04 06:36:31 +00:00
2013-11-11 00:29:36 +00:00
$query = $this -> query_coupons ( $filter );
2013-11-04 06:36:31 +00:00
$coupons = array ();
foreach ( $query -> posts as $coupon_id ) {
2013-11-11 00:29:36 +00:00
if ( ! $this -> is_readable ( $coupon_id ) )
continue ;
$coupons [] = $this -> get_coupon ( $coupon_id , $fields );
2013-11-04 06:36:31 +00:00
}
2013-11-11 00:29:36 +00:00
$this -> server -> query_navigation_headers ( $query );
2013-11-04 06:36:31 +00:00
return array ( 'coupons' => $coupons );
}
/**
* Get the coupon for the given ID
*
* @ since 2.1
* @ param int $id the coupon ID
* @ param string $fields fields to include in response
2013-11-11 00:29:36 +00:00
* @ return array | WP_Error
2013-11-04 06:36:31 +00:00
*/
2013-11-11 00:29:36 +00:00
public function get_coupon ( $id , $fields = null ) {
2013-11-04 06:36:31 +00:00
global $wpdb ;
2013-11-11 00:29:36 +00:00
$id = $this -> validate_request ( $id , 'shop_coupon' , 'read' );
if ( is_wp_error ( $id ) )
return $id ;
2013-11-04 06:36:31 +00:00
// get the coupon code
$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 ) );
2013-11-11 00:29:36 +00:00
if ( is_null ( $code ) )
return new WP_Error ( 'woocommerce_api_invalid_coupon_id' , __ ( 'Invalid coupon ID' , 'woocommerce' ), array ( 'status' => 404 ) );
2013-11-04 06:36:31 +00:00
$coupon = new WC_Coupon ( $code );
$coupon_data = array (
'id' => $coupon -> id ,
'code' => $coupon -> code ,
'type' => $coupon -> type ,
2013-11-11 00:29:36 +00:00
'amount' => ( string ) number_format ( $coupon -> amount , 2 ),
2013-11-04 06:36:31 +00:00
'individual_use' => $coupon -> individual_use ,
'product_ids' => $coupon -> product_ids ,
'exclude_product_ids' => $coupon -> exclude_product_ids ,
'usage_limit' => $coupon -> usage_limit ,
'usage_limit_per_user' => $coupon -> usage_limit_per_user ,
'limit_usage_to_x_items' => $coupon -> limit_usage_to_x_items ,
'usage_count' => $coupon -> usage_count ,
'expiry_date' => $coupon -> expiry_date ,
'apply_before_tax' => $coupon -> apply_before_tax (),
'enable_free_shipping' => $coupon -> enable_free_shipping (),
'product_categories' => $coupon -> product_categories ,
'exclude_product_categories' => $coupon -> exclude_product_categories ,
'exclude_sale_items' => $coupon -> exclude_sale_items (),
'minimum_amount' => $coupon -> minimum_amount ,
'customer_email' => $coupon -> customer_email ,
);
2013-11-14 17:48:20 +00:00
return apply_filters ( 'woocommerce_api_coupon_response' , $coupon_data , $coupon , $fields , $this -> server );
2013-11-04 06:36:31 +00:00
}
/**
* Get the total number of coupons
*
* @ since 2.1
2013-11-11 00:29:36 +00:00
* @ param array $filter
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function get_coupons_count ( $filter = array () ) {
2013-11-04 06:36:31 +00:00
2013-11-11 00:29:36 +00:00
$query = $this -> query_coupons ( $filter );
// TODO: permissions?
2013-11-04 06:36:31 +00:00
return array ( 'count' => $query -> found_posts );
}
2013-11-11 00:29:36 +00:00
/**
* Get the coupon for the given code
*
* @ param string $code the coupon code
* @ param string $fields fields to include in response
* @ return int | WP_Error
*/
public function get_coupon_by_code ( $code , $fields = null ) {
global $wpdb ;
$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 ) );
if ( is_null ( $id ) )
return new WP_Error ( 'woocommerce_api_invalid_coupon_code' , __ ( 'Invalid coupon code' , 'woocommerce' ), array ( 'status' => 404 ) );
return $this -> get_coupon ( $id , $fields );
}
2013-11-04 06:36:31 +00:00
/**
* Create a coupon
*
* @ since 2.1
* @ param array $data
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function create_coupon ( $data ) {
// TODO: permissions check
2013-11-04 06:36:31 +00:00
// TODO: implement - what's the minimum set of data required?
return array ();
}
/**
* Edit a coupon
*
* @ since 2.1
* @ param int $id the coupon ID
* @ param array $data
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function edit_coupon ( $id , $data ) {
$id = $this -> validate_request ( $id , 'shop_coupon' , 'edit' );
if ( is_wp_error ( $id ) )
return $id ;
2013-11-04 06:36:31 +00:00
// TODO: implement
2013-11-11 00:29:36 +00:00
return $this -> get_coupon ( $id );
2013-11-04 06:36:31 +00:00
}
/**
* Delete a coupon
*
* @ since 2.1
* @ param int $id the coupon ID
* @ param bool $force true to permanently delete coupon , false to move to trash
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function delete_coupon ( $id , $force = false ) {
$id = $this -> validate_request ( $id , 'shop_coupon' , 'delete' );
2013-11-04 06:36:31 +00:00
2013-11-11 00:29:36 +00:00
if ( is_wp_error ( $id ) )
return $id ;
return $this -> delete ( $id , 'shop_coupon' , ( 'true' === $force ) );
2013-11-04 06:36:31 +00:00
}
/**
* Helper method to get coupon post objects
*
* @ since 2.1
* @ param array $args request arguments for filtering query
2013-11-11 00:29:36 +00:00
* @ return WP_Query
2013-11-04 06:36:31 +00:00
*/
2013-11-11 00:29:36 +00:00
private function query_coupons ( $args ) {
2013-11-04 06:36:31 +00:00
// set base query arguments
$query_args = array (
'fields' => 'ids' ,
'post_type' => 'shop_coupon' ,
'post_status' => 'publish' ,
);
2013-11-11 00:29:36 +00:00
$query_args = $this -> merge_query_args ( $query_args , $args );
2013-11-04 06:36:31 +00:00
return new WP_Query ( $query_args );
}
}