115 lines
4.1 KiB
PHP
115 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* REST API Coupons controller
|
|
*
|
|
* Handles requests to the /coupons endpoint.
|
|
*
|
|
* @author WooThemes
|
|
* @category API
|
|
* @package WooCommerce/API
|
|
* @since 2.6.0
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* REST API Coupons controller class.
|
|
*
|
|
* @package WooCommerce/API
|
|
* @extends WC_REST_Posts_Controller
|
|
*/
|
|
class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
|
|
|
/**
|
|
* Route base.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $rest_base = 'coupons';
|
|
|
|
/**
|
|
* Post type.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $post_type = 'shop_coupon';
|
|
|
|
/**
|
|
* Register the routes for coupons.
|
|
*/
|
|
public function register_routes() {
|
|
register_rest_route( WC_API::REST_API_NAMESPACE, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
|
|
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' ) ),
|
|
),
|
|
),
|
|
) );
|
|
}
|
|
|
|
/**
|
|
* Prepare a single coupon output for response.
|
|
*
|
|
* @param WP_Post $post Post object.
|
|
* @param WP_REST_Request $request Request object.
|
|
* @return WP_REST_Response $data
|
|
*/
|
|
public function prepare_item_for_response( $post, $request ) {
|
|
global $wpdb;
|
|
|
|
// 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 ) );
|
|
|
|
$coupon = new WC_Coupon( $code );
|
|
|
|
$data = array(
|
|
'id' => $coupon->id,
|
|
'code' => $coupon->code,
|
|
'type' => $coupon->type,
|
|
'created_at' => $this->prepare_date_response( $post->post_date_gmt ),
|
|
'updated_at' => $this->prepare_date_response( $post->post_modified_gmt ),
|
|
'amount' => wc_format_decimal( $coupon->coupon_amount, 2 ),
|
|
'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,
|
|
'usage_count' => (int) $coupon->usage_count,
|
|
'expiry_date' => ( ! empty( $coupon->expiry_date ) ) ? $this->prepare_date_response( $coupon->expiry_date ) : null,
|
|
'enable_free_shipping' => $coupon->enable_free_shipping(),
|
|
'product_category_ids' => array_map( 'absint', (array) $coupon->product_categories ),
|
|
'exclude_product_category_ids' => 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 ),
|
|
'customer_emails' => $coupon->customer_email,
|
|
'description' => $post->post_excerpt,
|
|
);
|
|
|
|
$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.
|
|
$response = rest_ensure_response( $data );
|
|
|
|
/**
|
|
* Filter the data for a response.
|
|
*
|
|
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
|
* prepared for the response.
|
|
*
|
|
* @param WP_REST_Response $response The response object.
|
|
* @param WP_Post $post Post object.
|
|
* @param WP_REST_Request $request Request object.
|
|
*/
|
|
return apply_filters( 'woocommerce_rest_api_prepare_' . $this->post_type, $response, $post, $request );
|
|
}
|
|
}
|