This is How to Enable Limited-Time Sales in WooCommerce
As an ecommerce store owner, you often want to create a sense of urgency and excitement around your product offerings. One effective way to do this is by enabling limited-time sales, where certain products or collections are available for purchase only during a specific time window.
In this blog post, we'll walk you through the code you can use to implement this functionality in your WooCommerce store. This will allow you to:
- Define the start and end time for the sales period, as well as the timezone of your store.
- Replace the add-to-cart button with a "View Product" link on the shop and archive pages during the non-sales hours.
- Replace the add-to-cart button with an inactive custom button on the single product page during the non-sales hours.
Let's get started!
Conditional Time Function
The first step is to create a function that checks whether the current time is within the defined sales period. Here's the code:
function conditional_hours_range(){
// Define start / end hours range and time zone (default is 'UTC').
$start_hour = 13;
$end_hour = 16;
date_default_timezone_set ('Europe/Paris');
$now = strtotime("now"); // Current time
$today_time = strtotime(date("Y-m-d")); // Today's time at 00:00
$starting_time = $today_time + ( $start_hour * 3600 );
$ending_time = $today_time + ( $end_hour * 3600 );
// Return true or false
return $now >= $starting_time && $now <= $ending_time ? false : true;
}
In this function, we first define the start and end hours for the sales period, as well as the timezone of your store. The date_default_timezone_set()
function is used to set the timezone.
Next, we calculate the current time ($now
), the time at the start of the day ($today_time
), the starting time of the sales period ($starting_time
), and the ending time of the sales period ($ending_time
). Finally, we return false
if the current time is within the sales period, and true
if it's outside the sales period.
Replacing the Add-to-Cart Button in the Shop and Archives Pages
Now, let's replace the add-to-cart button with a "View Product" link on the shop and archive pages during the non-sales hours:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Only on custom hour range
if( conditional_hours_range() ){
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
In this code, we use the woocommerce_loop_add_to_cart_link
filter to replace the add-to-cart button with a link to the single product page. The conditional_hours_range()
function is used to check whether the current time is outside the sales period.
Replacing the Add-to-Cart Button in the Single Product Page
Next, let's replace the add-to-cart button with an inactive custom button on the single product page during the non-sales hours:
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
// Only on custom hour range
if( conditional_hours_range() ){
global $product;
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'custom_disabled_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'custom_disabled_button', 30 );
}
}
}
function custom_disabled_button(){
$button_text = __( "Soon enabled", "woocommerce" );
$style = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important;"';
echo '<a class="button" '.$style.'>' . $button_text . '</a>';
}
In this code, we use the woocommerce_single_product_summary
action to replace the add-to-cart button with a custom disabled button. The conditional_hours_range()
function is used to check whether the current time is outside the sales period.
For variable product types, we need to remove the default add-to-cart button and add our custom disabled button. For all other product types, we simply replace the default add-to-cart button with our custom disabled button.
The custom_disabled_button()
function creates the custom disabled button with a "Soon enabled" text and some CSS styles to make it look inactive.
By implementing this code in your WooCommerce store, you can now create limited-time sales and generate a sense of urgency around your product offerings. Remember to test the code thoroughly and adjust the start/end hours and timezone to match your specific requirements.
If you're looking for a more comprehensive solution to manage your website's conversion rates and user behavior, consider checking out Flowpoint.ai. Flowpoint.ai can help you identify all the technical errors that are impacting your conversion rates and directly generate recommendations to fix them
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.