/** * Test that properties can still be accessed directly for backwards * compat sake. They throw a deprecated notice. * @since 2.7.0 */ public function test_coupon_backwards_compat_props_use_correct_getters() { // Accessing properties directly will throw some wanted deprected notices // So we need to let PHPUnit know we are expecting them and it's fine to continue $legacy_keys = array('id', 'exists', 'coupon_custom_fields', 'type', 'discount_type', 'amount', 'code', 'individual_use', 'product_ids', 'exclude_product_ids', 'usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items', 'usage_count', 'expiry_date', 'product_categories', 'exclude_product_categories', 'minimum_amount', 'maximum_amount', 'customer_email'); $this->expected_doing_it_wrong = array_merge($this->expected_doing_it_wrong, $legacy_keys); $coupon = WC_Helper_Coupon::create_coupon(); $coupon->add_meta_data('test_coupon_field', 'testing', true); $coupon->save_meta_data(); $coupon = new WC_Coupon($coupon->get_id()); $this->assertEquals($coupon->get_id(), $coupon->id); $this->assertEquals($coupon->get_id() > 0 ? true : false, $coupon->exists); $coupon_cf = $coupon->get_meta('test_coupon_field'); $this->assertEquals($coupon_cf, $coupon->coupon_custom_fields['test_coupon_field'][0]); $this->assertEquals($coupon->get_discount_type(), $coupon->type); $this->assertEquals($coupon->get_discount_type(), $coupon->discount_type); $this->assertEquals($coupon->get_amount(), $coupon->amount); $this->assertEquals($coupon->get_code(), $coupon->code); $this->assertEquals($coupon->get_individual_use(), 'yes' === $coupon->individual_use ? true : false); $this->assertEquals($coupon->get_product_ids(), $coupon->product_ids); $this->assertEquals($coupon->get_excluded_product_ids(), $coupon->exclude_product_ids); $this->assertEquals($coupon->get_usage_limit(), $coupon->usage_limit); $this->assertEquals($coupon->get_usage_limit_per_user(), $coupon->usage_limit_per_user); $this->assertEquals($coupon->get_limit_usage_to_x_items(), $coupon->limit_usage_to_x_items); $this->assertEquals($coupon->get_usage_count(), $coupon->usage_count); $this->assertEquals($coupon->get_date_expires(), $coupon->expiry_date); $this->assertEquals($coupon->get_product_categories(), $coupon->product_categories); $this->assertEquals($coupon->get_excluded_product_categories(), $coupon->exclude_product_categories); $this->assertEquals($coupon->get_minimum_amount(), $coupon->minimum_amount); $this->assertEquals($coupon->get_maximum_amount(), $coupon->maximum_amount); $this->assertEquals($coupon->get_email_restrictions(), $coupon->customer_email); }
/** * 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; } $coupon = new WC_Coupon($id); if (0 === $coupon->get_id()) { throw new WC_API_Exception('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), 404); } $coupon_data = array('id' => $coupon->get_id(), 'code' => $coupon->get_code(), 'type' => $coupon->get_discount_type(), 'created_at' => $this->server->format_datetime($coupon->get_date_created(), false, true), 'updated_at' => $this->server->format_datetime($coupon->get_date_modified(), false, true), '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(), false, true), '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)); }
/** * Ensure valid coupon data response. * @since 2.7.0 * @param array $response * @param WC_Coupon $coupon */ protected function check_get_coupon_response($response, $coupon) { $this->assertEquals((int) $coupon->get_id(), $response['id']); $this->assertEquals($coupon->get_code(), $response['code']); $this->assertEquals($coupon->get_discount_type(), $response['type']); $this->assertEquals($coupon->get_amount(), $response['amount']); $this->assertEquals($coupon->get_individual_use(), $response['individual_use']); $this->assertEquals($coupon->get_product_ids(), $response['product_ids']); $this->assertEquals($coupon->get_excluded_product_ids(), $response['exclude_product_ids']); $this->assertEquals((int) $coupon->get_usage_limit(), $response['usage_limit']); $this->assertEquals((int) $coupon->get_usage_limit_per_user(), $response['usage_limit_per_user']); $this->assertEquals((int) $coupon->get_limit_usage_to_x_items(), $response['limit_usage_to_x_items']); $this->assertEquals((int) $coupon->get_usage_count(), $response['usage_count']); $this->assertEquals($coupon->get_expiry_date(), $response['expiry_date']); $this->assertEquals($coupon->get_free_shipping(), $response['enable_free_shipping']); $this->assertEquals($coupon->get_product_categories(), $response['product_category_ids']); $this->assertEquals($coupon->get_excluded_product_categories(), $response['exclude_product_category_ids']); $this->assertEquals($coupon->get_exclude_sale_items(), $response['exclude_sale_items']); $this->assertEquals(wc_format_decimal($coupon->get_minimum_amount(), 2), $response['minimum_amount']); $this->assertEquals(wc_format_decimal($coupon->get_maximum_amount(), 2), $response['maximum_amount']); $this->assertEquals($coupon->get_email_restrictions(), $response['customer_emails']); $this->assertEquals($coupon->get_description(), $response['description']); $this->assertArrayHasKey('created_at', $response); $this->assertArrayHasKey('updated_at', $response); }
/** * 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) { global $wpdb; $id = $this->validate_request($id, 'shop_coupon', 'read'); if (is_wp_error($id)) { return $id; } // 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)); if (is_null($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_expiry_date()), '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 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 = 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'))); // 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'])) { return new WP_Error('woocommerce_rest_empty_coupon_code', sprintf(__('The coupon code cannot be empty.', 'woocommerce'), 'code'), array('status' => 400)); } } // Handle all writable props foreach ($data_keys as $key) { $value = $request[$key]; 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 ($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 'meta_data': if (is_array($value)) { foreach ($value as $meta) { $coupon->update_meta_data($meta['key'], $meta['value'], $meta['id']); } } 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; } } } /** * 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 WC_Coupon $coupon The coupon object. * @param WP_REST_Request $request Request object. */ return apply_filters("woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request); }
/** * 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) { try { $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)) { throw new WC_API_Exception('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), 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' => $coupon->get_date_expires() ? $this->server->format_datetime($coupon->get_date_expires()) : null, '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), 'maximum_amount' => wc_format_decimal($coupon->get_maximum_amount(), 2), 'customer_emails' => $coupon->get_email_restrictions(), 'description' => $coupon_post->post_excerpt); 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())); } }
/** * Test coupon saving. * @since 2.7.0 */ function test_coupon_save() { $coupon = WC_Helper_Coupon::create_coupon(); $coupon_id = $coupon->get_id(); $coupon->set_code('dummycoupon2'); $coupon->save(); $coupon->read($coupon_id); // Read from DB to retest $this->assertEquals('dummycoupon2', $coupon->get_code()); $this->assertEquals($coupon_id, $coupon->get_id()); $new_coupon = new WC_Coupon(); $new_coupon->set_code('dummycoupon3'); $new_coupon->save(); $new_coupon_id = $new_coupon->get_id(); $this->assertEquals('dummycoupon3', $new_coupon->get_code()); $this->assertNotEquals(0, $new_coupon_id); }
/** * 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 = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE id = %d AND post_type = 'shop_coupon' AND post_status = 'publish'", $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); }