예제 #1
0
 /**
  * Test wc_get_coupon_code_by_id().
  *
  * @since 2.7.0
  */
 public function test_wc_get_coupon_code_by_id()
 {
     // Create coupon.
     $code = 'testcoupon';
     $coupon = WC_Helper_Coupon::create_coupon($code);
     $this->assertEquals($code, wc_get_coupon_code_by_id($coupon->get_id()));
     // Delete coupon.
     WC_Helper_Coupon::delete_coupon($coupon->get_id());
     $this->assertEmpty(wc_get_coupon_code_by_id(0));
 }
예제 #2
0
 /**
  * Get the coupon for the given ID
  *
  * @since 2.1
  * @param int $id the coupon ID
  * @param string $fields fields to include in response
  * @return array|WP_Error
  */
 public function get_coupon($id, $fields = null)
 {
     $id = $this->validate_request($id, 'shop_coupon', 'read');
     if (is_wp_error($id)) {
         return $id;
     }
     // get the coupon code
     $code = wc_get_coupon_code_by_id($id);
     if (empty($code)) {
         return new WP_Error('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), array('status' => 404));
     }
     $coupon = new WC_Coupon($code);
     $coupon_post = get_post($coupon->get_id());
     $coupon_data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'type' => $coupon->get_discount_type(), 'created_at' => $this->server->format_datetime($coupon_post->post_date_gmt), 'updated_at' => $this->server->format_datetime($coupon_post->post_modified_gmt), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => array_map('absint', (array) $coupon->get_product_ids()), 'exclude_product_ids' => array_map('absint', (array) $coupon->get_excluded_product_ids()), '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, 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(), 'usage_count' => (int) $coupon->get_usage_count(), 'expiry_date' => $this->server->format_datetime($coupon->get_date_expires()), 'enable_free_shipping' => $coupon->get_free_shipping(), 'product_category_ids' => array_map('absint', (array) $coupon->get_product_categories()), 'exclude_product_category_ids' => array_map('absint', (array) $coupon->get_excluded_product_categories()), 'exclude_sale_items' => $coupon->get_exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->get_minimum_amount(), 2), 'customer_emails' => $coupon->get_email_restrictions());
     return array('coupon' => apply_filters('woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server));
 }
 /**
  * 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)
 {
     $code = wc_get_coupon_code_by_id($post->ID);
     $coupon = new WC_Coupon($code);
     $data = $coupon->get_data();
     $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');
     // 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;
     }
     $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));
     /**
      * 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_prepare_{$this->post_type}", $response, $post, $request);
 }
 /**
  * Prepare a single coupon for create or update.
  *
  * @param WP_REST_Request $request Request object.
  * @return WP_Error|stdClass $data Post object.
  */
 protected function prepare_item_for_database($request)
 {
     global $wpdb;
     // ID.
     if (isset($request['id'])) {
         $code = wc_get_coupon_code_by_id($request['id']);
         $coupon = new WC_Coupon($code);
     } else {
         $coupon = new WC_Coupon();
     }
     $schema = $this->get_item_schema();
     // Validate required POST fields.
     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 = $coupon->get_id() ? $coupon->get_id() : 0;
         // Check for duplicate coupon codes.
         $coupon_found = $wpdb->get_var($wpdb->prepare("\n\t\t\t\tSELECT {$wpdb->posts}.ID\n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\tWHERE {$wpdb->posts}.post_type = 'shop_coupon'\n\t\t\t\tAND {$wpdb->posts}.post_status = 'publish'\n\t\t\t\tAND {$wpdb->posts}.post_title = '%s'\n\t\t\t\tAND {$wpdb->posts}.ID != %s\n\t\t\t ", $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));
         }
         $coupon->set_code($coupon_code);
     }
     // Coupon description (excerpt).
     if (!empty($schema['properties']['description']) && isset($request['description'])) {
         $coupon->set_description(wp_filter_post_kses($request['description']));
     }
     /**
      * 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 WP_REST_Request $request       Request object.
      */
     return apply_filters("woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request);
 }