Add Inventory Counter to WooCommerce Product Page

How to add inventory counter to product page in WordPress; WordPress show stock quantity on product page; Add stock counter to WooCommerce product page; Display inventory count on WordPress product page; WordPress plugin to show product stock levels; Show available stock on WooCommerce product page; Add custom inventory field to WordPress product page; WordPress display product stock quantity; How to show stock levels on WordPress product page; WordPress inventory counter for product page;

Explanation

To show an inventory counter on your WooCommerce product page, this code does the trick. It displays how many items are in stock right on the product page, making it easy for customers to see availability.

  • Check Stock: The code first checks if the product is in stock. If it's not, nothing is shown.
  • Display Quantity: If there are items available, it shows a message like "In stock: 5". If there are no items left, it displays "Out of stock".
  • Where It Appears: This information is added to the product summary section on the product page.
  • Load Script: A script is loaded only on single product pages to ensure everything runs smoothly.
  • WooCommerce Check: The code checks if WooCommerce is active before running, ensuring it doesn't cause errors if WooCommerce isn't installed.

By using this setup, you can easily inform your customers about product availability, enhancing their shopping experience on your site.

Code

// Add inventory counter to WooCommerce product page

function wp_dudecom_display_stock_quantity() {
    global $product;

    if ( ! $product->is_in_stock() ) {
        return;
    }

    $stock_quantity = $product->get_stock_quantity();

    if ( $stock_quantity > 0 ) {
        echo '<p class="stock in-stock">' . sprintf( __( 'In stock: %s', 'woocommerce' ), $stock_quantity ) . '</p>';
    } else {
        echo '<p class="stock out-of-stock">' . __( 'Out of stock', 'woocommerce' ) . '</p>';
    }
}

add_action( 'woocommerce_single_product_summary', 'wp_dudecom_display_stock_quantity', 20 );

// Ensure the function is only executed on single product pages
function wp_dudecom_enqueue_stock_quantity_script() {
    if ( is_product() ) {
        wp_enqueue_script( 'wp-dudecom-stock-quantity', get_template_directory_uri() . '/js/stock-quantity.js', array( 'jquery' ), null, true );
    }
}

add_action( 'wp_enqueue_scripts', 'wp_dudecom_enqueue_stock_quantity_script' );

// Security best practices: Ensure the function is only executed in the WooCommerce context
function wp_dudecom_check_woocommerce_active() {
    if ( ! class_exists( 'WooCommerce' ) ) {
        return;
    }

    add_action( 'woocommerce_single_product_summary', 'wp_dudecom_display_stock_quantity', 20 );
}

add_action( 'init', 'wp_dudecom_check_woocommerce_active' );

Instructions

To add an inventory counter to your WooCommerce product page, follow these steps:

File Location: You will need to add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and active on your WordPress site.

Implementation Steps:

  1. Access your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are editing the functions.php file, or go to Plugins > Editor if you are using a custom plugin.
  3. Locate and open the functions.php file of your active theme or your custom plugin file.
  4. Copy and paste the provided code into the file.
  5. Save the changes to the file.
  6. Ensure that the stock-quantity.js file is available in your theme's js directory. If not, create a placeholder file or adjust the script enqueueing as needed.
  7. Visit a product page on your site to verify that the inventory counter is displayed correctly.

By following these steps, you will successfully display an inventory counter on your WooCommerce product pages, enhancing the shopping experience for your customers.

If you need further assistance or want to implement more advanced functionality, consider reaching out to wp-dude.com for expert help.