From 16b9f22706f447328f786c6c2c54d8726d1bbade Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Wed, 7 Oct 2015 17:02:06 -0300 Subject: [PATCH] [API] Created method to return tax classes --- includes/api/class-wc-api-resource.php | 2 +- includes/api/class-wc-api-taxes.php | 28 +++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/includes/api/class-wc-api-resource.php b/includes/api/class-wc-api-resource.php index cc85fc11ad5..018821c5130 100644 --- a/includes/api/class-wc-api-resource.php +++ b/includes/api/class-wc-api-resource.php @@ -43,7 +43,7 @@ class WC_API_Resource { $response_names = array( 'order', 'coupon', 'customer', 'product', 'report', 'customer_orders', 'customer_downloads', 'order_note', 'order_refund', - 'product_reviews', 'product_category' + 'product_reviews', 'product_category', 'tax_class' ); foreach ( $response_names as $name ) { diff --git a/includes/api/class-wc-api-taxes.php b/includes/api/class-wc-api-taxes.php index 70b38102f83..56d01edc76b 100644 --- a/includes/api/class-wc-api-taxes.php +++ b/includes/api/class-wc-api-taxes.php @@ -34,7 +34,7 @@ class WC_API_Taxes extends WC_API_Resource { # GET/POST /taxes $routes[ $this->base ] = array( - array( array( $this, 'get_taxes' ), WC_API_Server::READABLE ), + array( array( $this, 'get_tax_classes' ), WC_API_Server::READABLE ), array( array( $this, 'create_tax' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ), ); @@ -59,18 +59,36 @@ class WC_API_Taxes extends WC_API_Resource { } /** - * Get all taxes + * Get all tax classes * * @since 2.5.0 * * @param string $fields - * @param array $filter - * @param int $page * * @return array */ - public function get_taxes( $fields = null, $filter = array(), $page = 1 ) { + public function get_tax_classes( $fields = null ) { + try { + // Permissions check + if ( ! current_user_can( 'manage_woocommerce' ) ) { + throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_tax_classes', __( 'You do not have permission to read tax classes', 'woocommerce' ), 401 ); + } + $tax_classes = array(); + + $classes = WC_Tax::get_tax_classes(); + + foreach ( $classes as $class ) { + $tax_classes[] = apply_filters( 'woocommerce_api_tax_class_response', array( + 'id' => sanitize_title( $class ), + 'name' => $class + ), $class, $fields, $this ); + } + + return array( 'tax_classes' => apply_filters( 'woocommerce_api_tax_classes_response', $tax_classes, $classes, $fields, $this ) ); + } catch ( WC_API_Exception $e ) { + return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } } /**