/** * Test coupon reading from the DB. * @since 2.7.0 */ function test_coupon_read() { $code = 'coupon-' . time(); $coupon = new WC_Coupon(); $coupon->set_code($code); $coupon->set_description('This is a test coupon.'); $coupon->set_usage_count(5); $coupon->create(); $coupon_id = $coupon->get_id(); $coupon_read = new WC_Coupon(); $coupon_read->read($coupon_id); $this->assertEquals(5, $coupon_read->get_usage_count()); $this->assertEquals($code, $coupon_read->get_code()); $this->assertEquals('This is a test coupon.', $coupon_read->get_description()); }
/** * Format posts from WP_Query result to items in which each item contain * common properties of item, for instance `post_title` will be `code`. * * @since 2.5.0 * @param array $posts Array of post * @return array Items */ protected function format_posts_to_items($posts) { $items = array(); foreach ($posts as $post) { $coupon = new WC_Coupon(); $coupon->read($post->ID); $coupon_usage_limit = $coupon->get_usage_limit(); $coupon_usage_limit_per_user = $coupon->get_usage_limit_per_user(); $coupon_date_expires = $coupon->get_date_expires(); $items[] = array('id' => $post->ID, 'code' => $post->post_title, 'type' => $coupon->get_discount_type(), 'created_at' => $this->format_datetime($post->post_date_gmt), 'updated_at' => $this->format_datetime($post->post_modified_gmt), 'amount' => wc_format_decimal($coupon->get_amount(), 2), 'individual_use' => $coupon->get_individual_use(), 'product_ids' => implode(', ', is_array($coupon->get_product_ids()) ? $coupon->get_product_ids() : array()), 'exclude_product_ids' => implode(', ', is_array($coupon->get_excluded_product_ids()) ? $coupon->get_excluded_product_ids() : array()), '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->get_limit_usage_to_x_items(), 'usage_count' => (int) $coupon->get_usage_count(), 'expiry_date' => !empty($coupon_date_expires) ? $this->format_datetime($coupon_date_expires) : null, 'free_shipping' => $coupon->get_free_shipping(), 'product_category_ids' => implode(', ', is_array($coupon->get_product_categories()) ? $coupon->get_product_categories() : array()), 'exclude_product_category_ids' => implode(', ', is_array($coupon->get_excluded_product_categories()) ? $coupon->get_excluded_product_categories() : array()), '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' => implode(', ', is_array($coupon->get_email_restrictions()) ? $coupon->get_email_restrictions() : array()), 'description' => $post->post_excerpt); } return $items; }