【问题标题】:Send an email notification when a coupon code is used on a WooCommerce Order在 WooCommerce 订单上使用优惠券代码时发送电子邮件通知
【发布时间】:2020-11-24 09:03:19
【问题描述】:

使用特定优惠券时如何向业务伙伴发送订单通知?

我在此处应用优惠券时找到了一个解决方案: Send an email notification when a specific coupon code is applied in WooCommerce

但是,我需要为提交订单后的时间找到一个解决方案,因为订单并不总是在应用优惠券后提交。

每张优惠券都有自己的电子邮件地址。

【问题讨论】:

    标签: php wordpress woocommerce email-notifications coupon


    【解决方案1】:

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

    // 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();
        }
    }
    

    如果已为所应用的优惠券设置了电子邮件收件人,则会为每个应用的优惠券向提交的订单发送电子邮件。

    小心!仅选择以下功能之一:

    对于 woocommerce 版本最高 4.3 (新挂钩)

    // 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 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2018-01-05
      • 2021-03-09
      • 2020-07-28
      • 1970-01-01
      • 2019-07-02
      • 2020-09-18
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多