WooCommerce limited stock display apex tech agency



In order to establish a sense of urgency and enhance sales, store owners that use WooCommerce frequently want to display dynamic stock messages. This is typically done in order to promote sales. There is a possibility that your website will become more sluggish if you employ plugins for functions that are quite simple.

In this post, you will learn how to display a WooCommerce limited stock display in a dynamic manner without having to rely on plugins. This will be accomplished while you are reading this post. As an alternative, we will make use of a PHP function that is not only lightweight but also provides a more streamlined experience on the page.

Video Tutorial

You are currently viewing a placeholder content from Default. To access the actual content, click the button below. Please note that doing so will share data with third-party providers.

More Information

Why Should You Avoid Using WordPress Plugins for WooCommerce limited stock display?

The employment of plugins is beneficial; yet, they may have a number of downsides, including the following:

1. Challenges with Performance

A large number of WooCommerce plugins provide additional CSS, JavaScript, and database queries, which makes the website function more slowly. This is one of the reasons why performance issues occur.

2. Security Risks

As a security issue, plugins have the potential to become obsolete or vulnerable to attacks. This poses a possible threat to the system.

3. Compatibility Issues

There are some plugins that are not compatible with WooCommerce upgrades or other extensions that have been installed. These incompatibility issues can be frustrating.

4. Limited Customization Options

Plugins typically offer generic solutions, but they do not offer the capability to modify code in accordance with specific requirements.

By using a customized code snippet, you can avoid these problems and optimize the speed and dependability of your business.

How to Display a WooCommerce limited stock display Without a Plugin?

If you follow these instructions, you will be able to add a dynamic stock counter to your WooCommerce store:

  1. Proceed to the dashboard of your WordPress account that you have created.
  2. Select Theme Editor after clicking on the Appearance menu option.
  3. Launch your theme in order to have access to the functions.php file.
  4. Include the following PHP code at the very end of the file by copying and pasting it into the appropriate location.

Random Stock Message and Custom Code for WooCommerce

// Start the session
add_action('init', 'start_session', 1);
function start_session() {
    if (!session_id()) {
        session_start();
    }
}

// Add a custom message to the product page to show randomized stock left
add_action('woocommerce_single_product_summary', 'add_random_stock_message', 20);

function add_random_stock_message() {
    global $product;

    if ($product->is_in_stock() && $product->get_type() == 'simple') {
        if (!isset($_SESSION['random_stock_left'])) {
            $_SESSION['random_stock_left'] = rand(2, 12);
        }

        $random_stock_left = $_SESSION['random_stock_left'];

        echo '<p class="stock-left-message">Only <span class="stock-number">' . $random_stock_left . '</span> left in stock. Buy it now!</p>';
    }
}

// Enqueue custom CSS for animation
add_action('wp_head', 'add_custom_css');
function add_custom_css() {
    ?>
    <style>
        @keyframes zoomInOut {
            0% { transform: scale(1); }
            50% { transform: scale(1.2); }
            100% { transform: scale(1); }
        }
        .stock-left-message {
            color: red;
            font-weight: bold;
            font-size: 18px;
        }
        .stock-number {
            color: rgb(15,113,64);
            display: inline-block;
            animation: zoomInOut 1.5s ease-in-out infinite;
        }
    </style>
    <?php
}

How This Code Works:

  • The process of storing stock numbers for each individual product begins at the initiation of a session.
  • An arbitrary number that falls anywhere between two and twelve is assigned to each product during the entirety of each session.
  • In a manner that is dynamic, the message is displayed beneath the description of the product.
  • The stock number has an animation effect that zooms in and out, and this is done with the intention of bringing as much attention as possible to itself.
  • The stock number has been given the color rgb(15,113,64) in order to strengthen its visibility.

Advantages of Using This Code Instead of Plugins

1. Enhanced Performance Acceleration

As a result of the fact that this PHP function is lightweight, it does not introduce any new scripts or database queries, which helps to ensure that your website continues to run quickly.

2. A Safer Option

In contrast to plugins, which may have vulnerabilities in terms of security, the code in question is entirely within your control.

3. Optional Enhancements for Personalization

To make it adhere to the style of your brand, it is possible to make adjustments to the animation, color, stock range, or any other element.

4. Compatibility With WooCommerce Upgrades

Due to the fact that it only modifies the logic that is displayed on the front end of the website, this code is compatible with WooCommerce upgrades.

Conclusion

The deployment of this WooCommerce limited stock display custom code has the potential to enhance the overall user experience while also increasing conversions by creating a sense of urgency. Using this method instead of plugins ensures that your website remains lightweight and properly optimized.

If you want to improve the efficiency of your WooCommerce store, start using this code today! 🚀

How do I add a dynamic stock message to WooCommerce without using a plugin?

Can I display random stock numbers for each product dynamically?

Will adding custom PHP code for stock messages slow down my WooCommerce site?

How do I make stock messages product-specific in WooCommerce?

What are the advantages of using custom code instead of a plugin for stock messages?

Can I add animation effects to my stock messages using CSS?

How do I ensure my custom stock message updates properly for different products?

Will WooCommerce updates affect my custom stock display code?

How can I customize the stock message text and appearance?

Is using PHP for stock messages secure compared to plugins?


Want to add WhatsApp Button in your WordPress without plugin?


WhatsApp Button without plugin Apex Tech Agency

Want to add WhatsApp Button in your WordPress without plugin?

Share Article:

Leave a Reply