WooCommerce Product get_stock_quantity() method return null

Viewed 3811

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!

I have access to the product variable but the result is null but I would like to retrieve the quantity.

3 Answers

Using WC_Product method get_manage_stock() with it, you will be able to avoid this problem. 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; as for variable products it will take the variable product object instead of the variations of this variable product. The WC_Product object is already included in the function as an argument. 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().

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. If it is, then we're good to 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;
}
Related