【发布时间】:2020-09-18 17:17:24
【问题描述】:
我正在尝试在 WooCommerce 订单电子邮件上显示使用过的优惠券 + 根据使用过的优惠券添加特定消息。
显示优惠券的工作基于
Add Applied Coupon Code in Admin New Order Email Template - WooCommerce
我试图通过检查字符串$coupon_codes 是否包含特定单词/特定优惠券(在本例中为“grouppurchase”)来扩展它,然后显示一条消息。
它需要用于$coupon_codes,因为它可以(并且将会)与多张优惠券一起使用
我设法使它与以下代码一起工作
How do I check if a string contains a specific word?
问题是 (strpos) 每当使用任何优惠券时都会显示消息,无论优惠券中写的是什么,我不知道为什么
我正在关注这个答案背后的 strpos 逻辑
// Display used Coupons on Emails and Add GroupPurchase Warning
add_action( 'woocommerce_email_after_order_table', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admins and when there at least 1 coupon in the order
if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;
foreach( $order->get_items('coupon') as $coupon ){
$coupon_codes[] = $coupon->get_code();
}
// For one coupon
if( count($coupon_codes) == 1 ){
$coupon_code = reset($coupon_codes);
echo '<p>'.__( 'Used Coupon: ').$coupon_code.'<p>';
}
// For multiple coupons
else {
$coupon_codes = implode( ', ', $coupon_codes);
echo '<p>'.__( 'Used Coupons: ').$coupon_codes.'<p>';
}
if (strpos($coupon_codes, 'groupurchase') !== false) {
echo '<p>' . 'With this coupon you are participating in a group purchase' . '<p>' ;
}
}
【问题讨论】:
标签: php wordpress woocommerce email-notifications coupon