WC_API_Products::delete_product_category()
Delete a product category.
Method of the class: WC_API_Products{}
Hooks from the method
Return
Array|WP_Error
. Success message if succeed, otherwise WP_Error will be returned
Usage
$WC_API_Products = new WC_API_Products(); $WC_API_Products->delete_product_category( $id );
- $id(int) (required)
- Product category term ID
Changelog
Since 2.5.0 | Introduced. |
WC_API_Products::delete_product_category() WC API Products::delete product category code WC 7.7.0
public function delete_product_category( $id ) { global $wpdb; try { // Check permissions if ( ! current_user_can( 'manage_product_terms' ) ) { throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_category', __( 'You do not have permission to delete product category', 'woocommerce' ), 401 ); } $id = absint( $id ); $deleted = wp_delete_term( $id, 'product_cat' ); if ( ! $deleted || is_wp_error( $deleted ) ) { throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_category', __( 'Could not delete the category', 'woocommerce' ), 401 ); } do_action( 'woocommerce_api_delete_product_category', $id, $this ); return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_category' ) ); } catch ( WC_API_Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } }