• Hi there!

    I am in the process of helping a small leisure rental company with an issue regarding coupons in conjunction with a booking from Pinpoint.

    They would like to create coupons valid only for bookings on weekdays (Monday/Thursday).

    A Pinpoint booking will continue to be handled as a Woocommerce product. So are the coupons.

    Now I have a code that works with Woocommerce booking, but these hooks are different in the shopping cart.

    Will you help me make this code correct using the right hook(s)? The code is shown below.

    // Coupon validity checking
    add_filter('woocommerce_coupon_is_valid', 'coupon_week_days_check', 10, 2);
    function coupon_week_days_check($valid, $coupon)
    {
        // Set the coupon codes you want to restrict to weekdays
        $coupon_codes = array('couponcode', 'anothercode', 'thirdcode');
    
        $coupon_code = strtolower($coupon->get_code());
        $found = false;
    
        // Loop through cart items to check if the coupon code matches and the date is a weekday
        foreach (WC()->cart->get_cart() as $cart_item) {
            if (in_array($coupon_code, $coupon_codes) && isset($cart_item['booking']['_date'])) {
                $the_day = date('D', strtotime($cart_item['booking']['_date']));
                if (in_array($the_day, array('Mon', 'Tue', 'Wed', 'Thu'))) {
                    $found = true;
                    break; // No need to continue checking once we find a matching weekday
                }
            }
        }
    
        // If the coupon code is not valid for weekdays, set it as invalid
        if (!$found) {
            $valid = false;
        }
    
        return $valid;
    }
    
    // Coupon validity checking error message
    add_filter('woocommerce_coupon_error', 'coupon_weekdays_error_message', 10, 3);
    function coupon_weekdays_error_message($err, $err_code, $coupon)
    {
        // Set the coupon codes you want to restrict to weekdays
        $coupon_codes = array('couponcode', 'anothercode', 'thirdcode');
        $coupon_code = strtolower($coupon->get_code());
    
        if (intval($err_code) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED && in_array($coupon_code, $coupon_codes)) {
            $err = __("Code $coupon_code is uitsluitend te gebruiken voor boekingen op ma/do.", "woocommerce");
        }
    
        return $err;
    }

    In this, I think the combination hooks [‘booking’][‘_date’] is the culprit; but I can’t find the right hooks(s) in the source code.

    I have already tried these alternatives:
    [‘reservation’][‘_date’]
    [‘appointmend’][‘_date’]

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter vectory

    (@vectory)

    Nearly a week has past. No answer whatsoever?

    After a couple of other previous support tickets without relevant help and a big update promised almost a year ago, containing important new features, I’m not very enthousiastic choosing the premium plan for Pinpoint…

    Plugin Support Pinpoint World Support

    (@pinpointworld)

    Hi,

    Sorry for the delay, we tried to find a solution for you and also had to test it ourselves.

    What you can do is in the foreach, the loop you go tough the cart items you should add the following code:

    global $wpdb;
            global $DOPBSPWooCommerce;
            $reservations_data = $wpdb->get_results($wpdb->prepare('SELECT * FROM '.$DOPBSPWooCommerce->tables->woocommerce.' WHERE cart_item_key="%s" AND token="%s"',
                                                                               $cart_item['key'],
                                                                               $cart_item['dopbsp_token']));
    
                        foreach ($reservations_data as $reservation_data){
                            $data = json_decode($reservation_data->data);
    }       

    In the $data you can find all the information regarding the reservation for that cart. This includes the $data->check_in and $data->check_out dates.

    Hope this helps.

    Best Regards,

    Support Team

    Thread Starter vectory

    (@vectory)

    Hi,

    Thanks for your reply.
    I understand you had to test it yourself, I’m sorry.

    I’m affraid I’m getting it to work.
    Can you please provide me the complete and working php you tested it with?

    Thanks for your trouble.

    • This reply was modified 1 year, 9 months ago by vectory.
    Plugin Support Pinpoint World Support

    (@pinpointworld)

    Hi,

    Here is the function, we have run a couple of tests and it worked:

    {
    // Set the coupon codes you want to restrict to weekdays
        $coupon_codes = array('couponcode', 'anothercode', 'thirdcode');
    
        $coupon_code = strtolower($coupon->get_code());
        $found = false;
    
    // Loop through cart items to check if the coupon code matches and the date is a weekday
        foreach (WC()->cart->get_cart() as $cart_item) {
            global $wpdb;
            global $DOPBSPWooCommerce;
            $reservations_data = $wpdb->get_results($wpdb->prepare('SELECT * FROM '.$DOPBSPWooCommerce->tables->woocommerce.' WHERE cart_item_key="%s" AND token="%s"',
                                                                               $cart_item['key'],
                                                                               $cart_item['dopbsp_token']));
    
                        foreach ($reservations_data as $reservation_data){
                            $data = json_decode($reservation_data->data);
                            if (in_array($coupon_code, $coupon_codes)) {
                                $the_day = date('D', strtotime($data->check_in));
                                error_log( print_r( $the_day, true ));
                                if (in_array($the_day, array('Mon', 'Tue', 'Wed', 'Thu'))) {
                                    $found = true;
                                break; // No need to continue checking once we find a matching weekday
                                }       
            
    
    
                            }
                        }
    }
    
    // If the coupon code is not valid for weekdays, set it as invalid
    if (!$found) {
        $valid = false;
    }
    
    return $valid;
    }

    Best Regards,

    Support Team

    Thread Starter vectory

    (@vectory)

    Great! Thanks!
    This code works for me.

    I’ve encountered a new problem with this code:
    Regular coupon codes do not work anymore.. Is this in your test environment as well?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Cart Date Hook for Coupon rerstriction’ is closed to new replies.