skip to Main Content

I’m using this coding to restrict the users to create an order per year, and it’s working well. But, I noticed that if the date created of the last order is more than 365 days, the coding takes it as it was from this year so it doesn’t let users that have purchased with more than 365 days to buy again. Can someone please review the coding and make the necessary modifications? Thanks in advance!

function new_order_allowed() {
    if( ! ( is_cart() || is_checkout() ) ) return;

    if ( is_user_logged_in() ) {
        $user_id = get_current_user_id();

        $last_order = wc_get_customer_last_order( $user_id );

        if ( $last_order ) {
            $date_created = $last_order->get_date_created()->format( 'z' ) + 1;
            $current_time = current_time( 'z', true ) + 1;
            $year_in_day = 365;
            $days_passed = $current_time - $date_created;

            if ( $days_passed < $year_in_day ) {
               wc_add_notice( sprintf( '<b>ONLY ONE PURCHASE IS ALLOWED WITHIN 365 DAYS. </b><br>Your last order was %1$s days ago. Please try again when the 365 day period has been reached. ',  $days_passed ), 'error' );

                remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
            }
        }
    }
}
add_action( 'woocommerce_check_cart_items', 'new_order_allowed' );

2

Answers


  1. Why you not do it in a simple way?

    $dateFrom = new DateTime($last_order->get_date_created()->getTimestamp());
    $dateTo = new DateTime();
    $dateTo->modify('-1 year');
    echo $dateFrom->diff($dateTo)->days;
    

    That way $dateFrom->diff($dateTo)->days; will return for you the quantity of day between both dates.

    Login or Signup to reply.
  2. Try the following revised code based on timestamps:

    add_action( 'woocommerce_check_cart_items', 'new_order_allowed' );
    function new_order_allowed() {
        if ( is_user_logged_in() && ( is_cart() || is_checkout() ) ) {
            $last_order = wc_get_customer_last_order( get_current_user_id() );
    
            if ( is_a( $last_order, 'WC_Order') ) {
                $created_timestamp = $last_order->get_date_created()->getTimestamp();
                $allowed_timestamp = strtotime('-1 year');
                $remaining_days    = floor(($created_timestamp - $allowed_timestamp) / DAY_IN_SECONDS);
                $days_passed       = 365 - $remaining_days;
    
                if ( $created_timestamp >= $allowed_timestamp ) {
                    wc_add_notice( sprintf( 
                        __('<strong>ONLY ONE PURCHASE IS ALLOWED WITHIN 365 DAYS.</strong><br>Your last order was made %s ago. Please try again when the 365 day period has been reached.'),
                    sprintf( _n( '%s day', '%s days', $days_passed ) ) ), 'error' );
    
                    remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
                }
            }
        }
    }
    

    It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search