Tuesday, January 10, 2023
HomeWordPress Developmentwoocommerce offtopic - replace all merchandise with similar SKU prefixes when...

woocommerce offtopic – replace all merchandise with similar SKU prefixes when updating inventory with CSV?


On my WooCommerce retailer, there are a number of instances the place I present a similar variable product with totally different names and descriptions (for website positioning causes). As WooCommerce does not enable to make use of a similar SKU for a number of merchandise, I add a suffix “_2”, “_3”, and so on to the duplicated merchandise. For instance if the primary variable product has a SKU “12345678” for a variation, the duplicated product can have a SKU “12345678_2” for a similar variation.
I additionally set a customized subject ‘tri_produits’ with the identical worth for all duplicated merchandise to search out them simply.

Now I wish to replace the inventory and worth of all duplicated merchandise after I replace the value from a CSV file (utilizing WooCommerce Merchandise -> Import button). So I’ve written the beneath code:

operate update_imported_product($object, $knowledge)
{

    // Replace variable merchandise with similar SKU + suffix.

    if ($object->get_type() === 'variation'){
        $product_id = wp_get_post_parent_id($object->get_id());
    }
    else
    {
        $product_id = $object->get_id();    
    }
    
    // On the lookout for different merchandise with similar worth for customized subject tri_produits (these are the duplicated merchandise)

    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',
                        'worth' => $tri_produits,
                    )
                )
            );                  
            // Get the merchandise
            $merchandise = get_posts( $args);
            if(!empty($merchandise))
            {
                
                if(rely($merchandise) > 1)
                {

                    foreach ( $merchandise as $product ) 
                    {

                        // Examine if that is one other product
                        if($product->ID != $product_id)
                        {
                            // Loop on variations and examine if similar 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.'-'))
                                    {
                                            
                                        // Replace inventory
                                        $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 surely does not work. It finds the duplicate merchandise appropriately however when the code edits and saves them, the merchandise do not change. What ought to I modify in my code?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments