![](/img/trans.png)
[英]Custom email notification for local shipping and completed orders only in Woocommerce
[英]WooCommerce customized email only for processing and completed orders
我正在使用插件在 email 订单中发送自定义推荐代码,但现在我发现每个订单 email 都插入了代码。
function gens_raf_customer_email( $order, $sent_to_admin, $plain_text ) {
$user_id = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->customer_user : $order->get_customer_id();
if( ! empty( $user_id ) && ( get_user_meta($user_id, "gens_referral_id", true) ) != '' ){
$code = get_user_meta($user_id, "gens_referral_id", true);
} else {
$code = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->billing_email : $order->get_billing_email();
}
if( $plain_text ){
_e('Your referral code is: ','gens-raf') . $code;
} else {
echo '<p style="text-align:center;margin-top:10px;">Your referral code is: ' .get_home_url() .'?raf='. $code . '</p>';
}
} add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 3);
我在胡闹
if ( $email->id == 'customer_completed_order' )
但是 php 代码生成器给了我错误。 主要问题是仅将 email 发送到已完成和/或处理的订单,而不是退款、取消等。
谢谢!
您收到错误是因为您没有将可用的第四个$email
参数添加到woocommerce_email_customer_details
挂钩。
内容只会显示在已完成订单和处理订单模板中。
尝试这个:
add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 4 );
function gens_raf_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' || $email->id == 'customer_processing_order' ) {
$user_id = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->customer_user : $order->get_customer_id();
if( ! empty( $user_id ) && ( get_user_meta($user_id, "gens_referral_id", true) ) != '' ){
$code = get_user_meta($user_id, "gens_referral_id", true);
} else {
$code = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->billing_email : $order->get_billing_email();
}
if( $plain_text ){
_e('Your referral code is: ','gens-raf') . $code;
} else {
echo '<p style="text-align:center;margin-top:10px;">Your referral code is: ' .get_home_url() .'?raf='. $code . '</p>';
}
}
}
该代码已经过测试并且可以工作。
变量$email
是您的钩子 function 中缺少的第 4 个参数,您需要针对特定的 email 通知。
改为使用(对于 woocommerce 3 及更高版本) :
add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 100, 4 );
function gens_raf_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific notifications: Customer processing and completed orders
if ( in_array( $email->id, ['customer_processing_order', 'customer_completed_order'] ) ) {
$gens_raf = get_user_meta( $order->get_user_id(), "gens_referral_id", true );
$code = empty( $gens_raf ) ? $order->get_billing_email() : $gens_raf;
if( $plain_text ){
printf( __('Your referral code is: %s', 'gens-raf'), $code );
} else {
$output = sprintf( __("Your referral code is: %s", "gens-raf"), get_home_url() .'?raf='. $code );
echo '<p style="text-align:center;margin-top:10px;">' . $output . '</p>';
}
}
}
代码进入活动子主题(或活动主题)的functions.php文件。 它应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.