EShopExplore

Location:HOME > E-commerce > content

E-commerce

How to Schedule WooCommerce Sale Prices Based on Days of the Week with PHP and WordPress WooCommerce Development

May 14, 2025E-commerce4698
How to Schedule WooCommerce Sale Prices Based on Days of the Week with

How to Schedule WooCommerce Sale Prices Based on Days of the Week with PHP and WordPress WooCommerce Development

In the dynamic world of e-commerce, it's crucial to be able to schedule sale prices based on specific days of the week. This adaptability can significantly boost sales and customer engagement. In this article, we will guide you through the process of implementing such a feature using PHP and WooCommerce's built-in functions within a WordPress environment.

Introduction to WooCommerce Sale Price Scheduling

WooCommerce is a popular e-commerce plugin for WordPress that provides robust features to manage online stores. One of the key functionalities is the ability to set sale prices for products. However, setting fixed sale prices can be inefficient. By scheduling these prices based on the days of the week, you can create more targeted promotions that resonate with your customers at optimal times.

Step-by-Step Guide to Schedule WooCommerce Sale Prices Based on Days of the Week

Here’s a step-by-step guide to implement this functionality:

Step 1: Create a Custom Function

1. First, create a custom function that will adjust the sale price based on the day of the week. You can add this function to your theme's functions file or a custom plugin.

add_action('woocommerce_before_calculate_totals', 'set_weekly_sale_prices');
function set_weekly_sale_prices($cart) {
    // Check if we are in the admin area or if the cart is empty
    if(is_admin() || !is_cart() || !is_checkout()) {
        return;
    }
    // Get the current day of the week (0  Sunday, 6  Saturday)
    $current_day  date('w');
    // Define sale prices based on the day of the week
    $sale_prices  [
        0 > 20, // Sunday
        1 > 15, // Monday
        2 > 10, // Tuesday
        3 > 5,  // Wednesday
        4 > 0,  // Thursday
        5 > 10, // Friday
        6 > 25  // Saturday
    ];
    // Loop through the cart items and set the sale price
    foreach($cart->get_cart() as $cart_item_key > $cart_item) {
        $product  $cart_item['data'];
        // Get the regular price
        $regular_price  floatval($product->get_regular_price());
        // Set the sale price based on the current day
        if (isset($sale_prices[$current_day])) {
            $sale_price  $sale_prices[$current_day];
            // Apply the sale price if it's lower than the regular price
            if ($sale_price set_sale_price($sale_price);
            } else {
                $product->set_sale_price($regular_price);
            }
        }
    }
}

Step 2: Explanation of the Code

Hook into WooCommerce: The function set_weekly_sale_prices is hooked into woocommerce_before_calculate_totals, which means it runs before the cart totals are calculated.

Check Context: The function checks if it’s being run in the admin area or if the cart is empty. If so, it returns early.

Determine the Current Day: It retrieves the current day of the week using date('w'), which returns 0 for Sunday through 6 for Saturday.

Define Sale Prices: An associative array $sale_prices is defined to set the sale prices for each day of the week.

Loop Through Cart Items: The function loops through each item in the cart, retrieves the product, and checks the regular price.

Set Sale Price: If the sale price for the current day is lower than the regular price, it sets the sale price. Otherwise, it clears the sale price.

Step 3: Testing

1. Add products to your cart: Test the functionality by adding products to your cart. 2. Change the day of the week: Ensure that the sale prices are applied correctly.

Additional Considerations

Performance and Optimization

Performance: This code runs every time the cart is calculated. Be mindful of performance, especially if you have a large number of products.

Customization

You can customize the sale prices in the $sale_prices array to fit your needs.

Cache Management

If you're using caching plugins, you may need to clear the cache to see changes.

Conclusion

By following the steps outlined above, you can effectively manage sale prices based on the days of the week in WooCommerce. This feature allows you to create more targeted promotions and boost sales. Remember to test and optimize the function to ensure it works seamlessly with your e-commerce store.