admin管理员组文章数量:1291083
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 = '[email protected]';
$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...
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 = '[email protected]';
$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...
Share
Improve this question
asked Jun 8, 2021 at 14:10
user205760user205760
1 Answer
Reset to default 0You 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 = '[email protected]';
$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
本文标签: pluginsHow can I send the mail once
版权声明:本文标题:plugins - How can I send the mail once? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741516588a2382927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论