英文:
Avoid a hooked function to be triggered multiples times in WooCommerce
问题
我已在我的WordPress插件文件中包含了代码,以在创建或更新WooCommerce订单时与Zoho Books同步数据。然而,我遇到了一个问题,即在订单更新或创建期间,此功能被触发多次,导致在Zoho Books中创建多个发票。此外,在提供的示例代码中,当订单创建或更新时,电子邮件通知也被多次触发。我将非常感激您在解决此问题方面的任何协助。
add_action('woocommerce_update_order', 'my_function');
add_action('woocommerce_new_order', 'my_function');
function my_function($order_id) {
send_email_test();
}
function send_email_test() {
$to = 'ajaypartap92@gmail.com'; // 用收件人的电子邮件地址替换
$current_time = date('Y-m-d H:i:s');
$subject = $current_time.'示例主题'; // 用电子邮件主题替换
$message = '你好,这是一个示例电子邮件。'; // 用电子邮件内容替换
$headers = array(
'Content-Type: text/html; charset=UTF-8', // 将电子邮件内容类型设置为HTML
);
// 发送电子邮件
$result = wp_mail($to, $subject, $message, $headers);
// 检查电子邮件是否成功发送
if ($result) {
echo '电子邮件成功发送。';
} else {
echo '电子邮件发送失败。';
}
}
英文:
I have included code in my WordPress plugin file to sync data with Zoho Books whenever an WooCommerce order is created or updated. However, I am experiencing an issue where this function is triggered multiple times during order updates or creation, resulting in multiple invoices being created in Zoho Books. Additionally, in the provided sample code, the email notification is also being triggered multiple times when an order is created or updated. I would greatly appreciate any assistance in resolving this problem.
add_action('woocommerce_update_order', 'my_function');
add_action('woocommerce_new_order', 'my_function');
function my_function($order_id) {
send_email_test();
}
function send_email_test() {
$to = 'ajaypartap92@gmail.com'; // Replace with the recipient's email address
$current_time = date('Y-m-d H:i:s');
$subject = $current_time.'Example Subject'; // Replace with the subject of the email
$message = 'Hello, this is an example email.'; // Replace with the content of the email
$headers = array(
'Content-Type: text/html; charset=UTF-8', // Set the email content type as HTML
);
// Send the email
$result = wp_mail($to, $subject, $message, $headers);
// Check if the email was sent successfully
if ($result) {
echo 'Email sent successfully.';
} else {
echo 'Email sending failed.';
}
}
答案1
得分: 1
为了避免在操作挂钩上触发多次,您可以简单地使用WordPress的did_action()
如下所示:
add_action('woocommerce_update_order', 'my_function');
add_action('woocommerce_new_order', 'my_function');
function my_function($order_id) {
if (did_action('woocommerce_update_order') == 1 || did_action('woocommerce_new_order') == 1) {
send_email_test();
}
}
这次,您应该只会在订单创建或更新时收到一封电子邮件通知。
英文:
To avoid multiple triggers on action hooks, you can simply use WordPress did_action()
as follows:
add_action('woocommerce_update_order', 'my_function');
add_action('woocommerce_new_order', 'my_function');
function my_function($order_id) {
if( did_action('woocommerce_update_order') == 1 || did_action('woocommerce_new_order') == 1 ) {
send_email_test();
}
}
This time, you should receive just one email notification on order creation or update.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论