关于WooCommerce 3中的更新产品库存状态功能 [英] About update product stock status function in WooCommerce 3
问题描述
问题是,我必须修复自定义WooCommerce导入插件中的错误,该错误在将WC从2.6更新到3.4后出现.
The deal is, I have to fix an error in a custom WooCommerce import plugin, which appeared after updating WC from 2.6 to 3.4.
它使用"wc_update_product_stock_status"功能,并用于传递过帐(产品)ID和库存状态,因为它在数据库中表示("instock"和"outofstock",以字符串形式表示).但如今,正如我在WooCommerce文档中看到的那样( https://docs. woocommerce.com/wc-apidocs/function-wc_update_product_stock_status.html ),它接受整数而不是字符串.
It uses the 'wc_update_product_stock_status' function, and used to pass post (product) id and it's stock status as it is represented in DB ('instock' and 'outofstock', as a string). But nowadays, as I can see in the WooCommerce docs (https://docs.woocommerce.com/wc-apidocs/function-wc_update_product_stock_status.html) it accepts integer instead of string.
所以,问题是-存货价值缺货的那些整数是什么(1/0不适合).
So, the question is - what are those integers for in/out of stock values (1/0 did not fit).
推荐答案
If you look to the source code in wc_update_product_stock_status()
function:
/**
* Update a product's stock status.
*
* @param int $product_id
* @param int $status
*/
function wc_update_product_stock_status( $product_id, $status ) {
$product = wc_get_product( $product_id );
if ( $product ) {
$product->set_stock_status( $status );
$product->save();
}
}
它使用 WC_Product
set_stock_status()
Woocommerce 3 CRUD方法 使用字符串,但不是整数值:
/**
* Set stock status.
*
* @param string $status New status.
*/
public function set_stock_status( $status = 'instock' ) {
$valid_statuses = wc_get_product_stock_status_options();
if ( isset( $valid_statuses[ $status ] ) ) {
$this->set_prop( 'stock_status', $status );
} else {
$this->set_prop( 'stock_status', 'instock' );
}
}
So it's an error in the comment usage in
wc_update_product_stock_status()
function.
它仍然使用:'instock'
和'outofstock'
状态字符串.默认值为'instock'
…
It still uses: 'instock'
and 'outofstock'
status strings. the default value is 'instock'
…
主要区别还在于,库存状态现在作为自定义分类法
product_visibility
The main difference is also that stock status is now handled as
outofstock
term for the custom taxonomyproduct_visibility
在Woocommerce 3之前,库存状态被作为产品元数据处理.
Before Woocommerce 3, stock status was handled as product meta data.
这篇关于关于WooCommerce 3中的更新产品库存状态功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!