[REST API v1] Implement CRUD to coupons

This commit is contained in:
Claudio Sanches 2017-02-09 19:22:12 -02:00
parent 5d87a28407
commit 23fbc31dfb
2 changed files with 190 additions and 470 deletions

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Coupons controller class.
*
* @package WooCommerce/API
* @extends WC_REST_Posts_Controller
* @extends WC_REST_Coupons_V1_Controller
*/
class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
class WC_REST_Coupons_Controller extends WC_REST_Coupons_V1_Controller {
/**
* Endpoint namespace.
@ -29,118 +29,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
*/
protected $namespace = 'wc/v2';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'coupons';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'shop_coupon';
/**
* Order refunds actions.
*/
public function __construct() {
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
}
/**
* Register the routes for coupons.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
'code' => array(
'description' => __( 'Coupon code.', 'woocommerce' ),
'required' => true,
'type' => 'string',
),
) ),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'force' => array(
'default' => false,
'type' => 'boolean',
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ),
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_batch_schema' ),
) );
}
/**
* Query args.
*
* @param array $args
* @param WP_REST_Request $request
* @return array
*/
public function query_args( $args, $request ) {
global $wpdb;
if ( ! empty( $request['code'] ) ) {
$id = wc_get_coupon_id_by_code( $request['code'] );
$args['post__in'] = array( $id );
}
return $args;
}
/**
* Prepare a single coupon output for response.
*
@ -149,16 +37,11 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
* @return WP_REST_Response $data
*/
public function prepare_item_for_response( $post, $request ) {
$coupon = new WC_Coupon( (int) $post->ID );
$data = $coupon->get_data();
// The API returns 'expiry_date' and 'exclude_product_ids' instead of date_expires and 'excluded_product_ids'.
$data['expiry_date'] = $data['date_expires'];
$data['exclude_product_ids'] = $data['excluded_product_ids'];
unset( $data['excluded_product_ids'], $data['date_expires'] );
$coupon = new WC_Coupon( (int) $post->ID );
$data = $coupon->get_data();
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
$format_date = array( 'date_created', 'date_modified', 'expiry_date' );
$format_date = array( 'date_created', 'date_modified', 'date_expires' );
$format_null = array( 'usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items' );
// Format decimal values.
@ -195,15 +78,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
}
/**
* Only reutrn writeable props from schema.
* @param array $schema
* @return bool
*/
protected function filter_writable_props( $schema ) {
return empty( $schema['readonly'] );
}
/**
* Prepare a single coupon for create or update.
*
@ -218,14 +92,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
$schema = $this->get_item_schema();
$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
// BW compat
if ( $request['exclude_product_ids'] ) {
$request['excluded_product_ids'] = $request['exclude_product_ids'];
}
if ( $request['expiry_date'] ) {
$request['date_expires'] = $request['expiry_date'];
}
// Validate required POST fields.
if ( 'POST' === $request->get_method() && 0 === $coupon->get_id() ) {
if ( empty( $request['code'] ) ) {
@ -233,7 +99,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
}
}
// Handle all writable props
// Handle all writable props.
foreach ( $data_keys as $key ) {
$value = $request[ $key ];
@ -281,104 +147,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request );
}
/**
* Create a single item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
if ( ! empty( $request['id'] ) ) {
/* translators: %s: post type */
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
}
$coupon_id = $this->save_coupon( $request );
if ( is_wp_error( $coupon_id ) ) {
return $coupon_id;
}
$post = get_post( $coupon_id );
$this->update_additional_fields_for_object( $post, $request );
$this->add_post_meta_fields( $post, $request );
/**
* Fires after a single item is created or updated via the REST API.
*
* @param object $post Inserted object (not a WP_Post object).
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $post, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
return $response;
}
/**
* Update a single coupon.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
try {
$post_id = (int) $request['id'];
if ( empty( $post_id ) || get_post_type( $post_id ) !== $this->post_type ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
}
$coupon_id = $this->save_coupon( $request );
if ( is_wp_error( $coupon_id ) ) {
return $coupon_id;
}
$post = get_post( $coupon_id );
$this->update_additional_fields_for_object( $post, $request );
/**
* Fires after a single item is created or updated via the REST API.
*
* @param object $post Inserted object (not a WP_Post object).
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $post, $request );
return rest_ensure_response( $response );
} catch ( Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
/**
* Saves a coupon to the database.
*/
public function save_coupon( $request ) {
try {
$coupon = $this->prepare_item_for_database( $request );
if ( is_wp_error( $coupon ) ) {
return $coupon;
}
$coupon->save();
return $coupon->get_id();
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
/**
* Get the Coupon's schema, conforming to JSON Schema.
*
@ -401,6 +169,11 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'amount' => array(
'description' => __( 'The amount of discount.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'date_created' => array(
'description' => __( "The date the coupon was created, in the site's timezone.", 'woocommerce' ),
'type' => 'date-time',
@ -413,11 +186,6 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'description' => array(
'description' => __( 'Coupon description.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'discount_type' => array(
'description' => __( 'Determines the type of discount that will be applied.', 'woocommerce' ),
'type' => 'string',
@ -425,12 +193,12 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
'enum' => array_keys( wc_get_coupon_types() ),
'context' => array( 'view', 'edit' ),
),
'amount' => array(
'description' => __( 'The amount of discount.', 'woocommerce' ),
'description' => array(
'description' => __( 'Coupon description.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'expiry_date' => array(
'date_expires' => array(
'description' => __( 'UTC DateTime when the coupon expires.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
@ -455,7 +223,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
),
'context' => array( 'view', 'edit' ),
),
'exclude_product_ids' => array(
'excluded_product_ids' => array(
'description' => __( "List of product ID's the coupon cannot be used on.", 'woocommerce' ),
'type' => 'array',
'items' => array(
@ -563,20 +331,4 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['code'] = array(
'description' => __( 'Limit result set to resources with a specific code.', 'woocommerce' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}

View File

@ -7,7 +7,7 @@
* @author WooThemes
* @category API
* @package WooCommerce/API
* @since 2.6.0
* @since 2.7.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -126,16 +126,15 @@ class WC_REST_Coupons_V1_Controller extends WC_REST_Posts_Controller {
/**
* Query args.
*
* @param array $args
* @param WP_REST_Request $request
* @param array $args Query args
* @param WP_REST_Request $request Request data.
* @return array
*/
public function query_args( $args, $request ) {
global $wpdb;
if ( ! empty( $request['code'] ) ) {
$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $request['code'] ) );
$id = wc_get_coupon_id_by_code( $request['code'] );
$args['post__in'] = array( $id );
}
@ -150,47 +149,59 @@ class WC_REST_Coupons_V1_Controller extends WC_REST_Posts_Controller {
* @return WP_REST_Response $data
*/
public function prepare_item_for_response( $post, $request ) {
global $wpdb;
$coupon = new WC_Coupon( (int) $post->ID );
$_data = $coupon->get_data();
// 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'", $post->ID ) );
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
$format_date = array( 'date_created', 'date_modified', 'date_expires' );
$format_null = array( 'usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items' );
$coupon = new WC_Coupon( $code );
// Format decimal values.
foreach ( $format_decimal as $key ) {
$_data[ $key ] = wc_format_decimal( $_data[ $key ], 2 );
}
// Format date values.
foreach ( $format_date as $key ) {
$_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $_data[ $key ] ) ) ) : null;
}
// Format null values.
foreach ( $format_null as $key ) {
$_data[ $key ] = $_data[ $key ] ? $_data[ $key ] : null;
}
$data = array(
'id' => $coupon->id,
'code' => $coupon->code,
'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt ),
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt ),
'discount_type' => $coupon->type,
'description' => $post->post_excerpt,
'amount' => wc_format_decimal( $coupon->coupon_amount, 2 ),
'expiry_date' => $coupon->expiry_date ? wc_rest_prepare_date_response( date( 'Y-m-d', $coupon->expiry_date ) ) : null,
'usage_count' => (int) $coupon->usage_count,
'individual_use' => ( 'yes' === $coupon->individual_use ),
'product_ids' => array_map( 'absint', (array) $coupon->product_ids ),
'exclude_product_ids' => array_map( 'absint', (array) $coupon->exclude_product_ids ),
'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,
'free_shipping' => $coupon->enable_free_shipping(),
'product_categories' => array_map( 'absint', (array) $coupon->product_categories ),
'excluded_product_categories' => array_map( 'absint', (array) $coupon->exclude_product_categories ),
'exclude_sale_items' => $coupon->exclude_sale_items(),
'minimum_amount' => wc_format_decimal( $coupon->minimum_amount, 2 ),
'maximum_amount' => wc_format_decimal( $coupon->maximum_amount, 2 ),
'email_restrictions' => $coupon->customer_email,
'used_by' => $coupon->get_used_by(),
'id' => $_data['id'],
'code' => $_data['code'],
'date_created' => $_data['date_created'],
'date_modified' => $_data['date_modified'],
'discount_type' => $_data['discount_type'],
'description' => $_data['description'],
'amount' => $_data['amount'],
'expiry_date' => $_data['date_expires'],
'usage_count' => $_data['usage_count'],
'individual_use' => $_data['individual_use'],
'product_ids' => $_data['product_ids'],
'exclude_product_ids' => $_data['excluded_product_ids'],
'usage_limit' => $_data['usage_limit'],
'usage_limit_per_user' => $_data['usage_limit_per_user'],
'limit_usage_to_x_items' => $_data['limit_usage_to_x_items'],
'free_shipping' => $_data['free_shipping'],
'product_categories' => $_data['product_categories'],
'excluded_product_categories' => $_data['excluded_product_categories'],
'exclude_sale_items' => $_data['exclude_sale_items'],
'minimum_amount' => $_data['minimum_amount'],
'maximum_amount' => $_data['maximum_amount'],
'email_restrictions' => $_data['email_restrictions'],
'used_by' => $_data['used_by'],
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $post ) );
$response->add_links( $this->prepare_links( $post, $request ) );
/**
* Filter the data for a response.
@ -205,6 +216,15 @@ class WC_REST_Coupons_V1_Controller extends WC_REST_Posts_Controller {
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
}
/**
* Only reutrn writeable props from schema.
* @param array $schema
* @return bool
*/
protected function filter_writable_props( $schema ) {
return empty( $schema['readonly'] );
}
/**
* Prepare a single coupon for create or update.
*
@ -214,218 +234,167 @@ class WC_REST_Coupons_V1_Controller extends WC_REST_Posts_Controller {
protected function prepare_item_for_database( $request ) {
global $wpdb;
$data = new stdClass;
$id = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
$coupon = new WC_Coupon( $id );
$schema = $this->get_item_schema();
$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
// ID.
if ( isset( $request['id'] ) ) {
$data->ID = absint( $request['id'] );
// Update to schema to make compatible with CRUD schema.
if ( $request['exclude_product_ids'] ) {
$request['excluded_product_ids'] = $request['exclude_product_ids'];
}
if ( $request['expiry_date'] ) {
$request['date_expires'] = $request['expiry_date'];
}
$schema = $this->get_item_schema();
// Validate required POST fields.
if ( 'POST' === $request->get_method() && empty( $data->ID ) ) {
if ( 'POST' === $request->get_method() && 0 === $coupon->get_id() ) {
if ( empty( $request['code'] ) ) {
return new WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce' ), 'code' ), array( 'status' => 400 ) );
}
}
// Code.
if ( ! empty( $schema['properties']['code'] ) && ! empty( $request['code'] ) ) {
$coupon_code = apply_filters( 'woocommerce_coupon_code', $request['code'] );
$id = isset( $data->ID ) ? $data->ID : 0;
// Handle all writable props.
foreach ( $data_keys as $key ) {
$value = $request[ $key ];
// 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'
AND $wpdb->posts.ID != %s
", $coupon_code, $id ) );
if ( ! is_null( $value ) ) {
switch ( $key ) {
case 'code' :
$coupon_code = apply_filters( 'woocommerce_coupon_code', $value );
$id = $coupon->get_id() ? $coupon->get_id() : 0;
$id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );
if ( $coupon_found ) {
return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
if ( $id_from_code ) {
return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
}
$coupon->set_code( $coupon_code );
break;
case 'description' :
$coupon->set_description( wp_filter_post_kses( $value ) );
break;
default :
if ( is_callable( array( $coupon, "set_{$key}" ) ) ) {
$coupon->{"set_{$key}"}( $value );
}
break;
}
}
$data->post_title = $coupon_code;
}
// Content.
$data->post_content = '';
// Coupon description (excerpt).
if ( ! empty( $schema['properties']['description'] ) && isset( $request['description'] ) ) {
$data->post_excerpt = wp_filter_post_kses( $request['description'] );
}
// Post type.
$data->post_type = $this->post_type;
// Post status.
$data->post_status = 'publish';
// Comment status.
$data->comment_status = 'closed';
// Ping status.
$data->ping_status = 'closed';
/**
* Filter the query_vars used in `get_items` for the constructed query.
*
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
* prepared for insertion.
*
* @param stdClass $data An object representing a single item prepared
* for inserting or updating the database.
* @param WC_Coupon $coupon The coupon object.
* @param WP_REST_Request $request Request object.
*/
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request );
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request );
}
/**
* Expiry date format.
* Create a single item.
*
* @param string $expiry_date
* @return string
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
protected function get_coupon_expiry_date( $expiry_date ) {
if ( '' !== $expiry_date ) {
return date( 'Y-m-d', strtotime( $expiry_date ) );
public function create_item( $request ) {
if ( ! empty( $request['id'] ) ) {
/* translators: %s: post type */
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
}
return '';
$coupon_id = $this->save_coupon( $request );
if ( is_wp_error( $coupon_id ) ) {
return $coupon_id;
}
$post = get_post( $coupon_id );
$this->update_additional_fields_for_object( $post, $request );
$this->add_post_meta_fields( $post, $request );
/**
* Fires after a single item is created or updated via the REST API.
*
* @param object $post Inserted object (not a WP_Post object).
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $post, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
return $response;
}
/**
* Add post meta fields.
* Update a single coupon.
*
* @param WP_Post $post
* @param WP_REST_Request $request
* @return bool|WP_Error
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
protected function add_post_meta_fields( $post, $request ) {
$data = array_filter( $request->get_params() );
public function update_item( $request ) {
try {
$post_id = (int) $request['id'];
$defaults = array(
'discount_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' => '',
'free_shipping' => false,
'product_categories' => array(),
'excluded_product_categories' => array(),
'exclude_sale_items' => false,
'minimum_amount' => '',
'maximum_amount' => '',
'email_restrictions' => array(),
'description' => ''
);
if ( empty( $post_id ) || get_post_type( $post_id ) !== $this->post_type ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
}
$data = wp_parse_args( $data, $defaults );
$coupon_id = $this->save_coupon( $request );
if ( is_wp_error( $coupon_id ) ) {
return $coupon_id;
}
// Set coupon meta.
update_post_meta( $post->ID, 'discount_type', $data['discount_type'] );
update_post_meta( $post->ID, 'coupon_amount', wc_format_decimal( $data['amount'] ) );
update_post_meta( $post->ID, 'individual_use', ( true === $data['individual_use'] ) ? 'yes' : 'no' );
update_post_meta( $post->ID, 'product_ids', implode( ',', array_filter( array_map( 'intval', $data['product_ids'] ) ) ) );
update_post_meta( $post->ID, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $data['exclude_product_ids'] ) ) ) );
update_post_meta( $post->ID, 'usage_limit', absint( $data['usage_limit'] ) );
update_post_meta( $post->ID, 'usage_limit_per_user', absint( $data['usage_limit_per_user'] ) );
update_post_meta( $post->ID, 'limit_usage_to_x_items', absint( $data['limit_usage_to_x_items'] ) );
update_post_meta( $post->ID, 'usage_count', absint( $data['usage_count'] ) );
update_post_meta( $post->ID, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ) ) );
update_post_meta( $post->ID, 'free_shipping', ( true === $data['free_shipping'] ) ? 'yes' : 'no' );
update_post_meta( $post->ID, 'product_categories', array_filter( array_map( 'intval', $data['product_categories'] ) ) );
update_post_meta( $post->ID, 'exclude_product_categories', array_filter( array_map( 'intval', $data['excluded_product_categories'] ) ) );
update_post_meta( $post->ID, 'exclude_sale_items', ( true === $data['exclude_sale_items'] ) ? 'yes' : 'no' );
update_post_meta( $post->ID, 'minimum_amount', wc_format_decimal( $data['minimum_amount'] ) );
update_post_meta( $post->ID, 'maximum_amount', wc_format_decimal( $data['maximum_amount'] ) );
update_post_meta( $post->ID, 'customer_email', array_filter( array_map( 'sanitize_email', $data['email_restrictions'] ) ) );
$post = get_post( $coupon_id );
$this->update_additional_fields_for_object( $post, $request );
return true;
/**
* Fires after a single item is created or updated via the REST API.
*
* @param object $post Inserted object (not a WP_Post object).
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $post, $request );
return rest_ensure_response( $response );
} catch ( Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
/**
* Update post meta fields.
* Saves a coupon to the database.
*
* @param WP_Post $post
* @param WP_REST_Request $request
* @return bool|WP_Error
* @since 2.7.0
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|int
*/
protected function update_post_meta_fields( $post, $request ) {
if ( isset( $request['amount'] ) ) {
update_post_meta( $post->ID, 'coupon_amount', wc_format_decimal( $request['amount'] ) );
}
protected function save_coupon( $request ) {
try {
$coupon = $this->prepare_item_for_database( $request );
if ( isset( $request['individual_use'] ) ) {
update_post_meta( $post->ID, 'individual_use', ( true === $request['individual_use'] ) ? 'yes' : 'no' );
}
if ( is_wp_error( $coupon ) ) {
return $coupon;
}
if ( isset( $request['product_ids'] ) ) {
update_post_meta( $post->ID, 'product_ids', implode( ',', array_filter( array_map( 'intval', $request['product_ids'] ) ) ) );
$coupon->save();
return $coupon->get_id();
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
if ( isset( $request['exclude_product_ids'] ) ) {
update_post_meta( $post->ID, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $request['exclude_product_ids'] ) ) ) );
}
if ( isset( $request['usage_limit'] ) ) {
update_post_meta( $post->ID, 'usage_limit', absint( $request['usage_limit'] ) );
}
if ( isset( $request['usage_limit_per_user'] ) ) {
update_post_meta( $post->ID, 'usage_limit_per_user', absint( $request['usage_limit_per_user'] ) );
}
if ( isset( $request['limit_usage_to_x_items'] ) ) {
update_post_meta( $post->ID, 'limit_usage_to_x_items', absint( $request['limit_usage_to_x_items'] ) );
}
if ( isset( $request['usage_count'] ) ) {
update_post_meta( $post->ID, 'usage_count', absint( $request['usage_count'] ) );
}
if ( isset( $request['expiry_date'] ) ) {
update_post_meta( $post->ID, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $request['expiry_date'] ) ) );
}
if ( isset( $request['free_shipping'] ) ) {
update_post_meta( $post->ID, 'free_shipping', ( true === $request['free_shipping'] ) ? 'yes' : 'no' );
}
if ( isset( $request['product_categories'] ) ) {
update_post_meta( $post->ID, 'product_categories', array_filter( array_map( 'intval', $request['product_categories'] ) ) );
}
if ( isset( $request['excluded_product_categories'] ) ) {
update_post_meta( $post->ID, 'exclude_product_categories', array_filter( array_map( 'intval', $request['excluded_product_categories'] ) ) );
}
if ( isset( $request['exclude_sale_items'] ) ) {
update_post_meta( $post->ID, 'exclude_sale_items', ( true === $request['exclude_sale_items'] ) ? 'yes' : 'no' );
}
if ( isset( $request['minimum_amount'] ) ) {
update_post_meta( $post->ID, 'minimum_amount', wc_format_decimal( $request['minimum_amount'] ) );
}
if ( isset( $request['maximum_amount'] ) ) {
update_post_meta( $post->ID, 'maximum_amount', wc_format_decimal( $request['maximum_amount'] ) );
}
if ( isset( $request['email_restrictions'] ) ) {
update_post_meta( $post->ID, 'customer_email', array_filter( array_map( 'sanitize_email', $request['email_restrictions'] ) ) );
}
return true;
}
/**
@ -434,7 +403,6 @@ class WC_REST_Coupons_V1_Controller extends WC_REST_Posts_Controller {
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => $this->post_type,