当所有变体的库存水平为 0 时,以编程方式复制 WooCommerce 产品

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

当我商店中的任何产品达到 0 库存时(在其所有变体中),我需要它执行以下操作:

  1. 将相关产品保存为草稿。
  2. 将此产品复制到新产品。
  3. 将新产品中的每个变体的库存水平设置为 1。
  4. 将新产品的名称更改为与旧产品同名(不附加“(复制)”)。
  5. 发布新产品。

我在 functions.php 中有我的代码,它正在努力复制产品。我遇到麻烦的地方是获取新产品的 ID、设置其名称,然后将其变体设置为每个都有 1 个库存。如有任何帮助,我们将不胜感激!

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

product_duplicate()函数returns一个WC_Product对象。因此,您可以通过 get_id() 函数访问产品 ID,如下所示:

$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

要循环遍历这个新创建的产品的变体并分配数量,您需要先使用 get_children() 获取变体 ID,然后循环遍历结果,如下所示:

$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();
}