Eigentlich habe ich eine Woocommerce-Website mit ungefähr 800 Produkten. Und viele der Produkte sind variable Produkte und jedes Produkt hat einige Attribute, die als Variation verwendet werden, aber das Problem ist, dass vielen Produkten ungenutzte Attribute zugewiesen sind, die in diesen bestimmten Produktvariationen nicht verwendet werden.
Daher möchte ich ungenutzte Attribute von jedem Produkt entfernen, die in der Variation dieses bestimmten Produkts nicht verwendet werden.
Ich suche nach einer Abfrage oder einem Code-Snippet, das mir hilft, anstatt jedes einzelne Produkt zu überprüfen.
Im Moment habe ich keine Ahnung, wie ich das machen soll.
Ich möchte nur unbenutzte Attribute entfernen, die in diesen Produktvariationen nicht verwendet werden.
Versuchen Sie, dieses Skript auszuführen: https://gist.github.com/yratof/f21242d4263c461f4a5b6b766cd24373
add_action( 'init', function() {
ini_set( 'memory_limit', '2048M' );
set_time_limit( 0 );
$posts = get_posts( [
'post_type' => 'product',
'posts_per_page' => -1
] );
$count = 0;
foreach ( $posts as $post ) {
$product = get_product( $post );
if ( $product->product_type !== 'variable' ) {
continue;
}
$count ++;
$va = $product->get_variation_attributes();
$vas = [];
foreach ( $product->get_attributes() as $attribute ) {
if ( isset( $attribute['is_taxonomy'] ) && $attribute['is_taxonomy'] ) {
$terms = wp_get_post_terms( $product->id, $attribute['name'] ) ;
// var_dump( $terms );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $va[ $attribute['name'] ] ) ) {
// var_dump( $term );
if ( ! isset( $vas[$attribute['name']] ) ) {
$vas[$attribute['name']] = [];
}
$vas[$attribute['name']][] = $term->term_id;
}
}
}
}
foreach ($vas as $tax => $vals) {
wp_set_post_terms( $product->id, $vals, $tax );
}
}
wp_die( 'All attributes have been filtered: Total products changed: '. $count );
} );