Created
December 7, 2016 16:01
-
-
Save yratof/f21242d4263c461f4a5b6b766cd24373 to your computer and use it in GitHub Desktop.
Woocommerce remove unused attributes on product variations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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 ); | |
} ); |
Thanks. I've managed to remove the redundant attributes via a Bulk Editor that does it in batches of 100. All done in around 15 minutes.
Yeah, if your server has a memory limit, trying to amend 20k products in one go might be a struggle haha
… On 21 Aug 2021, at 16:05, moded-mike ***@***.***> wrote:
***@***.*** commented on this gist.
Thanks. I've managed to remove the redundant attributes via a Bulk Editor that does it in batches of 100. All done in around 15 minutes.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It will be most likely your server running out, but you can stop this script after 10 products instead of -1