Created
April 7, 2021 18:32
-
-
Save jonshipman/c252eec5ab2c706951af259a134af94c to your computer and use it in GitHub Desktop.
Woocommerce Product Attribute Dedupe
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 | |
/** | |
* Product Attribute DeDupe | |
* | |
* @package Divide | |
*/ | |
/** | |
* Product Attributes in Woocommerce are only checked in this tables for "register_taxonomy" loop. | |
* Deleting duplicates in this table does not require reassigning IDs. Terms are related to taxonomies by name only. | |
* | |
* @return void | |
*/ | |
function shipman_product_attribute_dedupe() { | |
global $wpdb; | |
if ( ! is_admin() ) { | |
$_a = $wpdb->get_col( | |
' | |
SELECT | |
attribute_name | |
FROM | |
wp_woocommerce_attribute_taxonomies | |
GROUP BY attribute_name | |
HAVING COUNT(attribute_name) > 1; | |
' | |
); | |
foreach ( $_a as $attribute_name ) { | |
$attributes = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM `wp_woocommerce_attribute_taxonomies` WHERE `attribute_name` = %s', $attribute_name ) ); | |
$first = null; | |
if ( ! empty( $attributes ) ) { | |
foreach ( $attributes as $attr ) { | |
if ( null === $first ) { | |
$first = $attr; | |
continue; | |
} else { | |
$wpdb->delete( 'wp_woocommerce_attribute_taxonomies', array( 'attribute_id' => $attr->attribute_id ) ); | |
} | |
} | |
} | |
} | |
delete_transient( 'wc_attribute_taxonomies' ); | |
die; | |
} | |
} | |
add_action( 'init', 'shipman_product_attribute_dedupe', 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment