Woocommerce How To Show Clock On Checkout

WooCommerce: How to Show a Clock on Your Checkout Page (Easy Guide for Newbies)

Want to add a little extra *urgency* or simply inform your customers about your business hours during checkout? Adding a clock to your WooCommerce checkout page is a simple way to do it. While WooCommerce doesn’t have a built-in clock feature, we’ll walk you through a couple of easy ways to add one, even if you’re not a coding whiz!

Why would you want a clock on your checkout? Think about these real-life scenarios:

* Limited-Time Offers: Imagine you’re running a flash sale that ends at midnight. A clock on the checkout page acts as a visual reminder of the ticking time bomb, encouraging customers to complete their purchase. “Order now before the timer runs out!”

* Order Cut-Off Times: You might have a cut-off time for same-day shipping. A clock helps customers understand if they’ll make the cut-off and receive their order quickly. “Orders placed before 2 PM EST ship today!”

* Operational Hours: You only operate during specific times. A clock makes it clear when you’re actively processing orders and handling customer service, managing expectations.

Let’s dive in!

Method 1: Using a Plugin (Easiest for Beginners)

The quickest and most beginner-friendly way to add a clock is by using a plugin. There are many plugins that offer clock functionality, but for our example, we’ll focus on a straightforward approach that any newbie can follow.

1. Search for a suitable plugin: In your WordPress dashboard, go to Plugins > Add New. Search for “HTML Widget”. A simple, free HTML Widget plugin will suffice.

2. Install and Activate the Plugin: Click “Install Now” and then “Activate.”

3. Add the Widget to Your Checkout Page: This part requires a little trickery. WooCommerce checkout isn’t as easily modified with widgets like a sidebar. Instead, we need a little code snippet to tell WordPress *where* to put our clock widget. You’ll need to modify your theme’s `functions.php` file. Be cautious when editing this file directly! *Always backup your website before making changes.*

 /** 
  • Add clock widget to WooCommerce checkout page.
  • */ function add_clock_to_checkout() { ?>
    <?php } add_action( 'woocommerce_before_checkout_form', 'add_clock_to_checkout' );

/

* Register a custom sidebar for the checkout.

*/

function register_checkout_sidebar() {

register_sidebar(

array(

‘id’ => ‘checkout-sidebar’, //This must match the one used above!

‘name’ => __( ‘Checkout Clock Sidebar’, ‘your-theme’ ),

‘description’ => __( ‘Sidebar for displaying the clock on the checkout page.’, ‘your-theme’ ),

‘before_widget’ => ‘

‘,

‘after_widget’ => ‘

‘,

‘before_title’ => ‘

‘,

‘after_title’ => ‘

‘,

)

);

}

add_action( ‘widgets_init’, ‘register_checkout_sidebar’ );

Explanation:

  • The first function `add_clock_to_checkout()` defines the HTML wrapper and the location where the widget area will be inserted on the checkout page. `woocommerce_before_checkout_form` is a WooCommerce action hook that places content *before* the checkout form.
  • The `dynamic_sidebar` function calls the widget from specified area.
  • The second function `register_checkout_sidebar()` creates a custom widget area (sidebar) that is specifically intended for adding our clock widget.
  • Always replace `’your-theme’` with your actual theme name.

Important: If you’re unsure how to edit `functions.php`, consider using a code snippets plugin. This is a safer way to add code without directly modifying your theme files.

4. Create your Clock Widget: Go to Appearance > Widgets. You should now see a new sidebar called “Checkout Clock Sidebar”. Drag an “HTML” Widget (the one you installed earlier) into this sidebar.

5. Add Clock Code to the Widget: Inside the HTML widget, paste the following code:

function updateClock() {

var now = new Date();

var hours = now.getHours();

var minutes = now.getMinutes();

var seconds = now.getSeconds();

// Add leading zeros for better display

hours = hours < 10 ? "0" + hours : hours;

minutes = minutes < 10 ? "0" + minutes : minutes;

seconds = seconds < 10 ? "0" + seconds : seconds;

var timeString = hours + “:” + minutes + “:” + seconds;

document.getElementById(“clock”).innerHTML = timeString;

}

// Update the clock every second

setInterval(updateClock, 1000);

#clock {

font-size: 20px;

text-align: center;

margin-bottom: 10px;

}

Explanation:

  • This code creates a simple digital clock that updates every second.
  • The `
    ` tag adds basic styling to the clock. You can customize this to match your website’s design.

6. Save the Widget: Click “Save” in the widget settings.

7. Check Your Checkout Page: Visit your WooCommerce checkout page. You should now see a functioning clock displayed!

Method 2: Using Custom Code (Intermediate)

If you’re comfortable with coding, you can add the clock directly using custom code. This method avoids using a plugin but requires editing your theme’s files (again, backup first!). This method is very similar to the plugin approach, but it avoids the step of requiring another plugin.

1. Edit `functions.php`: Go to Appearance > Theme Editor (or edit the `functions.php` file directly via Explore this article on How To Add A Short Dscription To Woocommerce Payment Gategay FTP). Add the following code to your `functions.php` file:

 /** 
  • Add clock to WooCommerce checkout page.
  • */ function add_clock_to_checkout_direct() { ?>
    function updateClock() { var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); var seconds = now.getSeconds();

    // Add leading zeros for better display

    hours = hours < 10 ? "0" + hours : hours;

    minutes = minutes < 10 ? "0" + minutes : minutes;

    seconds = seconds < 10 ? "0" + seconds : seconds;

    var timeString = hours + “:” + minutes + “:” + seconds;

    document.getElementById(“clock”).innerHTML = timeString;

    }

    // Update the clock every second

    setInterval(updateClock, 1000);

    #clock {

    font-size: 20px;

    text-align: center;

    margin-bottom: 10px;

    }

    <?php

    }

    add_action( ‘woocommerce_before_checkout_form’, ‘add_clock_to_checkout_direct’ );

    Explanation: This code combines the HTML, JavaScript, and CSS from the widget example into a single function that adds the clock directly to the checkout page.

    2. Save Changes: Click “Update File.”

    3. Check Your Checkout Page: Visit your WooCommerce checkout page. The clock should now be visible.

    Customization Tips:

    * Placement: You can change the `woocommerce_before_checkout_form` action hook to other hooks, such as `woocommerce_after_checkout_form` (after the form) or `woocommerce_before_customer_details` (before the customer details section), to adjust the clock’s placement. Consult the WooCommerce hook reference for more options.

    * Styling: Modify the CSS within the `

    ` tag to change the clock’s appearance (font size, color, background, etc.) to match your site’s branding.

    * Timezone: By default, the clock displays the server’s time. If you need to display a different timezone, you’ll need to use a more advanced JavaScript library like Moment.js and its timezone extension, and modify the JavaScript code accordingly. This is for more advanced users.

    * More Advanced Clocks: For countdown timers that count down to a specific date and time, use a Javascript library like “Countdown.js” or another dedicated countdown timer plugin.

    Adding a clock to your WooCommerce checkout page is a simple yet effective way to enhance the customer experience and boost conversions. Choose the method that best suits your technical skills and website’s needs. Remember to always back up your website before making changes to theme files! Good luck!