WC_API_Coupons::get_coupon( int $id, string $fields = null )
Get the coupon for the given ID
Description Description
Parameters Parameters
- $id
-
(Required) the coupon ID
- $fields
-
(Optional) fields to include in response
Default value: null
Return Return
(array|WP_Error)
Source Source
File: includes/legacy/api/v2/class-wc-api-coupons.php
public function get_coupon( $id, $fields = null ) { try { $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() ? $coupon->get_date_created()->getTimestamp() : 0 ), // API gives UTC times. 'updated_at' => $this->server->format_datetime( $coupon->get_date_modified() ? $coupon->get_date_modified()->getTimestamp() : 0 ), // API gives UTC times. '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()->getTimestamp() ) : null, // API gives UTC times. '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->get_description(), ); 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() ) ); } }
Changelog Changelog
Version | Description |
---|---|
2.1 | Introduced. |