Skip to content

Instantly share code, notes, and snippets.

@luizbills
Last active October 27, 2022 19:59
Show Gist options
  • Save luizbills/86f63617fd826b815b132a49ee3ec2d8 to your computer and use it in GitHub Desktop.
Save luizbills/86f63617fd826b815b132a49ee3ec2d8 to your computer and use it in GitHub Desktop.
Copy custom terms when duplicatin a woocommerce product
<?php
// credits: https://github.com/woocommerce/woocommerce/issues/17487#issuecomment-565433819
add_action( 'woocommerce_product_duplicate', function ( $duplicate, $product ) {
$taxonomies = [ 'taxonomy1', 'taxonomy2' ];
foreach ( $taxonomies as $taxonomy ) {
$terms = get_the_terms( $product->get_id(), $taxonomy );
if ( ! is_wp_error( $terms ) ) {
wp_set_object_terms( $duplicate->get_id(), wp_list_pluck( $terms, 'term_id' ), $taxonomy );
}
}
}, 999, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment