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
*/
2014-09-20 19:24:20 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-11-04 06:36:31 +00:00
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-06 05:45:32 +00:00
# GET/POST /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
2016-08-27 03:11:30 +00:00
$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-06 05:45:32 +00:00
# GET/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
);
2015-05-07 16:13:41 +00:00
# POST|PUT /coupons/bulk
$routes [ $this -> base . '/bulk' ] = array (
array ( array ( $this , 'bulk' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
);
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 ();
2015-01-05 17:42:25 +00:00
foreach ( $query -> posts as $coupon_id ) {
2013-11-04 06:36:31 +00:00
2014-03-05 06:07:42 +00:00
if ( ! $this -> is_readable ( $coupon_id ) ) {
2013-11-11 00:29:36 +00:00
continue ;
2014-03-05 06:07:42 +00:00
}
2013-11-11 00:29:36 +00:00
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 ) {
2015-01-05 17:42:25 +00:00
try {
2013-11-11 00:29:36 +00:00
2015-01-05 17:42:25 +00:00
$id = $this -> validate_request ( $id , 'shop_coupon' , 'read' );
2013-11-11 00:29:36 +00:00
2015-01-05 17:42:25 +00:00
if ( is_wp_error ( $id ) ) {
return $id ;
}
2013-11-04 06:36:31 +00:00
2016-11-18 18:51:45 +00:00
$coupon = new WC_Coupon ( $id );
2013-11-04 06:36:31 +00:00
2016-11-18 18:51:45 +00:00
if ( 0 === $coupon -> get_id () ) {
2015-01-05 17:42:25 +00:00
throw new WC_API_Exception ( 'woocommerce_api_invalid_coupon_id' , __ ( 'Invalid coupon ID' , 'woocommerce' ), 404 );
}
2013-11-04 06:36:31 +00:00
2015-01-05 17:42:25 +00:00
$coupon_data = array (
2016-02-23 16:01:40 +00:00
'id' => $coupon -> get_id (),
'code' => $coupon -> get_code (),
'type' => $coupon -> get_discount_type (),
2017-03-14 23:00:15 +00:00
'created_at' => $this -> server -> format_datetime ( $coupon -> get_date_created () ? $coupon -> get_date_created () -> getTimestamp () : 0 ), // API gives UTC times.
'updated_at' => $this -> server -> format_datetime ( $coupon -> get_date_modified () ? $coupon -> get_date_modified () -> getTimestamp () : 0 ), // API gives UTC times.
2016-02-23 16:01:40 +00:00
'amount' => wc_format_decimal ( $coupon -> get_amount (), 2 ),
2016-02-25 16:36:39 +00:00
'individual_use' => $coupon -> get_individual_use (),
2016-02-23 16:01:40 +00:00
'product_ids' => array_map ( 'absint' , ( array ) $coupon -> get_product_ids () ),
'exclude_product_ids' => array_map ( 'absint' , ( array ) $coupon -> get_excluded_product_ids () ),
2016-08-15 18:28:07 +00:00
'usage_limit' => $coupon -> get_usage_limit () ? $coupon -> get_usage_limit () : null ,
'usage_limit_per_user' => $coupon -> get_usage_limit_per_user () ? $coupon -> get_usage_limit_per_user () : null ,
2016-02-23 16:01:40 +00:00
'limit_usage_to_x_items' => ( int ) $coupon -> get_limit_usage_to_x_items (),
'usage_count' => ( int ) $coupon -> get_usage_count (),
2017-03-14 23:00:15 +00:00
'expiry_date' => $coupon -> get_date_expires () ? $this -> server -> format_datetime ( $coupon -> get_date_expires () -> getTimestamp () ) : null , // API gives UTC times.
2016-02-25 16:36:39 +00:00
'enable_free_shipping' => $coupon -> get_free_shipping (),
2016-02-23 16:01:40 +00:00
'product_category_ids' => array_map ( 'absint' , ( array ) $coupon -> get_product_categories () ),
2016-02-25 18:58:00 +00:00
'exclude_product_category_ids' => array_map ( 'absint' , ( array ) $coupon -> get_excluded_product_categories () ),
2016-02-25 16:36:39 +00:00
'exclude_sale_items' => $coupon -> get_exclude_sale_items (),
2016-02-23 16:01:40 +00:00
'minimum_amount' => wc_format_decimal ( $coupon -> get_minimum_amount (), 2 ),
'maximum_amount' => wc_format_decimal ( $coupon -> get_maximum_amount (), 2 ),
'customer_emails' => $coupon -> get_email_restrictions (),
2016-11-22 13:59:08 +00:00
'description' => $coupon -> get_description (),
2015-01-05 17:42:25 +00:00
);
return array ( 'coupon' => apply_filters ( 'woocommerce_api_coupon_response' , $coupon_data , $coupon , $fields , $this -> server ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2013-11-04 06:36:31 +00:00
}
/**
* Get the total number of coupons
*
* @ since 2.1
2017-05-15 11:50:52 +00:00
*
2013-11-11 00:29:36 +00:00
* @ param array $filter
2017-05-15 11:50:52 +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_coupons_count ( $filter = array () ) {
2015-01-05 17:42:25 +00:00
try {
if ( ! current_user_can ( 'read_private_shop_coupons' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_coupons_count' , __ ( 'You do not have permission to read the coupons count' , 'woocommerce' ), 401 );
}
2013-11-04 06:36:31 +00:00
2015-01-05 18:40:27 +00:00
$query = $this -> query_coupons ( $filter );
2015-01-05 17:42:25 +00:00
return array ( 'count' => ( int ) $query -> found_posts );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
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 ;
2015-01-05 17:42:25 +00:00
try {
2016-03-07 13:07:59 +00:00
$id = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1; " , $code ) );
2013-11-11 00:29:36 +00:00
2015-01-05 17:42:25 +00:00
if ( is_null ( $id ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_coupon_code' , __ ( 'Invalid coupon code' , 'woocommerce' ), 404 );
}
2013-11-11 00:29:36 +00:00
2015-01-05 17:42:25 +00:00
return $this -> get_coupon ( $id , $fields );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2013-11-11 00:29:36 +00:00
}
2013-11-04 06:36:31 +00:00
/**
* Create a coupon
*
2014-03-04 13:44:33 +00:00
* @ since 2.2
2017-05-15 11:50:52 +00:00
*
2013-11-04 06:36:31 +00:00
* @ param array $data
2017-05-15 11:50:52 +00:00
*
* @ return array | WP_Error
2013-11-04 06:36:31 +00:00
*/
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
2015-01-05 17:42:25 +00:00
try {
2015-04-13 16:06:19 +00:00
if ( ! isset ( $data [ 'coupon' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_coupon_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'coupon' ), 400 );
}
$data = $data [ 'coupon' ];
2014-08-30 19:50:40 +00:00
2015-01-05 17:42:25 +00:00
// Check user permission
if ( ! current_user_can ( 'publish_shop_coupons' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_create_coupon' , __ ( 'You do not have permission to create coupons' , 'woocommerce' ), 401 );
}
2014-03-04 13:44:33 +00:00
2015-01-05 17:42:25 +00:00
$data = apply_filters ( 'woocommerce_api_create_coupon_data' , $data , $this );
2014-08-30 19:50:40 +00:00
2015-01-05 17:42:25 +00:00
// Check if coupon code is specified
if ( ! isset ( $data [ 'code' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_coupon_code' , sprintf ( __ ( 'Missing parameter %s' , 'woocommerce' ), 'code' ), 400 );
}
2014-03-04 13:44:33 +00:00
2017-02-22 17:40:24 +00:00
$coupon_code = wc_format_coupon_code ( $data [ 'code' ] );
2016-08-30 10:43:53 +00:00
$id_from_code = wc_get_coupon_id_by_code ( $coupon_code );
2014-03-06 05:45:32 +00:00
2016-08-30 10:43:53 +00:00
if ( $id_from_code ) {
2015-01-05 17:42:25 +00:00
throw new WC_API_Exception ( 'woocommerce_api_coupon_code_already_exists' , __ ( 'The coupon code already exists' , 'woocommerce' ), 400 );
}
2014-03-04 13:44:33 +00:00
2015-01-05 17:42:25 +00:00
$defaults = array (
'type' => 'fixed_cart' ,
'amount' => 0 ,
'individual_use' => false ,
'product_ids' => array (),
'exclude_product_ids' => array (),
'usage_limit' => '' ,
'usage_limit_per_user' => '' ,
'limit_usage_to_x_items' => '' ,
'usage_count' => '' ,
'expiry_date' => '' ,
'enable_free_shipping' => false ,
'product_category_ids' => array (),
'exclude_product_category_ids' => array (),
'exclude_sale_items' => false ,
'minimum_amount' => '' ,
'maximum_amount' => '' ,
'customer_emails' => array (),
2016-08-27 01:46:45 +00:00
'description' => '' ,
2015-01-05 17:42:25 +00:00
);
$coupon_data = wp_parse_args ( $data , $defaults );
2014-03-04 13:44:33 +00:00
2015-01-05 17:42:25 +00:00
// Validate coupon types
2015-01-20 19:03:21 +00:00
if ( ! in_array ( wc_clean ( $coupon_data [ 'type' ] ), array_keys ( wc_get_coupon_types () ) ) ) {
2015-01-05 17:42:25 +00:00
throw new WC_API_Exception ( 'woocommerce_api_invalid_coupon_type' , sprintf ( __ ( 'Invalid coupon type - the coupon type must be any of these: %s' , 'woocommerce' ), implode ( ', ' , array_keys ( wc_get_coupon_types () ) ) ), 400 );
}
2014-03-04 13:44:33 +00:00
2015-01-05 17:42:25 +00:00
$new_coupon = array (
'post_title' => $coupon_code ,
'post_content' => '' ,
'post_status' => 'publish' ,
'post_author' => get_current_user_id (),
'post_type' => 'shop_coupon' ,
2016-08-27 01:46:45 +00:00
'post_excerpt' => $coupon_data [ 'description' ],
2015-01-05 17:42:25 +00:00
);
2014-03-06 05:49:01 +00:00
2016-03-02 14:54:33 +00:00
$id = wp_insert_post ( $new_coupon , true );
2014-03-04 13:44:33 +00:00
2015-01-05 17:42:25 +00:00
if ( is_wp_error ( $id ) ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_create_coupon' , $id -> get_error_message (), 400 );
}
2014-03-04 13:44:33 +00:00
2015-01-05 17:42:25 +00:00
// Set coupon 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' , ( true === $coupon_data [ 'individual_use' ] ) ? 'yes' : 'no' );
update_post_meta ( $id , 'product_ids' , implode ( ',' , array_filter ( array_map ( 'intval' , $coupon_data [ 'product_ids' ] ) ) ) );
update_post_meta ( $id , 'exclude_product_ids' , implode ( ',' , array_filter ( array_map ( 'intval' , $coupon_data [ 'exclude_product_ids' ] ) ) ) );
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' ] ) );
2015-01-20 19:57:27 +00:00
update_post_meta ( $id , 'expiry_date' , $this -> get_coupon_expiry_date ( wc_clean ( $coupon_data [ 'expiry_date' ] ) ) );
2017-03-15 15:10:42 +00:00
update_post_meta ( $id , 'date_expires' , $this -> get_coupon_expiry_date ( wc_clean ( $coupon_data [ 'expiry_date' ] ), true ) );
2015-01-05 17:42:25 +00:00
update_post_meta ( $id , 'free_shipping' , ( true === $coupon_data [ 'enable_free_shipping' ] ) ? 'yes' : 'no' );
update_post_meta ( $id , 'product_categories' , array_filter ( array_map ( 'intval' , $coupon_data [ 'product_category_ids' ] ) ) );
update_post_meta ( $id , 'exclude_product_categories' , array_filter ( array_map ( 'intval' , $coupon_data [ 'exclude_product_category_ids' ] ) ) );
update_post_meta ( $id , 'exclude_sale_items' , ( true === $coupon_data [ 'exclude_sale_items' ] ) ? 'yes' : 'no' );
update_post_meta ( $id , 'minimum_amount' , wc_format_decimal ( $coupon_data [ 'minimum_amount' ] ) );
update_post_meta ( $id , 'maximum_amount' , wc_format_decimal ( $coupon_data [ 'maximum_amount' ] ) );
update_post_meta ( $id , 'customer_email' , array_filter ( array_map ( 'sanitize_email' , $coupon_data [ 'customer_emails' ] ) ) );
do_action ( 'woocommerce_api_create_coupon' , $id , $data );
2017-08-10 21:57:31 +00:00
do_action ( 'woocommerce_new_coupon' , $id );
2015-01-05 17:42:25 +00:00
$this -> server -> send_status ( 201 );
return $this -> get_coupon ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
2014-03-05 05:28:05 +00:00
}
2013-11-04 06:36:31 +00:00
}
/**
* Edit a coupon
*
2014-03-04 13:44:33 +00:00
* @ since 2.2
2017-05-15 11:50:52 +00:00
*
2013-11-04 06:36:31 +00:00
* @ param int $id the coupon ID
* @ param array $data
2017-05-15 11:50:52 +00:00
*
* @ return array | WP_Error
2013-11-04 06:36:31 +00:00
*/
2013-11-11 00:29:36 +00:00
public function edit_coupon ( $id , $data ) {
2015-01-05 17:42:25 +00:00
try {
2015-04-13 16:06:19 +00:00
if ( ! isset ( $data [ 'coupon' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_coupon_data' , sprintf ( __ ( 'No %1$s data specified to edit %1$s' , 'woocommerce' ), 'coupon' ), 400 );
}
$data = $data [ 'coupon' ];
2014-08-30 19:50:40 +00:00
2015-01-05 17:42:25 +00:00
$id = $this -> validate_request ( $id , 'shop_coupon' , 'edit' );
2013-11-11 00:29:36 +00:00
2015-01-05 17:42:25 +00:00
if ( is_wp_error ( $id ) ) {
return $id ;
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
$data = apply_filters ( 'woocommerce_api_edit_coupon_data' , $data , $id , $this );
2014-08-30 19:50:40 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'code' ] ) ) {
global $wpdb ;
2014-03-05 07:34:01 +00:00
2017-02-22 17:40:24 +00:00
$coupon_code = wc_format_coupon_code ( $data [ 'code' ] );
2016-08-30 10:43:53 +00:00
$id_from_code = wc_get_coupon_id_by_code ( $coupon_code , $id );
2014-03-05 07:34:01 +00:00
2016-08-30 10:43:53 +00:00
if ( $id_from_code ) {
2015-01-05 17:42:25 +00:00
throw new WC_API_Exception ( 'woocommerce_api_coupon_code_already_exists' , __ ( 'The coupon code already exists' , 'woocommerce' ), 400 );
}
2014-03-05 07:34:01 +00:00
2015-07-20 15:49:21 +00:00
$updated = wp_update_post ( array ( 'ID' => intval ( $id ), 'post_title' => $coupon_code ) );
if ( 0 === $updated ) {
throw new WC_API_Exception ( 'woocommerce_api_cannot_update_coupon' , __ ( 'Failed to update coupon' , 'woocommerce' ), 400 );
}
}
if ( isset ( $data [ 'description' ] ) ) {
$updated = wp_update_post ( array ( 'ID' => intval ( $id ), 'post_excerpt' => $data [ 'description' ] ) );
if ( 0 === $updated ) {
2015-01-05 17:42:25 +00:00
throw new WC_API_Exception ( 'woocommerce_api_cannot_update_coupon' , __ ( 'Failed to update coupon' , 'woocommerce' ), 400 );
}
2014-03-05 06:03:10 +00:00
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'type' ] ) ) {
// Validate coupon types
if ( ! in_array ( wc_clean ( $data [ 'type' ] ), array_keys ( wc_get_coupon_types () ) ) ) {
throw new WC_API_Exception ( 'woocommerce_api_invalid_coupon_type' , sprintf ( __ ( 'Invalid coupon type - the coupon type must be any of these: %s' , 'woocommerce' ), implode ( ', ' , array_keys ( wc_get_coupon_types () ) ) ), 400 );
}
update_post_meta ( $id , 'discount_type' , $data [ 'type' ] );
2014-03-06 05:45:32 +00:00
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'amount' ] ) ) {
update_post_meta ( $id , 'coupon_amount' , wc_format_decimal ( $data [ 'amount' ] ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'individual_use' ] ) ) {
update_post_meta ( $id , 'individual_use' , ( true === $data [ 'individual_use' ] ) ? 'yes' : 'no' );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'product_ids' ] ) ) {
update_post_meta ( $id , 'product_ids' , implode ( ',' , array_filter ( array_map ( 'intval' , $data [ 'product_ids' ] ) ) ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'exclude_product_ids' ] ) ) {
update_post_meta ( $id , 'exclude_product_ids' , implode ( ',' , array_filter ( array_map ( 'intval' , $data [ 'exclude_product_ids' ] ) ) ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'usage_limit' ] ) ) {
update_post_meta ( $id , 'usage_limit' , absint ( $data [ 'usage_limit' ] ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'usage_limit_per_user' ] ) ) {
update_post_meta ( $id , 'usage_limit_per_user' , absint ( $data [ 'usage_limit_per_user' ] ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'limit_usage_to_x_items' ] ) ) {
update_post_meta ( $id , 'limit_usage_to_x_items' , absint ( $data [ 'limit_usage_to_x_items' ] ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'usage_count' ] ) ) {
update_post_meta ( $id , 'usage_count' , absint ( $data [ 'usage_count' ] ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'expiry_date' ] ) ) {
update_post_meta ( $id , 'expiry_date' , $this -> get_coupon_expiry_date ( wc_clean ( $data [ 'expiry_date' ] ) ) );
2017-03-15 15:10:42 +00:00
update_post_meta ( $id , 'date_expires' , $this -> get_coupon_expiry_date ( wc_clean ( $data [ 'expiry_date' ] ), true ) );
2015-01-05 17:42:25 +00:00
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'enable_free_shipping' ] ) ) {
update_post_meta ( $id , 'free_shipping' , ( true === $data [ 'enable_free_shipping' ] ) ? 'yes' : 'no' );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'product_category_ids' ] ) ) {
update_post_meta ( $id , 'product_categories' , array_filter ( array_map ( 'intval' , $data [ 'product_category_ids' ] ) ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'exclude_product_category_ids' ] ) ) {
update_post_meta ( $id , 'exclude_product_categories' , array_filter ( array_map ( 'intval' , $data [ 'exclude_product_category_ids' ] ) ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'exclude_sale_items' ] ) ) {
update_post_meta ( $id , 'exclude_sale_items' , ( true === $data [ 'exclude_sale_items' ] ) ? 'yes' : 'no' );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'minimum_amount' ] ) ) {
update_post_meta ( $id , 'minimum_amount' , wc_format_decimal ( $data [ 'minimum_amount' ] ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'maximum_amount' ] ) ) {
update_post_meta ( $id , 'maximum_amount' , wc_format_decimal ( $data [ 'maximum_amount' ] ) );
}
2014-08-08 11:38:58 +00:00
2015-01-05 17:42:25 +00:00
if ( isset ( $data [ 'customer_emails' ] ) ) {
update_post_meta ( $id , 'customer_email' , array_filter ( array_map ( 'sanitize_email' , $data [ 'customer_emails' ] ) ) );
}
2014-03-05 05:43:13 +00:00
2015-01-05 17:42:25 +00:00
do_action ( 'woocommerce_api_edit_coupon' , $id , $data );
2017-08-10 21:57:31 +00:00
do_action ( 'woocommerce_update_coupon' , $id );
2013-11-04 06:36:31 +00:00
2015-01-05 17:42:25 +00:00
return $this -> get_coupon ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
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
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2013-11-04 06:36:31 +00:00
*/
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
2014-03-05 06:07:42 +00:00
if ( is_wp_error ( $id ) ) {
2013-11-11 00:29:36 +00:00
return $id ;
2014-03-05 06:07:42 +00:00
}
2013-11-11 00:29:36 +00:00
2014-08-30 19:50:40 +00:00
do_action ( 'woocommerce_api_delete_coupon' , $id , $this );
2013-11-11 00:29:36 +00:00
return $this -> delete ( $id , 'shop_coupon' , ( 'true' === $force ) );
2013-11-04 06:36:31 +00:00
}
2014-11-25 14:44:33 +00:00
/**
* expiry_date format
*
* @ since 2.3 . 0
2015-01-23 14:35:15 +00:00
* @ param string $expiry_date
2017-03-14 23:00:15 +00:00
* @ param bool $as_timestamp ( default : false )
* @ return string | int
2014-11-25 14:44:33 +00:00
*/
2017-03-14 23:00:15 +00:00
protected function get_coupon_expiry_date ( $expiry_date , $as_timestamp = false ) {
2014-11-25 14:44:33 +00:00
if ( '' != $expiry_date ) {
2017-03-14 23:00:15 +00:00
if ( $as_timestamp ) {
return strtotime ( $expiry_date );
}
2014-11-25 14:44:33 +00:00
return date ( 'Y-m-d' , strtotime ( $expiry_date ) );
}
return '' ;
}
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 );
}
2015-05-07 16:13:41 +00:00
/**
* Bulk update or insert coupons
* Accepts an array with coupons in the formats supported by
* WC_API_Coupons -> create_coupon () and WC_API_Coupons -> edit_coupon ()
*
* @ since 2.4 . 0
2017-05-15 11:50:52 +00:00
*
2015-05-07 16:13:41 +00:00
* @ param array $data
2017-05-15 11:50:52 +00:00
*
* @ return array | WP_Error
2015-05-07 16:13:41 +00:00
*/
public function bulk ( $data ) {
try {
if ( ! isset ( $data [ 'coupons' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_coupons_data' , sprintf ( __ ( 'No %1$s data specified to create/edit %1$s' , 'woocommerce' ), 'coupons' ), 400 );
}
$data = $data [ 'coupons' ];
$limit = apply_filters ( 'woocommerce_api_bulk_limit' , 100 , 'coupons' );
// Limit bulk operation
if ( count ( $data ) > $limit ) {
2016-10-11 01:39:13 +00:00
throw new WC_API_Exception ( 'woocommerce_api_coupons_request_entity_too_large' , sprintf ( __ ( 'Unable to accept more than %s items for this request.' , 'woocommerce' ), $limit ), 413 );
2015-05-07 16:13:41 +00:00
}
$coupons = array ();
foreach ( $data as $_coupon ) {
$coupon_id = 0 ;
// Try to get the coupon ID
if ( isset ( $_coupon [ 'id' ] ) ) {
$coupon_id = intval ( $_coupon [ 'id' ] );
}
// Coupon exists / edit coupon
if ( $coupon_id ) {
$edit = $this -> edit_coupon ( $coupon_id , array ( 'coupon' => $_coupon ) );
if ( is_wp_error ( $edit ) ) {
$coupons [] = array (
'id' => $coupon_id ,
2016-08-27 01:46:45 +00:00
'error' => array ( 'code' => $edit -> get_error_code (), 'message' => $edit -> get_error_message () ),
2015-05-07 16:13:41 +00:00
);
} else {
$coupons [] = $edit [ 'coupon' ];
}
2016-09-02 03:15:49 +00:00
} else {
2015-05-07 16:13:41 +00:00
2016-09-02 03:15:49 +00:00
// Coupon don't exists / create coupon
2015-05-07 16:13:41 +00:00
$new = $this -> create_coupon ( array ( 'coupon' => $_coupon ) );
if ( is_wp_error ( $new ) ) {
$coupons [] = array (
'id' => $coupon_id ,
2016-08-27 01:46:45 +00:00
'error' => array ( 'code' => $new -> get_error_code (), 'message' => $new -> get_error_message () ),
2015-05-07 16:13:41 +00:00
);
} else {
$coupons [] = $new [ 'coupon' ];
}
}
}
return array ( 'coupons' => apply_filters ( 'woocommerce_api_coupons_bulk_response' , $coupons , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2013-11-04 06:36:31 +00:00
}