检查订单备注是否包含特定单词并在 WooCommerce 客户完成订单电子邮件通知中显示消息
Check if order note contains specific words and display message in WooCommerce customer completed order email notification
我正在尝试向 WooCommerce 中的客户完成订单模板文件添加特定功能。
我希望该功能检查订单备注是否包含特定单词“Loren ipsum”,如果是,则显示我选择的消息。
这是我的客户完成的 order.php 电子邮件模板文件的样子:
<?php
/**
* Customer completed order email
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-completed-order.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates\Emails
* @version 3.7.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/*
* @hooked WC_Emails::email_header() Output the email header
*/
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<?php /* translators: %s: Customer first name */ ?>
<p>
<?php
$comment_obj = get_comment( $comment_id );
$customer_note = $comment_obj->comment_content;
$word = "Loren ipsum";
$mystring = $customer_note;
$order_id = $order->get_id();
// ACF field, will give an error if you don't use this specific plugin
//$pristatysimePatys = get_field('pristatysime_patys', $order_id);
if (strpos($mystring, $word) !== false) {
$message = "Your order number " . $order_id . " is sended. " . $customer_note ." .";
} elseif ($pristatysimePatys) {
$message = "Your order number " . $order_id . " is sended. The custom field checkbox in order are selected.";
} else {
$message = "There are no Loren Ipsum in order notes and custom field with checkbox is not selected.";
}
printf($message);
?>
</p>
<?php
/*
* @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Structured_Data::generate_order_data() Generates structured data.
* @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
* @since 2.5.0
*/
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
但我在某个地方犯了一个错误,因为即使“Loren ipsum”这个词在订单注释中,它也会在电子邮件中显示我的其他结果。有什么建议吗?
关于您的代码尝试的一些注意事项:
- 无需覆盖模板文件,您可以使用
woocommerce_email_order_details
挂钩,然后通过 $email->id
定位正确的电子邮件通知
- get_comment() 检索给定评论 ID 或评论对象的评论数据。要获取订单备注,您可以使用
wc_get_order_notes()
或 $order->get_customer_note()
在结账时获取客户备注。
所以你得到:
function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific email notification
if ( $email->id == 'customer_completed_order' ) {
// Get order id
$order_id = $order->get_id();
// Get order notes
$order_notes = wc_get_order_notes( array(
'order_id' => $order_id,
'order_by' => 'date_created',
'order' => 'ASC',
));
// Notes is NOT empty
if ( ! empty( $order_notes ) ) {
foreach ( $order_notes as $order_note ) {
// DEBUG information, delete afterwards
echo '<p style="color:red;font-size:20px;">' . $order_note->content . '</p>';
// PHP 8
if ( str_contains( $order_note->content, 'Loren ipsum' ) ) {
echo '<p style="color:blue;font-size:20px;">My text 1</p>';
}
// Before PHP 8
if ( strpos( $order_note->content, 'Loren ipsum' ) !== false ) {
echo '<p style="color:blue;font-size:20px;">My text 2</p>';
}
}
}
// OR get customer note
$customer_note = $order->get_customer_note();
// NOT empty
if ( ! empty ( $customer_note ) ) {
echo '<p style="color:green;font-size:20px;">' . $customer_note . '</p>';
}
}
}
add_action ('woocommerce_email_order_details', 'action_woocommerce_email_order_details', 1, 4 );
代码进入活动子主题(或活动主题)的 functions.php 文件
我正在尝试向 WooCommerce 中的客户完成订单模板文件添加特定功能。
我希望该功能检查订单备注是否包含特定单词“Loren ipsum”,如果是,则显示我选择的消息。
这是我的客户完成的 order.php 电子邮件模板文件的样子:
<?php
/**
* Customer completed order email
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-completed-order.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates\Emails
* @version 3.7.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/*
* @hooked WC_Emails::email_header() Output the email header
*/
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<?php /* translators: %s: Customer first name */ ?>
<p>
<?php
$comment_obj = get_comment( $comment_id );
$customer_note = $comment_obj->comment_content;
$word = "Loren ipsum";
$mystring = $customer_note;
$order_id = $order->get_id();
// ACF field, will give an error if you don't use this specific plugin
//$pristatysimePatys = get_field('pristatysime_patys', $order_id);
if (strpos($mystring, $word) !== false) {
$message = "Your order number " . $order_id . " is sended. " . $customer_note ." .";
} elseif ($pristatysimePatys) {
$message = "Your order number " . $order_id . " is sended. The custom field checkbox in order are selected.";
} else {
$message = "There are no Loren Ipsum in order notes and custom field with checkbox is not selected.";
}
printf($message);
?>
</p>
<?php
/*
* @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Structured_Data::generate_order_data() Generates structured data.
* @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
* @since 2.5.0
*/
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
但我在某个地方犯了一个错误,因为即使“Loren ipsum”这个词在订单注释中,它也会在电子邮件中显示我的其他结果。有什么建议吗?
关于您的代码尝试的一些注意事项:
- 无需覆盖模板文件,您可以使用
woocommerce_email_order_details
挂钩,然后通过$email->id
定位正确的电子邮件通知
- get_comment() 检索给定评论 ID 或评论对象的评论数据。要获取订单备注,您可以使用
wc_get_order_notes()
或$order->get_customer_note()
在结账时获取客户备注。
所以你得到:
function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific email notification
if ( $email->id == 'customer_completed_order' ) {
// Get order id
$order_id = $order->get_id();
// Get order notes
$order_notes = wc_get_order_notes( array(
'order_id' => $order_id,
'order_by' => 'date_created',
'order' => 'ASC',
));
// Notes is NOT empty
if ( ! empty( $order_notes ) ) {
foreach ( $order_notes as $order_note ) {
// DEBUG information, delete afterwards
echo '<p style="color:red;font-size:20px;">' . $order_note->content . '</p>';
// PHP 8
if ( str_contains( $order_note->content, 'Loren ipsum' ) ) {
echo '<p style="color:blue;font-size:20px;">My text 1</p>';
}
// Before PHP 8
if ( strpos( $order_note->content, 'Loren ipsum' ) !== false ) {
echo '<p style="color:blue;font-size:20px;">My text 2</p>';
}
}
}
// OR get customer note
$customer_note = $order->get_customer_note();
// NOT empty
if ( ! empty ( $customer_note ) ) {
echo '<p style="color:green;font-size:20px;">' . $customer_note . '</p>';
}
}
}
add_action ('woocommerce_email_order_details', 'action_woocommerce_email_order_details', 1, 4 );
代码进入活动子主题(或活动主题)的 functions.php 文件