Typek – Blog

Overcoming the ‘Out of Stock’ Issue in WooCommerce: A Simple PHP Snippet Solution

Introduction

WooCommerce is a powerful e-commerce plugin for WordPress that allows businesses and entrepreneurs to sell their products online. One common issue faced by WooCommerce store owners is the ‘Out of Stock’ problem. This occurs when a product is out of stock or unavailable for purchase. In this tutorial, we will explore a simple PHP snippet solution to overcome the ‘Out of Stock’ issue in WooCommerce.

Understanding the ‘Out of Stock’ Issue in WooCommerce

When a product runs out of stock in WooCommerce, the default behavior is to display an ‘Out of Stock’ message on the product page. This can be a problem for store owners, as it may discourage potential customers from purchasing other products or lead to lost sales. Additionally, the default ‘Out of Stock’ message may not provide enough information to the customer, such as when the product will be back in stock or if there are alternative products available.

Creating a Custom PHP Snippet to Manage ‘Out of Stock’ Products

To overcome the ‘Out of Stock’ issue in WooCommerce, we can create a custom PHP snippet that will allow us to manage how out-of-stock products are displayed on our website. This can be done by either modifying the functions.php file in your WordPress theme, or by using a custom plugin that allows you to add PHP code snippets.

Step 1: Accessing the WooCommerce Hook

The first step in creating a custom PHP snippet is to access the WooCommerce hook that controls the display of out-of-stock products. This hook is called woocommerce_get_availability and can be found in the class-wc-product.php file located in the woocommerce/includes folder.

Step 2: Creating a Custom Function to Modify ‘Out of Stock’ Display

Next, we will create a custom function that will modify the display of out-of-stock products using the woocommerce_get_availability hook. This function will be added to our functions.php file or a custom plugin file.

For this tutorial, we will create a custom function called typek_modify_out_of_stock_message that will display a custom message when a product is out of stock, as well as provide an estimated restocking date and a link to alternative products.

<?php
function typek_modify_out_of_stock_message($availability, $product)
{
    // Check if the product is out of stock
    if (!$product->is_in_stock()) {
        // Set a custom out of stock message
        $availability['availability'] = __('Sorry, this product is currently out of stock.');

        // Set an estimated restocking date
        $restocking_date = get_post_meta($product->get_id(), '_restocking_date', true);
        if (!empty($restocking_date)) {
            $availability['availability'] .= ' ' . sprintf(__('We expect to have more in stock by %s.', 'woocommerce'), date_i18n(get_option('date_format'), strtotime($restocking_date)));
        }

        // Provide a link to alternative products
        $alternative_products_link = get_post_meta($product->get_id(), '_alternative_products_link', true);
        if (!empty($alternative_products_link)) {
            $availability['availability'] .= ' ' . sprintf(__('Click here to view alternative products.', 'woocommerce'), esc_url($alternative_products_link));
        }
    }

    return $availability;
}
add_filter('woocommerce_get_availability', 'typek_modify_out_of_stock_message', 10, 2);
?>

In the code above, we first check if the product is out of stock using the is_in_stock() method. If the product is out of stock, we set a custom out-of-stock message and then retrieve the estimated restocking date and alternative products link using the get_post_meta() function. If these values are not empty, we append them to the custom out-of-stock message.

Step 3: Adding Custom Fields for Restocking Date and Alternative Products

To provide the estimated restocking date and a link to alternative products in our custom function, we need to add custom fields to our WooCommerce product pages. This can be done using the woocommerce_product_options_stock_fields action hook.

First, create a custom function called typek_add_custom_stock_fields that will add the custom fields to the product pages. This function will be added to our functions.php file or a custom plugin file.

<?php
function typek_add_custom_stock_fields()
{
    echo '<div class="options_group">';

    // Add a custom field for the estimated restocking date
    woocommerce_wp_text_input(array(
        'id' => '_restocking_date',
        'label' => __('Estimated Restocking Date', 'woocommerce'),
        'placeholder' => 'YYYY-MM-DD',
        'description' => __('Enter the estimated restocking date for this product.', 'woocommerce'),
        'desc_tip' => 'true',
    ));

    // Add a custom field for the alternative products link
    woocommerce_wp_text_input(array(
        'id' => '_alternative_products_link',
        'label' => __('Alternative Products Link', 'woocommerce'),
        'placeholder' => 'https://example.com/alternative-products/',
        'description' => __('Enter the URL to a page with alternative products for this product.', 'woocommerce'),
        'desc_tip' => 'true',
    ));

    echo '</div>';
}
add_action('woocommerce_product_options_stock_fields', 'typek_add_custom_stock_fields');
?>

In the code above, we use the woocommerce_wp_text_input() function to add two custom fields for the estimated restocking date and alternative products link. These fields will be displayed in the ‘Product Data’ section of the product pages.

Step 4: Saving Custom Field Data

To save the custom field data when a product is updated, we need to create a custom function that will hook into the woocommerce_process_product_meta action.

Create a custom function called typek_save_custom_stock_fields that will save the custom field data to the product’s post meta. This function will be added to our functions.php file or a custom plugin file.

<?php
function typek_save_custom_stock_fields($post_id)
{
    // Save the estimated restocking date
    $restocking_date = isset($_POST['_restocking_date']) ? sanitize_text_field($_POST['_restocking_date']) : '';
    update_post_meta($post_id, '_restocking_date', $restocking_date);

    // Save the alternative products link
    $alternative_products_link = isset($_POST['_alternative_products_link']) ? esc_url_raw($_POST['_alternative_products_link']) : '';
    update_post_meta($post_id, '_alternative_products_link', $alternative_products_link);
}
add_action('woocommerce_process_product_meta', 'typek_save_custom_stock_fields');
?>

In the code above, we use the update_post_meta() function to save the custom field data to the product’s post meta.

Conclusion

By creating a custom PHP snippet to manage out-of-stock products, we can provide a better experience for our customers and potentially increase sales. This tutorial has demonstrated how to create a custom PHP snippet that modifies the ‘Out of Stock’ message, provides an estimated restocking date, and offers a link to alternative products. With this solution in place, you can overcome the ‘Out of Stock’ issue in your WooCommerce store and keep your customers engaged and informed.

More traffic??

Are you looking for effective solutions to increase your Google search visibility?

Contact us and discover the full potential of online sales.

What our clients are say?

For me, they are a happiness factory. They managed my store’s website in such a way that users became happy, after which it turned out that the search engine was also happy. When the search engine became happy and started bringing me more happy customers, my business became equally happy 🙂

Matt

Great company, I recommend it to everyone. Services of the highest standard. Very good customer approach. I am very satisfied with their work.

Lukasz Szajny

Complete professionalism and an incredible commitment to their work. It’s worth emphasizing their hassle-free communication and full availability. I definitely recommend them.

Alexander