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
*
2013-11-23 20:01:53 +00:00
* GET / coupons
2013-11-04 06:36:31 +00:00
* GET / coupons / count
2013-11-23 20:01:53 +00:00
* GET / coupons /< id >
2013-11-04 06:36:31 +00:00
*
* @ 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
2014-03-05 05:43:13 +00:00
# GET/PUT /coupons
2013-11-04 06:36:31 +00:00
$routes [ $this -> base ] = array (
2013-11-11 00:29:36 +00:00
array ( array ( $this , 'get_coupons' ), WC_API_Server :: READABLE ),
2014-03-05 05:43:13 +00:00
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
);
2014-03-04 13:44:33 +00:00
# GET/POST/PUT/DELETE /coupons/<id>
2013-11-04 06:36:31 +00:00
$routes [ $this -> base . '/(?P<id>\d+)' ] = array (
2014-03-04 13:44:33 +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 ),
2013-11-11 00:29:36 +00:00
);
2013-11-18 21:47:38 +00:00
# GET /coupons/code/<code>, note that coupon codes can contain spaces, dashes and underscores
$routes [ $this -> base . '/code/(?P<code>\w[\w\s\-]*)' ] = array (
2013-11-11 00:29:36 +00:00
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-19 02:06:45 +00:00
* @ param int $page
2013-11-04 06:36:31 +00:00
* @ return array
*/
2013-11-19 02:06:45 +00:00
public function get_coupons ( $fields = null , $filter = array (), $page = 1 ) {
$filter [ 'page' ] = $page ;
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 ;
2013-11-22 08:41:32 +00:00
$coupons [] = current ( $this -> get_coupon ( $coupon_id , $fields ) );
2013-11-04 06:36:31 +00:00
}
2013-11-19 02:06:45 +00:00
$this -> server -> add_pagination_headers ( $query );
2013-11-11 00:29:36 +00:00
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 );
2013-11-18 21:47:38 +00:00
$coupon_post = get_post ( $coupon -> id );
2013-11-04 06:36:31 +00:00
$coupon_data = array (
2013-11-23 21:36:56 +00:00
'id' => $coupon -> id ,
'code' => $coupon -> code ,
'type' => $coupon -> type ,
'created_at' => $this -> server -> format_datetime ( $coupon_post -> post_date_gmt ),
'updated_at' => $this -> server -> format_datetime ( $coupon_post -> post_modified_gmt ),
2013-11-25 13:34:21 +00:00
'amount' => wc_format_decimal ( $coupon -> amount , 2 ),
2013-11-23 21:36:56 +00:00
'individual_use' => ( 'yes' === $coupon -> individual_use ),
2013-12-10 22:31:41 +00:00
'product_ids' => array_map ( 'absint' , ( array ) $coupon -> product_ids ),
'exclude_product_ids' => array_map ( 'absint' , ( array ) $coupon -> exclude_product_ids ),
2013-11-23 21:36:56 +00:00
'usage_limit' => ( ! empty ( $coupon -> usage_limit ) ) ? $coupon -> usage_limit : null ,
'usage_limit_per_user' => ( ! empty ( $coupon -> usage_limit_per_user ) ) ? $coupon -> usage_limit_per_user : null ,
'limit_usage_to_x_items' => ( int ) $coupon -> limit_usage_to_x_items ,
'usage_count' => ( int ) $coupon -> usage_count ,
'expiry_date' => $this -> server -> format_datetime ( $coupon -> expiry_date ),
'apply_before_tax' => $coupon -> apply_before_tax (),
'enable_free_shipping' => $coupon -> enable_free_shipping (),
2013-12-10 22:31:41 +00:00
'product_category_ids' => array_map ( 'absint' , ( array ) $coupon -> product_categories ),
'exclude_product_category_ids' => array_map ( 'absint' , ( array ) $coupon -> exclude_product_categories ),
2013-11-23 21:36:56 +00:00
'exclude_sale_items' => $coupon -> exclude_sale_items (),
2013-11-25 13:34:21 +00:00
'minimum_amount' => wc_format_decimal ( $coupon -> minimum_amount , 2 ),
2013-11-23 21:36:56 +00:00
'customer_emails' => $coupon -> customer_email ,
2013-11-04 06:36:31 +00:00
);
2013-11-22 08:41:32 +00:00
return array ( 'coupon' => 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 );
2013-11-23 22:11:16 +00:00
if ( ! current_user_can ( 'read_private_shop_coupons' ) )
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 ) );
2013-11-04 06:36:31 +00:00
2013-11-23 21:36:56 +00:00
return array ( 'count' => ( int ) $query -> found_posts );
2013-11-04 06:36:31 +00:00
}
2013-11-11 00:29:36 +00:00
/**
* Get the coupon for the given code
*
2013-11-14 18:56:05 +00:00
* @ since 2.1
2013-11-11 00:29:36 +00:00
* @ 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
*
2014-03-04 13:44:33 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ param array $data
* @ return array
*/
2013-11-11 00:29:36 +00:00
public function create_coupon ( $data ) {
2014-03-04 15:05:38 +00:00
global $wpdb ;
2013-11-11 00:29:36 +00:00
2014-03-04 13:44:33 +00:00
if ( ! current_user_can ( 'publish_shop_coupons' ) ) {
2014-03-04 13:50:31 +00:00
return new WP_Error ( 'woocommerce_api_user_cannot_create_coupon' , __ ( 'You do not have permission to create coupons' , 'woocommerce' ), array ( 'status' => 401 ) );
2014-03-04 13:44:33 +00:00
}
if ( ! isset ( $data [ 'code' ] ) ) {
2014-03-04 13:50:31 +00:00
return new WP_Error ( 'woocommerce_api_missing_coupon_code' , sprintf ( __ ( 'Missing parameter %s' ), 'code' ), array ( 'status' => 400 ) );
2014-03-04 13:44:33 +00:00
}
// Check for duplicate coupon codes
$coupon_found = $wpdb -> get_var ( $wpdb -> prepare ( "
SELECT $wpdb -> posts . ID
FROM $wpdb -> posts
WHERE $wpdb -> posts . post_type = 'shop_coupon'
AND $wpdb -> posts . post_status = 'publish'
AND $wpdb -> posts . post_title = '%s'
" , wc_clean( $data['code'] ) ) );
if ( $coupon_found ) {
2014-03-04 13:50:31 +00:00
return new WP_Error ( 'woocommerce_api_invalid_coupon_code' , __ ( 'The coupon code already exists' ), array ( 'status' => 400 ) );
2014-03-04 13:44:33 +00:00
}
$defaults = array (
'type' => 'fixed_cart' ,
'amount' => 0 ,
'individual_use' => 'no' ,
'product_ids' => '' ,
'exclude_product_ids' => '' ,
'usage_limit' => '' ,
'usage_limit_per_user' => '' ,
'limit_usage_to_x_items' => '' ,
'usage_count' => '' ,
'expiry_date' => '' ,
'apply_before_tax' => 'yes' ,
'free_shipping' => 'no' ,
2014-03-04 15:05:38 +00:00
'product_categories' => '' ,
'exclude_product_categories' => '' ,
2014-03-04 13:44:33 +00:00
'exclude_sale_items' => 'no' ,
'minimum_amount' => '' ,
2014-03-04 15:05:38 +00:00
'customer_email' => '' ,
2014-03-04 13:44:33 +00:00
);
$coupon_data = wp_parse_args ( $data , $defaults );
$new_coupon = array (
2014-03-05 05:28:05 +00:00
'post_title' => wc_clean ( $coupon_data [ 'code' ] ),
'post_content' => '' ,
'post_status' => 'publish' ,
'post_author' => get_current_user_id (),
2014-03-04 13:44:33 +00:00
'post_type' => 'shop_coupon'
);
$id = wp_insert_post ( $new_coupon , $wp_error = false );
2014-03-05 05:28:05 +00:00
if ( is_wp_error ( $id ) ) {
return new WP_Error ( 'woocommerce_api_cannot_create_coupon' , $id -> get_error_message (), array ( 'status' => 400 ) );
}
2014-03-04 13:44:33 +00:00
// Add POST Meta
update_post_meta ( $id , 'discount_type' , $coupon_data [ 'type' ] );
update_post_meta ( $id , 'coupon_amount' , wc_format_decimal ( $coupon_data [ 'amount' ] ) );
update_post_meta ( $id , 'individual_use' , $coupon_data [ 'individual_use' ] );
2014-03-05 05:28:05 +00:00
update_post_meta ( $id , 'product_ids' , implode ( ',' , array_filter ( array_map ( 'intval' , explode ( ',' , $coupon_data [ 'product_ids' ] ) ) ) ) );
update_post_meta ( $id , 'exclude_product_ids' , implode ( ',' , array_filter ( array_map ( 'intval' , explode ( ',' , $coupon_data [ 'exclude_product_ids' ] ) ) ) ) );
2014-03-04 13:44:33 +00:00
update_post_meta ( $id , 'usage_limit' , absint ( $coupon_data [ 'usage_limit' ] ) );
update_post_meta ( $id , 'usage_limit_per_user' , absint ( $coupon_data [ 'usage_limit_per_user' ] ) );
update_post_meta ( $id , 'limit_usage_to_x_items' , absint ( $coupon_data [ 'limit_usage_to_x_items' ] ) );
update_post_meta ( $id , 'usage_count' , absint ( $coupon_data [ 'usage_count' ] ) );
update_post_meta ( $id , 'expiry_date' , wc_clean ( $coupon_data [ 'expiry_date' ] ) );
update_post_meta ( $id , 'apply_before_tax' , $coupon_data [ 'apply_before_tax' ] );
update_post_meta ( $id , 'free_shipping' , $coupon_data [ 'free_shipping' ] );
2014-03-04 15:05:38 +00:00
update_post_meta ( $id , 'product_categories' , implode ( ',' , array_filter ( array_map ( 'intval' , explode ( ',' , $coupon_data [ 'product_categories' ] ) ) ) ) );
update_post_meta ( $id , 'exclude_product_categories' , implode ( ',' , array_filter ( array_map ( 'intval' , explode ( ',' , $coupon_data [ 'exclude_product_categories' ] ) ) ) ) );
2014-03-04 13:44:33 +00:00
update_post_meta ( $id , 'exclude_sale_items' , wc_clean ( $coupon_data [ 'exclude_sale_items' ] ) );
update_post_meta ( $id , 'minimum_amount' , wc_format_decimal ( $coupon_data [ 'minimum_amount' ] ) );
2014-03-04 15:05:38 +00:00
update_post_meta ( $id , 'customer_email' , implode ( ',' , array_filter ( array_map ( 'trim' , explode ( ',' , $coupon_data [ 'customer_email' ] ) ) ) ) );
2014-03-04 13:44:33 +00:00
do_action ( 'woocommerce_api_create_coupon' , $id , $data );
return $this -> get_coupon ( $id );
2013-11-04 06:36:31 +00:00
}
/**
* Edit a coupon
*
2014-03-04 13:44:33 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ 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' );
2014-03-05 05:43:13 +00:00
if ( is_wp_error ( $id ) ) {
2014-03-04 15:05:38 +00:00
return $id ;
2014-03-05 05:43:13 +00:00
}
if ( isset ( $data [ 'code' ] ) ) {
2014-03-05 06:03:10 +00:00
$id = wp_update_post ( array ( 'ID' => intval ( $id ), 'post_title' => wc_clean ( $data [ 'code' ] ) ) );
if ( 0 === $id ) {
return new WP_Error ( 'woocommerce_api_cannot_update_coupon' , __ ( 'Failed to update coupon' , 'woocommerce' ), array ( 'status' => 400 ) );
}
2014-03-05 05:43:13 +00:00
}
if ( isset ( $data [ 'type' ] ) ) {
2014-03-05 05:59:27 +00:00
update_post_meta ( $id , 'discount_type' , $data [ 'type' ] );
2014-03-05 05:43:13 +00:00
}
if ( isset ( $data [ 'amount' ] ) ) {
update_post_meta ( $id , 'coupon_amount' , wc_format_decimal ( $data [ 'amount' ] ) );
}
if ( isset ( $data [ 'individual_use' ] ) ) {
update_post_meta ( $id , 'individual_use' , $data [ 'individual_use' ] );
}
if ( isset ( $data [ 'product_ids' ] ) ) {
update_post_meta ( $id , 'product_ids' , implode ( ',' , array_filter ( array_map ( 'intval' , explode ( ',' , $data [ 'product_ids' ] ) ) ) ) );
}
if ( isset ( $data [ 'exclude_product_ids' ] ) ) {
update_post_meta ( $id , 'exclude_product_ids' , implode ( ',' , array_filter ( array_map ( 'intval' , explode ( ',' , $data [ 'exclude_product_ids' ] ) ) ) ) );
}
if ( isset ( $data [ 'usage_limit' ] ) ) {
update_post_meta ( $id , 'usage_limit' , absint ( $data [ 'usage_limit' ] ) );
}
if ( isset ( $data [ 'usage_limit_per_user' ] ) ) {
update_post_meta ( $id , 'usage_limit_per_user' , absint ( $data [ 'usage_limit_per_user' ] ) );
}
if ( isset ( $data [ 'limit_usage_to_x_items' ] ) ) {
update_post_meta ( $id , 'limit_usage_to_x_items' , absint ( $data [ 'limit_usage_to_x_items' ] ) );
}
if ( isset ( $data [ 'usage_count' ] ) ) {
update_post_meta ( $id , 'usage_count' , absint ( $data [ 'usage_count' ] ) );
}
if ( isset ( $data [ 'expiry_date' ] ) ) {
update_post_meta ( $id , 'expiry_date' , wc_clean ( $data [ 'expiry_date' ] ) );
}
if ( isset ( $data [ 'apply_before_tax' ] ) ) {
update_post_meta ( $id , 'apply_before_tax' , wc_clean ( $data [ 'apply_before_tax' ] ) );
}
if ( isset ( $data [ 'free_shipping' ] ) ) {
update_post_meta ( $id , 'free_shipping' , wc_clean ( $data [ 'free_shipping' ] ) );
}
if ( isset ( $data [ 'product_categories' ] ) ) {
update_post_meta ( $id , 'product_categories' , implode ( ',' , array_filter ( array_map ( 'intval' , explode ( ',' , $data [ 'product_categories' ] ) ) ) ) );
}
if ( isset ( $data [ 'exclude_product_categories' ] ) ) {
update_post_meta ( $id , 'exclude_product_categories' , implode ( ',' , array_filter ( array_map ( 'intval' , explode ( ',' , $data [ 'exclude_product_categories' ] ) ) ) ) );
}
if ( isset ( $data [ 'exclude_sale_items' ] ) ) {
update_post_meta ( $id , 'exclude_sale_items' , wc_clean ( $data [ 'exclude_sale_items' ] ) );
}
if ( isset ( $data [ 'minimum_amount' ] ) ) {
update_post_meta ( $id , 'minimum_amount' , wc_format_decimal ( $data [ 'minimum_amount' ] ) );
}
if ( isset ( $data [ 'customer_email' ] ) ) {
update_post_meta ( $id , 'customer_email' , implode ( ',' , array_filter ( array_map ( 'trim' , explode ( ',' , $data [ 'customer_email' ] ) ) ) ) );
}
do_action ( 'woocommerce_api_edit_coupon' , $id , $data );
2013-11-04 06:36:31 +00:00
2013-11-11 00:29:36 +00:00
return $this -> get_coupon ( $id );
2013-11-04 06:36:31 +00:00
}
/**
* Delete a coupon
*
2014-03-04 13:44:33 +00:00
* @ since 2.2
2013-11-04 06:36:31 +00:00
* @ 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 );
}
}