/** * Test wc_get_coupon_id_by_code(). * * @since 2.7.0 */ public function test_wc_get_coupon_id_by_code() { // Create coupon. $code = 'testcoupon'; $coupon = WC_Helper_Coupon::create_coupon($code); $this->assertEquals($coupon->get_id(), wc_get_coupon_id_by_code($coupon->get_code())); // Delete coupon. WC_Helper_Coupon::delete_coupon($coupon->get_id()); $this->assertEmpty(wc_get_coupon_id_by_code(0)); }
/** * Coupon constructor. Loads coupon data. * @param mixed $data Coupon data, object, ID or code. */ public function __construct($data = '') { parent::__construct($data); if ($data instanceof WC_Coupon) { $this->read(absint($data->get_id())); } elseif ($coupon = apply_filters('woocommerce_get_shop_coupon_data', false, $data)) { _doing_it_wrong('woocommerce_get_shop_coupon_data', 'Reading a manual coupon via woocommerce_get_shop_coupon_data has been deprecated. Please sent an instance of WC_Coupon instead.', '2.7'); $this->read_manual_coupon($data, $coupon); } elseif (is_numeric($data) && 'shop_coupon' === get_post_type($data)) { $this->read($data); } elseif (!empty($data)) { $this->read(wc_get_coupon_id_by_code($data)); $this->set_code($data); } }
/** * Save meta box data. * * @param int $post_id * @param WP_Post $post */ public static function save($post_id, $post) { global $wpdb; // Check for dupe coupons $coupon_code = apply_filters('woocommerce_coupon_code', $post->post_title); $id_from_code = wc_get_coupon_id_by_code($coupon_code, $post_id); if ($id_from_code) { WC_Admin_Meta_Boxes::add_error(__('Coupon code already exists - customers will use the latest coupon with this code.', 'woocommerce')); } $coupon = new WC_Coupon($post_id); $coupon->set_props(array('code' => $post->post_title, 'discount_type' => wc_clean($_POST['discount_type']), 'amount' => wc_format_decimal($_POST['coupon_amount']), 'date_expires' => wc_clean($_POST['expiry_date']), 'individual_use' => isset($_POST['individual_use']), 'product_ids' => array_filter(array_map('intval', explode(',', $_POST['product_ids']))), 'excluded_product_ids' => array_filter(array_map('intval', explode(',', $_POST['exclude_product_ids']))), 'usage_limit' => absint($_POST['usage_limit']), 'usage_limit_per_user' => absint($_POST['usage_limit_per_user']), 'limit_usage_to_x_items' => absint($_POST['limit_usage_to_x_items']), 'free_shipping' => isset($_POST['free_shipping']), 'product_categories' => array_filter(array_map('intval', (array) $_POST['product_categories'])), 'excluded_product_categories' => array_filter(array_map('intval', (array) $_POST['exclude_product_categories'])), 'exclude_sale_items' => isset($_POST['exclude_sale_items']), 'minimum_amount' => wc_format_decimal($_POST['minimum_amount']), 'maximum_amount' => wc_format_decimal($_POST['maximum_amount']), 'email_restrictions' => array_filter(array_map('trim', explode(',', wc_clean($_POST['customer_email'])))))); $coupon->save(); do_action('woocommerce_coupon_options_save', $post_id); }
/** * Edit a coupon * * @since 2.2 * @param int $id the coupon ID * @param array $data * @return array */ public function edit_coupon($id, $data) { try { 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']; $id = $this->validate_request($id, 'shop_coupon', 'edit'); if (is_wp_error($id)) { return $id; } $data = apply_filters('woocommerce_api_edit_coupon_data', $data, $id, $this); if (isset($data['code'])) { global $wpdb; $coupon_code = apply_filters('woocommerce_coupon_code', $data['code']); $id_from_code = wc_get_coupon_id_by_code($coupon_code, $id); if ($id_from_code) { throw new WC_API_Exception('woocommerce_api_coupon_code_already_exists', __('The coupon code already exists', 'woocommerce'), 400); } $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) { throw new WC_API_Exception('woocommerce_api_cannot_update_coupon', __('Failed to update coupon', 'woocommerce'), 400); } } 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']); } if (isset($data['amount'])) { update_post_meta($id, 'coupon_amount', wc_format_decimal($data['amount'])); } if (isset($data['individual_use'])) { update_post_meta($id, 'individual_use', true === $data['individual_use'] ? 'yes' : 'no'); } if (isset($data['product_ids'])) { update_post_meta($id, 'product_ids', implode(',', array_filter(array_map('intval', $data['product_ids'])))); } if (isset($data['exclude_product_ids'])) { update_post_meta($id, 'exclude_product_ids', implode(',', array_filter(array_map('intval', $data['exclude_product_ids'])))); } if (isset($data['usage_limit'])) { update_post_meta($id, 'usage_limit', absint($data['usage_limit'])); } if (isset($data['usage_limit_per_user'])) { update_post_meta($id, 'usage_limit_per_user', absint($data['usage_limit_per_user'])); } if (isset($data['limit_usage_to_x_items'])) { update_post_meta($id, 'limit_usage_to_x_items', absint($data['limit_usage_to_x_items'])); } if (isset($data['usage_count'])) { update_post_meta($id, 'usage_count', absint($data['usage_count'])); } if (isset($data['expiry_date'])) { update_post_meta($id, 'expiry_date', $this->get_coupon_expiry_date(wc_clean($data['expiry_date']))); } if (isset($data['enable_free_shipping'])) { update_post_meta($id, 'free_shipping', true === $data['enable_free_shipping'] ? 'yes' : 'no'); } if (isset($data['product_category_ids'])) { update_post_meta($id, 'product_categories', array_filter(array_map('intval', $data['product_category_ids']))); } if (isset($data['exclude_product_category_ids'])) { update_post_meta($id, 'exclude_product_categories', array_filter(array_map('intval', $data['exclude_product_category_ids']))); } if (isset($data['exclude_sale_items'])) { update_post_meta($id, 'exclude_sale_items', true === $data['exclude_sale_items'] ? 'yes' : 'no'); } if (isset($data['minimum_amount'])) { update_post_meta($id, 'minimum_amount', wc_format_decimal($data['minimum_amount'])); } if (isset($data['maximum_amount'])) { update_post_meta($id, 'maximum_amount', wc_format_decimal($data['maximum_amount'])); } if (isset($data['customer_emails'])) { update_post_meta($id, 'customer_email', array_filter(array_map('sanitize_email', $data['customer_emails']))); } do_action('woocommerce_api_edit_coupon', $id, $data); return $this->get_coupon($id); } catch (WC_API_Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * 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); }