在 Woocommerce 3 中以编程方式更新产品库存 [英] Updating product stock programmatically in Woocommerce 3

查看:25
本文介绍了在 Woocommerce 3 中以编程方式更新产品库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常需要帮助.我正在尝试以编程方式更新 woocommerce 产品库存数量.我们通过一些 JSON 向我们提供了供应商提要.我可以从提要中读取库存,并可以正确地从帖子元中提取数据.我正在使用最新版本的 WP 和 WOO.PHP 是 7.2

I need so help. I'm trying to update the woocommerce product stock quantity programmatically. We have a vendor feed to us through some JSON. I can read the stock from the feed and can pull the data from the post meta correctly. I'm using the latest version of WP and WOO. PHP is 7.2

以下是我从 SKU 中查找产品 ID 的方法.

Below is how I am finding the Product ID from the SKU.

$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );

这将返回正确的 ID,我可以用它来查看已经存在的当前元数据:

This is returning the correct ID and I can use it to see the current metadata that is already there:

$website_stock = get_post_meta($product_id, '_stock', true);
echo "Website Stock -  " . $website_stock . "</br>";
$website_stock_status = get_post_meta($product_id, '_stock_status', true);
echo "Website Stock Status -  " . $website_stock_status . "</br>";

然后我更新我从提要中获得的库存.这可以是从零到 x 或从 x 到零以及介于两者之间的任何库存.这是我更新缺货的方式:

I then update the stock I am getting from the feed. This can be stock going from zero to x or x to zero and anywhere in between. This is how I am updating the out of stock:

$out_of_stock_staus = 'outofstock';

update_post_meta($product_id, '_stock', 0);
update_post_meta($product_id, '_stock_status', wc_clean( $out_of_stock_staus ));
wc_delete_product_transients( $product_id ); // Clear/refresh the variation cache

这就是奇怪的地方.

更新的 sku

数据在管理面板的产品视图中正确显示.作为旁注,这个 SKU 可以属于一个变体(我们有很多),也可以是一个简单的产品.最后,他们似乎都更新好了.没有产生我可以看到的错误.

The data is showing correctly inside the product view in the admin panel. As a side note, this SKU can belong to a variation (we have tons of them) or it could be a simple product. In the end, they all seem to update ok. No errors are being generated that I can see.

我在我的functions.php 中使用了一小段PHP 代码段,它使下拉列表中的缺货项目变灰.这是:

I use a little PHP snippet in my functions.php that greys the out of stock items in the drop down. Here it is:

    /* Grey out out of stock items in the product dropdown */
add_filter( 'woocommerce_variation_is_active', 'grey_out_variations_when_out_of_stock', 10, 2 );

function grey_out_variations_when_out_of_stock( $grey_out, $variation ) {

   if ( ! $variation->is_in_stock() )
        return false;

    return true;
}

所以问题是:

  • 现在缺货的商品不应在下拉菜单中显示为可点击,但它仍然是.
  • 前端的库存并不总是说零,它让您选择一个然后说没有库存,所以添加到购物车按钮是活动的,不应该是.所以前端看不到更新.
  • 产品的 Woocommerce 管理面板不会将缺货情况汇总给父级,我必须进行快速编辑和更新才能做到这一点.
  • 基本上,后端会看到更改,但前端并未真正正确显示.

任何人都可以提供的任何帮助将不胜感激!

Any help that anyone can provide would be greatly appreciated!

谢谢

推荐答案

更新 2

由于 woocommerce 3缺货"产品状态保存在 2 个位置:

Since woocommerce 3 "outofstock" product status is saved in 2 locations:

  1. 作为 _stock_status 元键的发布元数据(和以前一样).
  2. 作为帖子术语名称 outofstock 剩下的 product_visibility 自定义分类法
  1. As post meta data for _stock_status meta key (just as before).
  2. As a post term name outofstock remaining to product_visibility custom taxonomy

这意味着您只错过了一步(第 3 步):

That means that you missed just a step (the step 3):

$out_of_stock_staus = 'outofstock';

// 1. Updating the stock quantity
update_post_meta($product_id, '_stock', 0);

// 2. Updating the stock quantity
update_post_meta( $product_id, '_stock_status', wc_clean( $out_of_stock_staus ) );

// 3. Updating post term relationship
wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true );

// And finally (optionally if needed)
wc_delete_product_transients( $product_id ); // Clear/refresh the variation cache

希望它适用于您的 cron 作业.

It hope that it will work with your cron job.

原答案

自 woocommerce 3 以来,您的代码有点过时,并且没有专门针对产品变体的库存状态设置...

Your code is a bit outdated since woocommerce 3 and there is no specifically a stock status setting for product variations...

woocommerce 中有一个专用函数可以从您可以使用的 sku:

There is a dedicated function in woocommerce to get the product Id from the sku that you could use:

wc_get_product_id_by_sku( $product_sku );

对于父变量产品,您应该不需要启用库存管理,因为这已在其每个产品变体中完成(因此在产品变体级别).

For the parent variable product, you should not need to enabled stock management as this is done in each of its product variations (so at the product variation level).

从 woocommerce 3 开始,outofstock"库存状态也被管理认为自定义分类 product_visibility 术语名称是 outofstock.所以更新帖子元是不够的.

Since woocommerce 3, the "outofstock" stock status is also managed thought a custom taxonomy product_visibility which term name is outofstock. So updating post meta is not enough.

也最好使用 新的 CRUD setter 和 getter 方法 与 woocommerce 3 一起引入.

Also is better to use the new CRUD setters and getters methods introduced with woocommerce 3.

所以试试下面的代码:

// get the product ID from the SKU
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );

// Get an instance of the WC_Product object
$product = new WC_Product( $product_id );

// Get product stock quantity and stock status
$stock_quantity = $product->get_stock_quantity();
$stock_status   = $product->get_stock_status();

// Display stock quantity and status
echo '<p>Product Stock quantity: ' . $stock_quantity . '</br>
    Product Stock status: ' . $stock_status . '</p></br>';

// Set product stock quantity (zero) and stock status (out of stock)
$product->set_stock_quantity();
$product->set_stock_status('outofstock');

// Save the data and refresh caches
$product->save();

经过测试并在正常环境下工作(但显然不是使用 cron 作业)

Tested and works in a normal context (but apparently not with a cron job)

这篇关于在 Woocommerce 3 中以编程方式更新产品库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆