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

查看:124
本文介绍了在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. 保留为product_visibility自定义分类法的后一个术语名称outofstock
  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中有一个专用功能,用于从中获取产品ID您可以使用的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起,缺货"的库存状态也得到管理.自定义分类法 product_visibility,其术语名称为outofstock.因此,仅更新post meta是不够的.

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设置器和getters方法在woocommerce 3中引入.

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

因此,请尝试以下代码:

So try the following code instead:

// 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天全站免登陆