php - 当所有变体的库存水平为 0 时,以编程方式复制 WooCommerce 产品
问题描述
当我商店中的任何产品库存达到 0 时(在所有变体中),我需要它执行以下操作:
- 将有问题的产品保存为草稿。
- 将此产品复制到新产品。
- 将新产品中的每个变体设置为库存水平为 1。
- 将新产品的名称更改为与旧产品的名称相同(不附加“ (Copy)”)。
- 发布新产品。
我在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()
函数返回一个 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();
}
推荐阅读
- scala - Flink StreamingFileSink RowFormatBuilder with BucketAssigner 返回 Any?
- java - th 中的值:嵌套在 th 中的文本:thymeleaf 中的操作
- kubernetes - 节点资源不足:[DiskPressure]。但 df -h 仅显示 47% 的使用率
- laravel-6 - 数据表警告:表 id=laravel 数据表 - 异常消息未定义索引 yajra
- vue.js - 消除图标按钮上的波纹效果
- laravel - Laravel 6.x Auth 脚手架路由“verification.resend”不去 VerificationController@resend?
- javascript - 将搜索按钮添加到用于将 XML 数据获取到 HTML 表的函数中
- c++ - 如何可靠地测量笔记本电脑上 C++ 应用程序的性能
- python - SQLAlchemy 查询 API 和计算字段
- r - R:根据数据框列中的行值删除行