[英]WooCommerce Product get_stock_quantity() method return null
I have a problem when I try to retrieve the available quantity of a product in my view.当我尝试在我的视图中检索产品的可用数量时遇到问题。
This is my code:这是我的代码:
function wcs_custom_get_availability( $availability, $_product ) {
global $product;
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __( 'Plenty available in our store!', 'woocommerce');
}
// Change in Stock Text to only 1 or 2 left
if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 2 ) {
$availability['availability'] = sprintf( __('Only %s left in store!', 'woocommerce'), $product->get_stock_quantity() );
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sorry, All sold out!', 'woocommerce');
}
return $availability;
}
The result of the execution is as follows: Only left in store!执行结果如下: Only leave in store!
I have access to the product variable but the result is null but I would like to retrieve the quantity.我可以访问产品变量,但结果是 null,但我想检索数量。
Using WC_Product
method get_manage_stock()
with it, you will be able to avoid this problem.使用
WC_Product
方法get_manage_stock()
,您将能够避免这个问题。 I have also simplified a bit your code:我还简化了您的代码:
add_filter( 'woocommerce_get_availability', 'filter_wc_get_availability', 10, 2);
function filter_wc_get_availability( $availability, $product ) {
// In Stock
if ( $product->is_in_stock() ) {
$stock_quantity = $product->get_stock_quantity();
// Change Text for low stock (1 or 2 left)
if ( $product->get_manage_stock() && $stock_quantity == ( 1 || 2 ) ) {
$availability['availability'] = sprintf( __('Only %s left in store!', 'woocommerce'), $stock_quantity );
}
// Change in Stock Text (when more than 2)
else {
$availability['availability'] = __( 'Plenty available in our store!', 'woocommerce');
}
}
// Change Out of Stock Text
else {
$availability['availability'] = __('Sorry, All sold out!', 'woocommerce');
}
return $availability;
}
It should solve this problem.它应该解决这个问题。
Don't use
global $product;
不要使用
global $product;
as for variable products it will take the variable product object instead of the variations of this variable product.至于可变产品,它将采用可变产品 object 而不是该可变产品的变体。 The WC_Product object is already included in the function as an argument.
WC_Product object 已作为参数包含在 function 中。 Stock is decreased on the products variatons and not on the parent variable product.
产品变体的库存减少,而不是父变量产品的库存减少。
Correction:更正:
Replace $product->get_stock_quantity()
by $_product->get_stock_quantity()
.将
$product->get_stock_quantity()
替换为$_product->get_stock_quantity()
。
function wcs_custom_get_availability( $availability, $_product ) {
global $product;
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __( 'Plenty available in our store!', 'woocommerce');
}
// Change in Stock Text to only 1 or 2 left
if ( $_product->is_in_stock() && $_product->get_stock_quantity() <= 2 ) {
$availability['availability'] = sprintf( __('Only %s left in store!', 'woocommerce'), $_product->get_stock_quantity() );
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sorry, All sold out!', 'woocommerce');
}
return $availability;
}
in my case none of the answers worked, so I figured it out by myself.就我而言,没有一个答案有效,所以我自己想出来了。 The thing is you should check first if
managing_stock()
is on.问题是您应该首先检查
managing_stock()
是否打开。 If it is, then we're good to go.如果是,那么我们可以使用 go。
So, the final code is:所以,最终的代码是:
/**
* Change the test for "In Stock / Quantity Left / Out of Stock".
*/
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
global $product;
// Change In Stock Text
if ( $_product->managing_stock() && $_product->is_in_stock() ) {
echo '<p class="store-inside__number">';
$availability['availability'] = __( 'Plenty available in our store!', 'woocommerce');
echo '</p>';
}
// Change in Stock Text to only 1 or 2 left
if ( $_product->managing_stock() && $_product->is_in_stock() && $_product->get_stock_quantity() <= 2 ) {
$availability['availability'] = sprintf( __('Only %s left in store!', 'woocommerce'), $_product->get_stock_quantity() );
}
// Change Out of Stock Text
if ( $_product->managing_stock() && ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sorry, All sold out!', 'woocommerce');
}
return $availability;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.