WC_Post_Data::handle_global_attribute_updated
Handles updates to a global attribute by triggering variation summary regeneration.
Method of the class: WC_Post_Data{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = WC_Post_Data::handle_global_attribute_updated( $attribute_id, $attribute, $old_slug );
- $attribute_id(int) (required)
- Attribute ID.
- $attribute(string) (required)
- Attribute name.
- $old_slug(string) (required)
- Old attribute slug.
Changelog
| Since 10.2.0 | Introduced. |
WC_Post_Data::handle_global_attribute_updated() WC Post Data::handle global attribute updated code WC 10.5.0
public static function handle_global_attribute_updated( $attribute_id, $attribute, $old_slug ) {
// We use this trigger for both updates and deletions of global attributes.
// They pass different parameters to $old_slug - deleted attributes include the "pa_" prefix, while updated attributes do not.
// Remove it if existing for consistency.
if ( strpos( $old_slug, 'pa_' ) === 0 ) {
$old_slug = substr( $old_slug, 3 );
}
$taxonomy = 'pa_' . $old_slug;
$threshold = self::get_variation_summaries_sync_threshold();
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
$args = array(
'post_type' => 'product_variation',
'post_status' => 'any',
'posts_per_page' => $threshold + 1,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'attribute_' . $taxonomy,
'compare' => 'EXISTS',
),
),
);
// phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
$variation_ids = get_posts( $args );
if ( empty( $variation_ids ) ) {
return;
}
if ( count( $variation_ids ) <= $threshold ) {
// Update variation summaries that used this product attribute, but
// wait until shutdown. This will allow WooC to carry out post_meta migrations
// if the slug of the attribute changed.
add_action(
'shutdown',
function () use ( $variation_ids ) {
self::regenerate_variation_summaries( $variation_ids );
}
);
} else {
$new_slug = ! empty( $attribute['attribute_name'] ) ? $attribute['attribute_name'] : $old_slug;
$new_taxonomy = 'pa_' . $new_slug;
self::schedule_variation_summary_regeneration(
'wc_regenerate_attribute_variation_summaries',
array( $new_taxonomy ),
'Taxonomy: ' . $taxonomy . ', Attribute ID: ' . $attribute_id
);
}
}