WC_API_Products::delete_product_attribute( int $id )
Delete a product attribute
Description Description
Parameters Parameters
- $id
-
(Required) the product attribute ID
Return Return
(array|WP_Error)
Source Source
File: includes/legacy/api/v2/class-wc-api-products.php
public function delete_product_attribute( $id ) { global $wpdb; try { // Check permissions if ( ! current_user_can( 'manage_product_terms' ) ) { throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_product_attribute', __( 'You do not have permission to delete product attributes', 'woocommerce' ), 401 ); } $id = absint( $id ); $attribute_name = $wpdb->get_var( $wpdb->prepare( " SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d ", $id ) ); if ( is_null( $attribute_name ) ) { throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); } $deleted = $wpdb->delete( $wpdb->prefix . 'woocommerce_attribute_taxonomies', array( 'attribute_id' => $id ), array( '%d' ) ); if ( false === $deleted ) { throw new WC_API_Exception( 'woocommerce_api_cannot_delete_product_attribute', __( 'Could not delete the attribute', 'woocommerce' ), 401 ); } $taxonomy = wc_attribute_taxonomy_name( $attribute_name ); if ( taxonomy_exists( $taxonomy ) ) { $terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' ); foreach ( $terms as $term ) { wp_delete_term( $term->term_id, $taxonomy ); } } do_action( 'woocommerce_attribute_deleted', $id, $attribute_name, $taxonomy ); do_action( 'woocommerce_api_delete_product_attribute', $id, $this ); // Clear transients delete_transient( 'wc_attribute_taxonomies' ); WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' ); return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'product_attribute' ) ); } catch ( WC_API_Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } }
Changelog Changelog
Version | Description |
---|---|
2.4.0 | Introduced. |