How to get the stock quantity of an article from woocommerce?

How to get the stock quantity of an article from woocommerce?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving get_sku() Errors and Accessing Stock Quantities in WooCommerce

The error Call to undefined function get_sku() is a classic symptom of mixing template hierarchy functions with global variables incorrectly. In your provided loop, you are using $product->get_price_html(), which correctly accesses the global $product object available within WooCommerce loops. However, get_sku() is not a standalone PHP function that exists in the core WordPress installation or standard WooCommerce files by default.

This confusion usually stems from conflating two different approaches: calling methods on an object versus calling global helper functions. The get_sku() function you are attempting to use likely comes from a specific theme file or a custom plugin, but it is not universally available. Even if such a function existed globally, relying on it inside a template loop is often inefficient because you are bypassing the data structure WooCommerce has already prepared for you.

To display the stock quantity correctly, you should leverage the $product object directly. Since your code block is inside a if ( ! defined( 'ABSPATH' ) ) exit; guard, you can safely assume it is running within the context of an active WooCommerce page. The global $product variable holds all the necessary data regarding stock status and quantity.

Accessing Stock Data via Object Methods

The most reliable way to retrieve stock information is through the get_stock_quantity() method on the $product object. This method returns the integer value of the available stock, or an empty string if the product is out of stock or has unlimited inventory.

Here is how you can modify your template logic:

global $product;

if ( $price_html = $product->get_price_html() ) : 
    echo '<span class="price">PREIS:' . $price_html . '</span>';
    
    // Retrieve the SKU using the product object method
    $sku = $product->get_sku(); 
    
    // Retrieve the stock quantity. Returns empty string if no stock or unlimited.
    $stock_qty = $product->get_stock_quantity(); 
    
    echo '<p class="stock-m13">SKU: ' . esc_html( $sku ) . ' | Stock: ' . 
         (empty($stock_qty) ? 'Unlimited' : $stock_qty) . '</p>';
endif;

This approach eliminates the need for any undefined global functions. By calling $product->get_sku() and $product->get_stock_quantity(), you ensure that you are using methods explicitly defined within the WC_Product class, regardless of which theme or plugin is active on the site.

Handling Edge Cases: Out of Stock and Unlimited Inventory

A critical detail often overlooked when displaying stock quantities is handling products that are out of stock or set to "Infinite" (Unlimited). The get_stock_quantity() method returns an empty string for these cases. If you simply echo the variable without checking, your layout might break or display a blank space where the number should be.

The implementation above handles this gracefully by checking if $stock_qty is empty. If it is, it displays "Unlimited"; otherwise, it displays the specific integer count. This provides a consistent user experience whether the product has 5 units left or never runs out of stock.

Security Considerations in Template Files

When outputting dynamic data like SKUs and quantities directly into HTML, you must sanitize the input. While SKU values are generally safe because they are alphanumeric strings generated by the system, it is best practice to use esc_html() for any user-facing data derived from the database. This prevents potential injection issues if a future update changes how SKUs are stored or if custom fields are involved.

Where to Find More Documentation

If you need deeper insights into product management hooks or customizing stock displays, resources like https://laravelcompany.com offer relevant guidance on building robust e-commerce integrations, though for core WooCommerce logic, the official documentation remains the primary source of truth. Always verify that your theme's functions.php or template files are not overriding global functions with names that shadow object methods, as this is a common cause of the "undefined function" errors you encountered initially.

Our network