Home > Mobile >  Duplicate WooCommerce Product Programmatically when Stock Level of all Variations is 0
Duplicate WooCommerce Product Programmatically when Stock Level of all Variations is 0

Time:10-16

When any product in my store reaches 0 stock (across all its variations), I need it to do the following:

  1. Save the product in question as a draft.
  2. Duplicate this product to a new product.
  3. Set each variation in the new product to have a stock level of 1.
  4. Change the name of the new product to have the same name as the old product (without " (Copy)" being appended to it).
  5. Publish the new product.

I have my code in functions.php and it is working up to duplicating the product. Where I'm having trouble is grabbing the ID of the new product, setting its name, and then setting its variations to each have 1 in stock. Any assistance would be appreciated!

function order_checker($order_id) {
    // get the order
    $order = new WC_Order($order_id);
    
    // get the products from the order
    $all_products = $order->get_items();
    
    // loop through each product in the order
    foreach ($all_products as $product) {
        // get the product object
        $product_object = wc_get_product($product['product_id']);
        
        // if the product is a variable product
        if ($product_object->is_type('variable')) {
            // set "soldout" variable to true by default
            $soldout = true;
            
            // loop through the product variations and set "soldout" variable to false if they aren't all at 0 stock
            foreach ($product_object->get_available_variations() as $variation) {
                if ($variation['is_in_stock']) $soldout = false;
            }
            
            // if the product is sold out
            if ($soldout) :
                // save it as a draft
                wp_update_post(array(
                    'ID'            => $product['product_id'],
                    'post_status'   => 'draft'
                ));
            
                // duplicate it to a new product
                $duplicate_product = new WC_Admin_Duplicate_Product;
                $new_product = $duplicate_product -> product_duplicate($product_object);
                
                // grab the new product ID
                $new_product_id = $new_product->get_id();
            
                // grab the new product title
                $new_product_title = get_the_title($new_product_id);
            
                // remove " (Copy)" from the new product title
                $new_product_title = str_replace(' (Copy)', '', $new_product_title);
                
                // get an array of variation ids for the new product
                $new_variation_ids = $new_product->get_children();
            
                // loop through the variation ids
                foreach ($new_variation_ids as $new_variation_id) {
                    // get the variation object
                    $variation_object = new WC_Product_variation($new_variation_id);

                    // set the stock quantity to 1
                    $variation_object->set_stock_quantity(1);

                    // set the stock status to in stock
                    $variation_object->set_stock_status('instock');

                    // save and refresh cached data
                    $variation_object->save();
                }
                
                // set new product title and publish it
                wp_update_post(array(
                    'ID'            => $new_product_id,
                    'post_title'    => $new_product_title,
                    'post_status'   => 'publish'
                ));
            
                ?><script>console.log("This product has sold out!");</script><?php
            else : 
                ?><script>console.log("This product still has some options in stock!")</script><?php
            endif;
        }
    }
}
add_action('woocommerce_thankyou', 'order_checker', 10, 1);

CodePudding user response:

The product_duplicate() function returns a WC_Product object. So you can access the product id through get_id() function as given below:

$duplicate_product = new WC_Admin_Duplicate_Product;
$new_product = $duplicate_product -> product_duplicate($product_object);

$new_product_id = $new_product->get_id(); // (1) GRAB THE NEW PRODUCT ID

To loop through the variations of this newly created product and to assign quantity, you need to first get the variation ids using get_children() and then loop through the results as shown below:

$new_variation_ids = $new_product->get_children(); // get an array of variation ids for the newly created parent product.

foreach ($new_variation_ids as $new_variation_id) {

  $variation_object = new WC_Product_variation($new_variation_id);
        
        // Set the stock quantity
        $variation_object->set_stock_quantity(1);

        // Set the stock status
        $variation_object->set_stock_status('instock');

        // Save data (refresh cached data)
        $variation_object->save();
}

  • Related