1

I'm trying to make a function that sets a cart discount of 10% no matter what product or how many that are in the cart.

This code works fine:

function site_wide_shop_discount_with_custom_title( $cart ) {
  $discount = $cart->subtotal * 0.1;
  $cart->add_fee( __( 'YOUR TEXT HERE', 'your-text-domain' ) , -$discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'site_wide_shop_discount_with_custom_title' );

My goal is to limit this to a date range and to 100 orders. This code, which is my goal, does not work:

function shop_discount_for_100_orders( $cart ) {

    $discountWeekStart = new DateTime('2018-10-07'); // when the discount week starts
    $dsicountWeekEnd  = new DateTime('2018-10-15'); // when the discount week ends
    $hundred_orders_discount = $cart->subtotal * 0.1; // during discount week, we give ten percent off the cart subtotal
    $hundred_orders_discount_over = $cart->subtotal; // no more discount


    if ( $discountWeekStart ) {
    $cart->add_fee( __( 'Global Discount Week', 'my-text-domain' ) , -$hundred_orders_discount );
    } else if {
        $hundred_orders_discount_over;
}
    }
add_action( 'woocommerce_cart_calculate_fees', 'shop_discount_for_100_orders' );

Any ideas on how to limit the discount to a date range and how to set it to 100 orders counting from the last one?

Any ideas, help or support is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

0

The following code will set conditionally a percentage discount limited to a specific number of orders count and based on a date-time range.

You will need to define the shop time zone choosing the right string from this time zones strings list

The code:

// Add a discount conditionally based on a date range for the
add_action( 'woocommerce_cart_calculate_fees', 'limited_date_range_percentage_discount' );
function limited_date_range_percentage_discount( $cart ) {
    // Your settings:
    date_default_timezone_set('Europe/Paris'); // Define the Time zone from this allowed time zones strings (http://php.net/manual/en/timezones.php)
    $start_time       = mktime('00', '00', '00', '10', '07', '2018'); // starting on "2018-10-07"
    $end_time         = mktime('23', '59', '59', '10', '15', '2018'); // Ending on "2018-10-15" (included)
    $now_time         = strtotime("now"); // Now time
    $percentage       = 10; // Discount percentage
    $max_orders_count = 100; // Limit to the first XXX orders

    $subtotal       = $cart->get_subtotal();
    $dicounts_count = get_option('wc-discounted-orders-count') ? get_option('wc-discounted-orders-count') : 0;

    if ( $now_time >= $start_time && $now_time <= $end_time && $dicounts_count <= $max_orders_count ) {
        $discount = $cart->get_subtotal() * $percentage / 100;
        $cart->add_fee( __( 'Week Discount', 'woocommerce' ) . ' (' . $percentage . '%)', -$discount );
    }
}

// Discounted orders count update
add_action('woocommerce_checkout_create_order', 'update_discounted_orders_count', 20, 2);
function update_discounted_orders_count( $order, $data ) {
    if( $orders_count = get_option('wc-discounted-orders-count') ){
        update_option( 'wc-discounted-orders-count', $orders_count + 1 );
    } else {
        update_option( 'wc-discounted-orders-count', 1 );
    }
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I added the code and changed the order count limit to 2 instead of 100, made a test order and after one test order the discount was gone. Is that the expected output? Meaning; if I put 100 as the limit, order 100 will not get a discount? The code otherwise works. –  Oct 08 '18 at 06:08
  • @bjornen I have lightly updated the code… The 2nd function is increasing the orders count each time an order is placed. – LoicTheAztec Oct 08 '18 at 06:11
  • I will read more about the date formatting, but I need to ask if this is correct in terms of starting on the 7th of october and ending on the 15? '$start_time = mktime(00, 00, 00, 10, 07, 2018); // starting on "2018-10-07" $end_time = mktime(23, 59, 59, 15, 07, 2018); // Ending on "2018-10-15" (included)' Looks wrong, but I guess I'm wrong? –  Oct 08 '18 at 06:16
  • @bjornen Sorry there was a little mistake in `mkdate` arguments… I have updated the code. It's Ok now and you can check the displayed date time with `echo date('Y-m-d H:i:s', mktime('00', '00', '00', '10', '07', '2018'));` or `date('Y-m-d H:i:s', mktime('00', '00', '00', '10', '15', '2018'));` somewhere else. So this answer works perfectly now… – LoicTheAztec Oct 08 '18 at 06:49
  • thank you for the change and update. Works really well now. –  Oct 08 '18 at 08:07