wc_delete_attribute()
Delete attribute by ID.
Hooks from the function
Returns
true|false.
Usage
wc_delete_attribute( $id );
- $id(int) (required)
- Attribute ID.
Changelog
| Since 3.2.0 | Introduced. |
wc_delete_attribute() wc delete attribute code WC 10.5.0
function wc_delete_attribute( $id ) {
global $wpdb;
$name = $wpdb->get_var(
$wpdb->prepare(
"
SELECT attribute_name
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
WHERE attribute_id = %d
",
$id
)
);
$taxonomy = wc_attribute_taxonomy_name( $name );
/**
* Before deleting an attribute.
*
* @param int $id Attribute ID.
* @param string $name Attribute name.
* @param string $taxonomy Attribute taxonomy name.
*/
do_action( 'woocommerce_before_attribute_delete', $id, $name, $taxonomy );
if ( $name && $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $id ) ) ) {
if ( taxonomy_exists( $taxonomy ) ) {
$terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' );
foreach ( $terms as $term ) {
wp_delete_term( $term->term_id, $taxonomy );
}
}
/**
* After deleting an attribute.
*
* @param int $id Attribute ID.
* @param string $name Attribute name.
* @param string $taxonomy Attribute taxonomy name.
*/
do_action( 'woocommerce_attribute_deleted', $id, $name, $taxonomy );
wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
delete_transient( 'wc_attribute_taxonomies' );
WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
return true;
}
return false;
}