WC_API_Taxes

The WooCommerce WC API Taxes class.

Defined (1)

The class is defined in the following location(s).

/includes/api/legacy/v3/class-wc-api-taxes.php  
  1. class WC_API_Taxes extends WC_API_Resource { 
  2.  
  3. /** @var string $base the route base */ 
  4. protected $base = '/taxes'; 
  5.  
  6. /** 
  7. * Register the routes for this class 
  8. * GET /taxes 
  9. * GET /taxes/count 
  10. * GET /taxes/<id> 
  11. * @since 2.1 
  12. * @param array $routes 
  13. * @return array 
  14. */ 
  15. public function register_routes( $routes ) { 
  16.  
  17. # GET/POST /taxes 
  18. $routes[ $this->base ] = array( 
  19. array( array( $this, 'get_taxes' ), WC_API_Server::READABLE ),  
  20. array( array( $this, 'create_tax' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  21. ); 
  22.  
  23. # GET /taxes/count 
  24. $routes[ $this->base . '/count' ] = array( 
  25. array( array( $this, 'get_taxes_count' ), WC_API_Server::READABLE ),  
  26. ); 
  27.  
  28. # GET/PUT/DELETE /taxes/<id> 
  29. $routes[ $this->base . '/(?P<id>\d+)' ] = array( 
  30. array( array( $this, 'get_tax' ), WC_API_Server::READABLE ),  
  31. array( array( $this, 'edit_tax' ), WC_API_SERVER::EDITABLE | WC_API_SERVER::ACCEPT_DATA ),  
  32. array( array( $this, 'delete_tax' ), WC_API_SERVER::DELETABLE ),  
  33. ); 
  34.  
  35. # GET/POST /taxes/classes 
  36. $routes[ $this->base . '/classes' ] = array( 
  37. array( array( $this, 'get_tax_classes' ), WC_API_Server::READABLE ),  
  38. array( array( $this, 'create_tax_class' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  39. ); 
  40.  
  41. # GET /taxes/classes/count 
  42. $routes[ $this->base . '/classes/count' ] = array( 
  43. array( array( $this, 'get_tax_classes_count' ), WC_API_Server::READABLE ),  
  44. ); 
  45.  
  46. # GET /taxes/classes/<slug> 
  47. $routes[ $this->base . '/classes/(?P<slug>\w[\w\s\-]*)' ] = array( 
  48. array( array( $this, 'delete_tax_class' ), WC_API_SERVER::DELETABLE ),  
  49. ); 
  50.  
  51. # POST|PUT /taxes/bulk 
  52. $routes[ $this->base . '/bulk' ] = array( 
  53. array( array( $this, 'bulk' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  54. ); 
  55.  
  56. return $routes; 
  57.  
  58. /** 
  59. * Get all taxes 
  60. * @since 2.5.0 
  61. * @param string $fields 
  62. * @param array $filter 
  63. * @param string $class 
  64. * @param int $page 
  65. * @return array 
  66. */ 
  67. public function get_taxes( $fields = null, $filter = array(), $class = null, $page = 1 ) { 
  68. if ( ! empty( $class ) ) { 
  69. $filter['tax_rate_class'] = $class; 
  70.  
  71. $filter['page'] = $page; 
  72.  
  73. $query = $this->query_tax_rates( $filter ); 
  74.  
  75. $taxes = array(); 
  76.  
  77. foreach ( $query['results'] as $tax ) { 
  78. $taxes[] = current( $this->get_tax( $tax->tax_rate_id, $fields ) ); 
  79.  
  80. // Set pagination headers 
  81. $this->server->add_pagination_headers( $query['headers'] ); 
  82.  
  83. return array( 'taxes' => $taxes ); 
  84.  
  85. /** 
  86. * Get the tax for the given ID 
  87. * @since 2.5.0 
  88. * @param int $id The tax ID 
  89. * @param string $fields fields to include in response 
  90. * @return array|WP_Error 
  91. */ 
  92. public function get_tax( $id, $fields = null ) { 
  93. global $wpdb; 
  94.  
  95. try { 
  96. $id = absint( $id ); 
  97.  
  98. // Permissions check 
  99. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  100. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_tax', __( 'You do not have permission to read tax rate', 'woocommerce' ), 401 ); 
  101.  
  102. // Get tax rate details 
  103. $tax = WC_Tax::_get_tax_rate( $id ); 
  104.  
  105. if ( is_wp_error( $tax ) || empty( $tax ) ) { 
  106. throw new WC_API_Exception( 'woocommerce_api_invalid_tax_id', __( 'A tax rate with the provided ID could not be found', 'woocommerce' ), 404 ); 
  107.  
  108. $tax_data = array( 
  109. 'id' => (int) $tax['tax_rate_id'],  
  110. 'country' => $tax['tax_rate_country'],  
  111. 'state' => $tax['tax_rate_state'],  
  112. 'postcode' => '',  
  113. 'city' => '',  
  114. 'rate' => $tax['tax_rate'],  
  115. 'name' => $tax['tax_rate_name'],  
  116. 'priority' => (int) $tax['tax_rate_priority'],  
  117. 'compound' => (bool) $tax['tax_rate_compound'],  
  118. 'shipping' => (bool) $tax['tax_rate_shipping'],  
  119. 'order' => (int) $tax['tax_rate_order'],  
  120. 'class' => $tax['tax_rate_class'] ? $tax['tax_rate_class'] : 'standard',  
  121. ); 
  122.  
  123. // Get locales from a tax rate 
  124. $locales = $wpdb->get_results( $wpdb->prepare( " 
  125. SELECT location_code, location_type 
  126. FROM {$wpdb->prefix}woocommerce_tax_rate_locations 
  127. WHERE tax_rate_id = %d 
  128. ", $id ) ); 
  129.  
  130. if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) { 
  131. foreach ( $locales as $locale ) { 
  132. $tax_data[ $locale->location_type ] = $locale->location_code; 
  133.  
  134. return array( 'tax' => apply_filters( 'woocommerce_api_tax_response', $tax_data, $tax, $fields, $this ) ); 
  135. } catch ( WC_API_Exception $e ) { 
  136. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  137.  
  138. /** 
  139. * Create a tax 
  140. * @since 2.5.0 
  141. * @param array $data 
  142. * @return array 
  143. */ 
  144. public function create_tax( $data ) { 
  145. try { 
  146. if ( ! isset( $data['tax'] ) ) { 
  147. throw new WC_API_Exception( 'woocommerce_api_missing_tax_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'tax' ), 400 ); 
  148.  
  149. // Check permissions 
  150. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  151. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_tax', __( 'You do not have permission to create tax rates', 'woocommerce' ), 401 ); 
  152.  
  153. $data = apply_filters( 'woocommerce_api_create_tax_data', $data['tax'], $this ); 
  154.  
  155. $tax_data = array( 
  156. 'tax_rate_country' => '',  
  157. 'tax_rate_state' => '',  
  158. 'tax_rate' => '',  
  159. 'tax_rate_name' => '',  
  160. 'tax_rate_priority' => 1,  
  161. 'tax_rate_compound' => 0,  
  162. 'tax_rate_shipping' => 1,  
  163. 'tax_rate_order' => 0,  
  164. 'tax_rate_class' => '',  
  165. ); 
  166.  
  167. foreach ( $tax_data as $key => $value ) { 
  168. $new_key = str_replace( 'tax_rate_', '', $key ); 
  169. $new_key = 'tax_rate' === $new_key ? 'rate' : $new_key; 
  170.  
  171. if ( isset( $data[ $new_key ] ) ) { 
  172. if ( in_array( $new_key, array( 'compound', 'shipping' ) ) ) { 
  173. $tax_data[ $key ] = $data[ $new_key ] ? 1 : 0; 
  174. } else { 
  175. $tax_data[ $key ] = $data[ $new_key ]; 
  176.  
  177. // Create tax rate 
  178. $id = WC_Tax::_insert_tax_rate( $tax_data ); 
  179.  
  180. // Add locales 
  181. if ( ! empty( $data['postcode'] ) ) { 
  182. WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $data['postcode'] ) ); 
  183.  
  184. if ( ! empty( $data['city'] ) ) { 
  185. WC_Tax::_update_tax_rate_cities( $id, wc_clean( $data['city'] ) ); 
  186.  
  187. do_action( 'woocommerce_api_create_tax', $id, $data ); 
  188.  
  189. $this->server->send_status( 201 ); 
  190.  
  191. return $this->get_tax( $id ); 
  192. } catch ( WC_API_Exception $e ) { 
  193. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  194.  
  195. /** 
  196. * Edit a tax 
  197. * @since 2.5.0 
  198. * @param int $id The tax ID 
  199. * @param array $data 
  200. * @return array 
  201. */ 
  202. public function edit_tax( $id, $data ) { 
  203. try { 
  204. if ( ! isset( $data['tax'] ) ) { 
  205. throw new WC_API_Exception( 'woocommerce_api_missing_tax_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'tax' ), 400 ); 
  206.  
  207. // Check permissions 
  208. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  209. throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_tax', __( 'You do not have permission to edit tax rates', 'woocommerce' ), 401 ); 
  210.  
  211. $data = $data['tax']; 
  212.  
  213. // Get current tax rate data 
  214. $tax = $this->get_tax( $id ); 
  215.  
  216. if ( is_wp_error( $tax ) ) { 
  217. $error_data = $tax->get_error_data(); 
  218. throw new WC_API_Exception( $tax->get_error_code(), $tax->get_error_message(), $error_data['status'] ); 
  219.  
  220. $current_data = $tax['tax']; 
  221. $data = apply_filters( 'woocommerce_api_edit_tax_data', $data, $this ); 
  222. $tax_data = array(); 
  223. $default_fields = array( 
  224. 'tax_rate_country',  
  225. 'tax_rate_state',  
  226. 'tax_rate',  
  227. 'tax_rate_name',  
  228. 'tax_rate_priority',  
  229. 'tax_rate_compound',  
  230. 'tax_rate_shipping',  
  231. 'tax_rate_order',  
  232. 'tax_rate_class',  
  233. ); 
  234.  
  235. foreach ( $data as $key => $value ) { 
  236. $new_key = 'rate' === $key ? 'tax_rate' : 'tax_rate_' . $key; 
  237.  
  238. // Check if the key is valid 
  239. if ( ! in_array( $new_key, $default_fields ) ) { 
  240. continue; 
  241.  
  242. // Test new data against current data 
  243. if ( $value === $current_data[ $key ] ) { 
  244. continue; 
  245.  
  246. // Fix compund and shipping values 
  247. if ( in_array( $key, array( 'compound', 'shipping' ) ) ) { 
  248. $value = $value ? 1 : 0; 
  249.  
  250. $tax_data[ $new_key ] = $value; 
  251.  
  252. // Update tax rate 
  253. WC_Tax::_update_tax_rate( $id, $tax_data ); 
  254.  
  255. // Update locales 
  256. if ( ! empty( $data['postcode'] ) && $current_data['postcode'] != $data['postcode'] ) { 
  257. WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $data['postcode'] ) ); 
  258.  
  259. if ( ! empty( $data['city'] ) && $current_data['city'] != $data['city'] ) { 
  260. WC_Tax::_update_tax_rate_cities( $id, wc_clean( $data['city'] ) ); 
  261.  
  262. do_action( 'woocommerce_api_edit_tax_rate', $id, $data ); 
  263.  
  264. return $this->get_tax( $id ); 
  265. } catch ( WC_API_Exception $e ) { 
  266. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  267.  
  268. /** 
  269. * Delete a tax 
  270. * @since 2.5.0 
  271. * @param int $id The tax ID 
  272. * @return array 
  273. */ 
  274. public function delete_tax( $id ) { 
  275. global $wpdb; 
  276.  
  277. try { 
  278. // Check permissions 
  279. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  280. throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_tax', __( 'You do not have permission to delete tax rates', 'woocommerce' ), 401 ); 
  281.  
  282. $id = absint( $id ); 
  283.  
  284. WC_Tax::_delete_tax_rate( $id ); 
  285.  
  286. if ( 0 === $wpdb->rows_affected ) { 
  287. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_tax', __( 'Could not delete the tax rate', 'woocommerce' ), 401 ); 
  288.  
  289. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'tax' ) ); 
  290. } catch ( WC_API_Exception $e ) { 
  291. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  292.  
  293. /** 
  294. * Get the total number of taxes 
  295. * @since 2.5.0 
  296. * @param string $class 
  297. * @param array $filter 
  298. * @return array 
  299. */ 
  300. public function get_taxes_count( $class = null, $filter = array() ) { 
  301. try { 
  302. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  303. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_taxes_count', __( 'You do not have permission to read the taxes count', 'woocommerce' ), 401 ); 
  304.  
  305. if ( ! empty( $class ) ) { 
  306. $filter['tax_rate_class'] = $class; 
  307.  
  308. $query = $this->query_tax_rates( $filter, true ); 
  309.  
  310. return array( 'count' => (int) $query['headers']->total ); 
  311. } catch ( WC_API_Exception $e ) { 
  312. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  313.  
  314. /** 
  315. * Helper method to get tax rates objects 
  316. * @since 2.5.0 
  317. * @param array $args 
  318. * @param bool $count_only 
  319. * @return array 
  320. */ 
  321. protected function query_tax_rates( $args, $count_only = false ) { 
  322. global $wpdb; 
  323.  
  324. $results = ''; 
  325.  
  326. // Set args 
  327. $args = $this->merge_query_args( $args, array() ); 
  328.  
  329. $query = " 
  330. SELECT tax_rate_id 
  331. FROM {$wpdb->prefix}woocommerce_tax_rates 
  332. WHERE 1 = 1 
  333. "; 
  334.  
  335. // Filter by tax class 
  336. if ( ! empty( $args['tax_rate_class'] ) ) { 
  337. $tax_rate_class = 'standard' !== $args['tax_rate_class'] ? sanitize_title( $args['tax_rate_class'] ) : ''; 
  338. $query .= " AND tax_rate_class = '$tax_rate_class'"; 
  339.  
  340. // Order tax rates 
  341. $order_by = ' ORDER BY tax_rate_order'; 
  342.  
  343. // Pagination 
  344. $per_page = isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : get_option( 'posts_per_page' ); 
  345. $offset = 1 < $args['paged'] ? ( $args['paged'] - 1 ) * $per_page : 0; 
  346. $pagination = sprintf( ' LIMIT %d, %d', $offset, $per_page ); 
  347.  
  348. if ( ! $count_only ) { 
  349. $results = $wpdb->get_results( $query . $order_by . $pagination ); 
  350.  
  351. $wpdb->get_results( $query ); 
  352. $headers = new stdClass; 
  353. $headers->page = $args['paged']; 
  354. $headers->total = (int) $wpdb->num_rows; 
  355. $headers->is_single = $per_page > $headers->total; 
  356. $headers->total_pages = ceil( $headers->total / $per_page ); 
  357.  
  358. return array( 
  359. 'results' => $results,  
  360. 'headers' => $headers,  
  361. ); 
  362.  
  363. /** 
  364. * Bulk update or insert taxes 
  365. * Accepts an array with taxes in the formats supported by 
  366. * WC_API_Taxes->create_tax() and WC_API_Taxes->edit_tax() 
  367. * @since 2.5.0 
  368. * @param array $data 
  369. * @return array 
  370. */ 
  371. public function bulk( $data ) { 
  372. try { 
  373. if ( ! isset( $data['taxes'] ) ) { 
  374. throw new WC_API_Exception( 'woocommerce_api_missing_taxes_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'taxes' ), 400 ); 
  375.  
  376. $data = $data['taxes']; 
  377. $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'taxes' ); 
  378.  
  379. // Limit bulk operation 
  380. if ( count( $data ) > $limit ) { 
  381. throw new WC_API_Exception( 'woocommerce_api_taxes_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), 413 ); 
  382.  
  383. $taxes = array(); 
  384.  
  385. foreach ( $data as $_tax ) { 
  386. $tax_id = 0; 
  387.  
  388. // Try to get the tax rate ID 
  389. if ( isset( $_tax['id'] ) ) { 
  390. $tax_id = intval( $_tax['id'] ); 
  391.  
  392. if ( $tax_id ) { 
  393.  
  394. // Tax rate exists / edit tax rate 
  395. $edit = $this->edit_tax( $tax_id, array( 'tax' => $_tax ) ); 
  396.  
  397. if ( is_wp_error( $edit ) ) { 
  398. $taxes[] = array( 
  399. 'id' => $tax_id,  
  400. 'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),  
  401. ); 
  402. } else { 
  403. $taxes[] = $edit['tax']; 
  404. } else { 
  405.  
  406. // Tax rate don't exists / create tax rate 
  407. $new = $this->create_tax( array( 'tax' => $_tax ) ); 
  408.  
  409. if ( is_wp_error( $new ) ) { 
  410. $taxes[] = array( 
  411. 'id' => $tax_id,  
  412. 'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),  
  413. ); 
  414. } else { 
  415. $taxes[] = $new['tax']; 
  416.  
  417. return array( 'taxes' => apply_filters( 'woocommerce_api_taxes_bulk_response', $taxes, $this ) ); 
  418. } catch ( WC_API_Exception $e ) { 
  419. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  420.  
  421. /** 
  422. * Get all tax classes 
  423. * @since 2.5.0 
  424. * @param string $fields 
  425. * @return array 
  426. */ 
  427. public function get_tax_classes( $fields = null ) { 
  428. try { 
  429. // Permissions check 
  430. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  431. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_tax_classes', __( 'You do not have permission to read tax classes', 'woocommerce' ), 401 ); 
  432.  
  433. $tax_classes = array(); 
  434.  
  435. // Add standard class 
  436. $tax_classes[] = array( 
  437. 'slug' => 'standard',  
  438. 'name' => __( 'Standard rate', 'woocommerce' ),  
  439. ); 
  440.  
  441. $classes = WC_Tax::get_tax_classes(); 
  442.  
  443. foreach ( $classes as $class ) { 
  444. $tax_classes[] = apply_filters( 'woocommerce_api_tax_class_response', array( 
  445. 'slug' => sanitize_title( $class ),  
  446. 'name' => $class,  
  447. ), $class, $fields, $this ); 
  448.  
  449. return array( 'tax_classes' => apply_filters( 'woocommerce_api_tax_classes_response', $tax_classes, $classes, $fields, $this ) ); 
  450. } catch ( WC_API_Exception $e ) { 
  451. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  452.  
  453. /** 
  454. * Create a tax class. 
  455. * @since 2.5.0 
  456. * @param array $data 
  457. * @return array 
  458. */ 
  459. public function create_tax_class( $data ) { 
  460. try { 
  461. if ( ! isset( $data['tax_class'] ) ) { 
  462. throw new WC_API_Exception( 'woocommerce_api_missing_tax_class_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'tax_class' ), 400 ); 
  463.  
  464. // Check permissions 
  465. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  466. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_tax_class', __( 'You do not have permission to create tax classes', 'woocommerce' ), 401 ); 
  467.  
  468. $data = $data['tax_class']; 
  469.  
  470. if ( empty( $data['name'] ) ) { 
  471. throw new WC_API_Exception( 'woocommerce_api_missing_tax_class_name', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'name' ), 400 ); 
  472.  
  473. $name = sanitize_text_field( $data['name'] ); 
  474. $slug = sanitize_title( $name ); 
  475. $classes = WC_Tax::get_tax_classes(); 
  476. $exists = false; 
  477.  
  478. // Check if class exists. 
  479. foreach ( $classes as $key => $class ) { 
  480. if ( sanitize_title( $class ) === $slug ) { 
  481. $exists = true; 
  482. break; 
  483.  
  484. // Return error if tax class already exists. 
  485. if ( $exists ) { 
  486. throw new WC_API_Exception( 'woocommerce_api_cannot_create_tax_class', __( 'Tax class already exists', 'woocommerce' ), 401 ); 
  487.  
  488. // Add the new class. 
  489. $classes[] = $name; 
  490.  
  491. update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); 
  492.  
  493. do_action( 'woocommerce_api_create_tax_class', $slug, $data ); 
  494.  
  495. $this->server->send_status( 201 ); 
  496.  
  497. return array( 
  498. 'tax_class' => array( 
  499. 'slug' => $slug,  
  500. 'name' => $name,  
  501. ),  
  502. ); 
  503. } catch ( WC_API_Exception $e ) { 
  504. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  505.  
  506. /** 
  507. * Delete a tax class 
  508. * @since 2.5.0 
  509. * @param int $slug The tax class slug 
  510. * @return array 
  511. */ 
  512. public function delete_tax_class( $slug ) { 
  513. global $wpdb; 
  514.  
  515. try { 
  516. // Check permissions 
  517. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  518. throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_tax_class', __( 'You do not have permission to delete tax classes', 'woocommerce' ), 401 ); 
  519.  
  520. $slug = sanitize_title( $slug ); 
  521. $classes = WC_Tax::get_tax_classes(); 
  522. $deleted = false; 
  523.  
  524. foreach ( $classes as $key => $class ) { 
  525. if ( sanitize_title( $class ) === $slug ) { 
  526. unset( $classes[ $key ] ); 
  527. $deleted = true; 
  528. break; 
  529.  
  530. if ( ! $deleted ) { 
  531. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_tax_class', __( 'Could not delete the tax class', 'woocommerce' ), 401 ); 
  532.  
  533. update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); 
  534.  
  535. // Delete tax rate locations locations from the selected class. 
  536. $wpdb->query( $wpdb->prepare( " 
  537. DELETE locations.* 
  538. FROM {$wpdb->prefix}woocommerce_tax_rate_locations AS locations 
  539. INNER JOIN 
  540. {$wpdb->prefix}woocommerce_tax_rates AS rates 
  541. ON rates.tax_rate_id = locations.tax_rate_id 
  542. WHERE rates.tax_rate_class = '%s' 
  543. ", $slug ) ); 
  544.  
  545. // Delete tax rates in the selected class. 
  546. $wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => $slug ), array( '%s' ) ); 
  547.  
  548. return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'tax_class' ) ); 
  549. } catch ( WC_API_Exception $e ) { 
  550. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  551.  
  552. /** 
  553. * Get the total number of tax classes 
  554. * @since 2.5.0 
  555. * @return array 
  556. */ 
  557. public function get_tax_classes_count() { 
  558. try { 
  559. if ( ! current_user_can( 'manage_woocommerce' ) ) { 
  560. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_tax_classes_count', __( 'You do not have permission to read the tax classes count', 'woocommerce' ), 401 ); 
  561.  
  562. $total = count( WC_Tax::get_tax_classes() ) + 1; // +1 for Standard Rate 
  563.  
  564. return array( 'count' => $total ); 
  565. } catch ( WC_API_Exception $e ) { 
  566. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );