0

On my WooCommerce store, there are several cases where I show a same variable product with different names and descriptions (for SEO reasons). As WooCommerce doesn't allow to use a same SKU for several products, I add a suffix "_2", "_3", etc to the duplicated products. For example if the main variable product has a SKU "12345678" for a variation, the duplicated product will have a SKU "12345678_2" for the same variation. I also set a custom field 'tri_produits' with the same value for all duplicated products to find them easily.

Now I want to update the stock and price of all duplicated products when I update the price from a CSV file (using WooCommerce Products -> Import button). So I have written the below code:

function update_imported_product($object, $data)
{

    // Update variable products with same SKU + suffix.

    if ($object->get_type() === 'variation'){
        $product_id = wp_get_post_parent_id($object->get_id());
    }
    else
    {
        $product_id = $object->get_id();    
    }
    
    // Looking for other products with same value for custom field tri_produits (these are the duplicated products)

    if(isset($product_id))
    {

        $tri_produits = get_post_meta($product_id, 'tri_produits', true);
        $ugs = get_post_meta($product_id, '_sku', true); //$object->get_sku();

        if(isset($tri_produits))
        {

            $args = array(
                'post_type'  => 'product',
                'meta_query' => array(
                    array(
                        'key'   => 'tri_produits',
                        'value' => $tri_produits,
                    )
                )
            );                  
            // Get the products
            $products = get_posts( $args);
            if(!empty($products))
            {
                
                if(count($products) > 1)
                {

                    foreach ( $products as $product ) 
                    {

                        // Check if this is another product
                        if($product->ID != $product_id)
                        {
                            // Loop on variations and check if same sku prefix
                            $product_variations = get_posts( [
                                'post_type'       => 'product_variation',
                                'posts_per_page'  => -1,
                                'fields'          => 'ids',
                                'post_parent'     => $product->ID,
                            ] );

                            if ( !empty( $product_variations ) ) {

                                foreach ( $product_variations as $product_variation_id ) {
                                    $sku = get_post_meta( $product_variation_id, '_sku', true );
                            
                                    if(str_starts_with($sku,$ugs.'_') || str_starts_with($sku,$ugs.'-'))
                                    {
                                            
                                        // Update stock
                                        $product_variation = new WC_Product_Variation($product_variation_id);
                                        if ($object->get_type() === 'variation'){
                                            $pr=new WC_Product_Variation($object->get_id());
                                        }
                                        else{
                                            $pr=new WC_Product($object->get_id());
                                        }
                                        /*if($pr->get_stock_quantity() != $product_variation->get_stock_quantity())*/
                                        $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_filter('woocommerce_product_import_pre_insert_product_object', 'update_product_at_import', 10, 2);
add_action( 'woocommerce_product_import_inserted_product_object', 'update_imported_product', 10, 2 );

But it doesn't work. It finds the duplicate products correctly but when the code edits and saves them, the products don't change. What should I change in my code?

1
  • You'll have to check with WooCommerce support. Third-party plugin support—including WooCommerce—is off topic here.
    – Pat J
    Jan 10, 2023 at 22:21

1 Answer 1

0

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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.