简体   繁体   English

WooCommerce 产品 get_stock_quantity() 方法返回 null

[英]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.

相关问题 WooCommerce $ product-&gt; get_stock_quantity(); 打破购物车页面 - WooCommerce $product->get_stock_quantity(); breaking cart page Woocommerce 未捕获错误:在 null 上调用成员函数 get_stock_quantity() - Woocommerce Uncaught Error: Call to a member function get_stock_quantity() on null WooCommerce更新产品库存数量和状态 3 - Update product stock quantity and status in WooCommerce 3 按颜色显示产品变体库存数量 WooCommerce - Display product variation stock quantity by color for WooCommerce 从WooCommerce中的产品中检索库存数量的功能 - Function that retrieves the stock quantity from a product in WooCommerce 如何在woocommerce中读取vartaion产品的库存数量 - How to read stock quantity of a vartaion product in woocommerce WooCommerce 产品变化:自动启用管理库存和设置库存数量 - WooCommerce product variations: Auto enable Manage Stock and set stock quantity 如果Woocommerce中的产品缺货,请用表格替换数量字段 - Replace quantity field with a form if product is out of stock in Woocommerce WooCommerce:在普通页面或帖子上显示给定产品 ID 的库存数量 - WooCommerce: Display stock quantity for a given product ID on normal Pages or Posts 如何通过sku直接更新WooCommerce产品库存数量 - How to directly update WooCommerce product stock quantity by its sku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM