gpt4 book ai didi

php - 在 WooCommerce 订单上使用优惠券代码时发送电子邮件通知

转载 作者:行者123 更新时间:2023-12-04 03:59:42 24 4
gpt4 key购买 nike

使用特定优惠券时如何向业务伙伴发送订单通知?
我在此处应用优惠券时找到了该实例的解决方案:
Send an email notification when a specific coupon code is applied in WooCommerce
但是,我需要在提交订单后找到解决方案,因为应用优惠券后订单并不总是提交。
每张优惠券都有自己的电子邮件地址。

最佳答案

首先,我们向管理优惠券页面添加一个设置字段,以设置优惠券的电子邮件收件人:

// Add a custom field to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_text_field', 10 );
function add_coupon_text_field() {
woocommerce_wp_text_input( array(
'id' => 'email_recipient',
'label' => __( 'Email recipient', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Send an email notification to a defined recipient' ),
'desc_tip' => true, // Or false

) );
}

// Save the custom field value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_text_field', 10, 2 );
function save_coupon_text_field( $post_id, $coupon ) {
if( isset( $_POST['email_recipient'] ) ) {
$coupon->update_meta_data( 'email_recipient', sanitize_text_field( $_POST['email_recipient'] ) );
$coupon->save();
}
}
如果已为应用的优惠券设置了电子邮件收件人,则为每个应用的优惠券向提交的订单发送电子邮件。

Caution! choose only one of the following functions:


对于 woocommerce 版本最高 4.3(新 Hook )
// For Woocommerce version 4.3+
add_action( 'woocommerce_checkout_order_created', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order ){
$used_coupons = $order->get_used_coupons();

if( ! empty($used_coupons) ){
foreach ( $used_coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code ); // WC_Coupon Object
$recipient = $coupon->get_meta('email_recipient'); // get recipient

if( ! empty($recipient) ) {
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
wp_mail( $recipient, $subject, $content ); // Send email
}
}
}
}
或适用于所有 WooCommerce 版本(自 3.0 版起)
// For all Woocommerce versions (since 3.0)
add_action( 'woocommerce_checkout_update_order_meta', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order_id ){
$order = wc_get_order( $order_id );

$used_coupons = $order->get_used_coupons();

if( ! empty($used_coupons) ){
foreach ( $used_coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code ); // WC_Coupon Object
$recipient = $coupon->get_meta('email_recipient'); // get recipient

if( ! empty($recipient) ) {
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
wp_mail( $recipient, $subject, $content ); // Send email
}
}
}
}
代码在您的事件子主题(或事件主题)的functions.php 文件中。测试和工作。
enter image description here

关于php - 在 WooCommerce 订单上使用优惠券代码时发送电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63243959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com