This how I finally made it to update duplicated product variations with ‘-2’ SKU suffix:
function update_imported_product($object, $data)
{
if ($object->get_type() === 'variation'){
$product_id = wp_get_post_parent_id($object->get_id());
}
else
{
$product_id = $object->get_id();
}
if(isset($product_id))
{
$sku_in = get_post_meta($product_id, '_sku', true);
// Search and update variations with suffix -2
$args = array(
'post_type' => 'product_variation',
'meta_query' => array(
array(
'key' => '_sku',
'value' => $sku_in.'-2',
)));
$variations = get_posts( $args);
if(!empty($variations))
{
if(count($variations) > 0)
{
foreach ( $variations as $variation )
{
// Get input product data
if ($object->get_type() === 'variation'){
$pr=new WC_Product_Variation($object->get_id());
}
else{
$pr=new WC_Product($object->get_id());
}
// Update variation
$product_variation = new WC_Product_Variation($variation);
$product_variation->set_stock_quantity($pr->get_stock_quantity());
$product_variation->set_weight($pr->get_weight());
$product_variation->set_regular_price($pr->get_regular_price());
$product_variation->save();
}
}
}
}
return $object;
}
add_action( 'woocommerce_product_import_inserted_product_object', 'update_imported_product', 10, 2 );