wc_delete_attribute()
Delete attribute by ID.
Hooks from the function
Returns
bool.
Usage
wc_delete_attribute( $id );
- $id(int) (required)
- Attribute ID.
Changelog
| Since 3.2.0 | Introduced. |
wc_delete_attribute() wc delete attribute code WC 11.2.1
function wc_delete_attribute( $id ) {
global $wpdb, $wc_product_attributes;
$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 );
if ( taxonomy_exists( $taxonomy ) ) {
// When the caller defers term counting, any count queued for this taxonomy (by the
// deletions above or by the caller itself) resolves the taxonomy by name later, once
// it is gone. Flush while it still exists. WordPress drains its whole queue at once.
if ( wp_defer_term_counting() ) {
wp_update_term_count( array(), '', true );
}
unregister_taxonomy( $taxonomy );
}
unset( $wc_product_attributes[ $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;
}//end if
return false;
}