1

I am working on a marketplace plugin. If the vendors do not accept the order within the date range I set, the order will be cancelled. For this, I have determined a time period of order date +3 days. So far, so good.

For example,
deadline ( $last_time): 1623593126
Today: $now // (unix today)
Order Code: $order_number

According to the scenario above, I am sending an e-mail to an e-mail address by using wp_mail that the order was not accepted. The problem starts here. Wordpress mail not working as I want. If the scenario is realized, that is, if the vendor does not accept the order, an e-mail is sent. However, every time the page is refreshed, this mail comes again and again. How can I prevent this? Therefore, the mail should come only once. I would be glad if you help. I've been searching for about 3 days but couldn't find a solution. My code is below:

foreach ( $user_orders as $order ) {

//...codes...

if($now>$last_time) {
    $order_number = $order->get_order_number(); 
    $to = 'example@example.com';
    $subject = 'Failed order ';
    $body = '#'.$order_number. ' order number failed;
    $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail( $to, $subject, $body, $headers );

}

//...codes...
0

You can use meta to create a one time action

something like this

$order_id = $order->get_id();

if (get_post_meta($order_id, 'is_mail_sent', true)) return;

update_post_meta($order_id, 'is_mail_sent', true);

Add this right after the opening of the if block

This code will first check if the order meta value of is_mail_sent is true, if true it will return (exit) from the funcion.

If it was not true, first time, then it will continue to the rest of the code and also set the is_mail_sent to true to prevent future mail sending

You could also do this

$order_id = $order->get_id();

if($now>$last_time && !get_post_meta($order_id, 'is_mail_sent', true)) {
    update_post_meta($order_id, 'is_mail_sent', true);

    $order_number = $order->get_order_number(); 
    $to = 'example@example.com';
    $subject = 'Failed order ';
    $body = '#'.$order_number. ' order number failed;
    $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail( $to, $subject, $body, $headers );
}

This will perform the same check but will only wrap the mail part of the code

3
  • Thank you so much. Problem solved. The page must probably be loaded at least once for the email to be sent. Do you have any information on this subject? That is, if the vendor does not accept the order, an e-mail should be sent even if vendor does not log in to the page. Jun 8 at 20:09
  • For that you would need to use cron jobs, either built in wordpress cron jobs or cron jobs from your hosting Jun 9 at 6:06
  • Okay. I see. Thank you so much. Jun 17 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.