简体   繁体   English

在Woocommerce中发送包含动态主题和收件人的自定义电子邮件

[英]Send a custom email with a dynamic subject and recipient in Woocommerce

In Woocommerce, I have been able to create custom meta box with a button, which send an email, using Send a custom email when WooCommerce checkout button is pressed existing answer code. 在Woocommerce中,我已经能够创建一个带有按钮的自定义元框,该按钮可以通过按下WooCommerce签出按钮时使用自定义电子邮件发送现有电子邮件来发送电子邮件 It displays the following metabox: 它显示以下元框:

元框

So when I click the button it sends an email, which works just fine. 因此,当我单击该按钮时,它会发送一封电子邮件,效果很好。

My question: 我的问题:
How I can customize that code to send custom email as I need to send "Subject" only (with order id and payment total) to email address, which should be in format: mobilenumber@domain.com (so it will be send to SMS gateway and delivered as SMS on a mobile device)? 我如何自定义代码以发送自定义电子邮件,因为我只需要将“主题”(带有订单ID和付款总额)发送到电子邮件地址,该电子邮件地址的格式应为: mobilenumber@domain.com (这样它将发送至SMS网关并以短信形式在移动设备上发送)?

For example: 例如:
Email address: 123456789@smsgateway.com 电子邮件地址:123456789@smsgateway.com
subject: Your order {order_id} has been done. 主题:您的订单{order_id}已完成。 Total: {order_total}. 总计:{order_total}。 Thank you 谢谢

Updated 更新

The first function is optional and will display a required checkout field for the mobile phone. 第一个功能是可选的,它将显示手机的必填字段。

The second function code display a metabox in woocommerce admin order edit pages with a custom button that will trigger the action. 第二个功能代码使用自定义按钮在woocommerce管理员订单编辑页面中显示一个metabox,该按钮将触发操作。 When pressed it will send custom email to the SMS gateway with: 按下后,它将通过以下方式向SMS网关发送自定义电子邮件:

  • a specific dynamic subject containing the order number and the total amount 包含订单号和总金额的特定动态主题
  • a specific email address made of the customer mobile phone and a specific SMS gateway domain name like 'smsgateway.com' . 由客户手机制成的特定电子邮件地址和特定的SMS网关域名,例如'smsgateway.com'

Note: When using wp_mail() function, the message is mandatory (if there is no message or an empty one, the email is not sent). 注意:使用wp_mail()函数时,该消息是强制性的(如果没有消息或为空,则不发送电子邮件)。

The code: 编码:

// 1. Add mandatory billing phone field (and save the field value in the order)
add_filter( 'woocommerce_billing_fields', 'add_billing_mobile_phone_field', 20, 1 );
function add_billing_mobile_phone_field( $billing_fields ) {
    $billing_fields['billing_mobile_phone'] = array(
        'type'        => 'text',
        'label'       => __("Mobile phone", "woocommerce") ,
        'class'       => array('form-row-wide'),
        'required'    => true,
        'clear'       => true,
    );
    return $billing_fields;
}

// 2. Send a specific custom email to a SMS gateway from a meta box

// Add a metabox
add_action( 'add_meta_boxes', 'order_metabox_email_to_sms' );
function order_metabox_email_to_sms() {
    add_meta_box( 'sms_notification',
    __( 'SMS notification', "woocommerce" ),
    'order_metabox_content_email_to_sms',
    'shop_order', 'side', 'high' );
}

// Metabox content: Button and SMS processing script
function order_metabox_content_email_to_sms(){
    global $pagenow;

    if( $pagenow != 'post.php' || get_post_type($_GET['post']) != 'shop_order' )
        return; // Exit

    if ( ! ( isset($_GET['post']) && $_GET['post'] > 0 ) )
        return; // Exit

    $order_id = $_GET['post'];

    $is_sent = get_post_meta( $order_id, '_sms_email_sent', true );

    // Sending SMS
    if ( isset($_GET['send_sms']) && $_GET['send_sms'] && ! $is_sent ) {
        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // Using the billing mobile phone if it exist, or the billing phone if not.
        if( $mobile_phone = $order->get_meta( '_billing_mobile_phone' ) ) {
            $phone = $mobile_phone;
        } else {
            $phone = $order->get_billing_phone();
        }

        // Email address: Customer mobile phone + @ + sms domain
        $send_to = $phone . '@' . 'smsgateway.com';

        // Subject with the order ID and the order total amount
        $subject = sprintf(
            __("Your order number %d with a total of %s is being processed. Thank you.", "woocommerce"),
            $order_id, html_entity_decode( strip_tags( wc_price( $order->get_total() ) ) )
        );

        // Message: It is required (we just add the order number to it).
        $message = $order_id;

        // Sending this custom email
        $trigger_send = wp_mail( $send_to, $subject, $message );

        // Displaying the result
        if( $trigger_send ) {
            $result  = '<span style="color:green;">' ;
            $result .= __( "The SMS is being processed by the gateway." );

            // On email sent with success, Mark this email as sent in the Order meta data (To avoid repetitions)
            update_post_meta( $order_id, '_sms_email_sent', $trigger_send );
        } else {
            $result  = '<span style="color:red;">' ;
            $result .= __( "Sorry, but the SMS is not processed." );
        }
        $result .= '</span>';
    }

    // Displaying the button
    $href = '?post=' . $order_id . '&action=edit&send_sms=1';
    $send_text = isset($is_sent) && $is_sent ? '<em><small style="float:right;"> ('. __("Already sent") . ')</small></em>' : '';
    echo '<p><a href="' . $href . '" class="button">' . __( "Send SMS" ) . '</a>'.$send_text.'</p>';

    // Displaying a feed back on send
    echo isset($result) ? '<p><small>' . $result . '</small></p>' : false;
}

Code goes in function.php file of the active child theme (or active theme). 代码进入活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。


The four possibilities within this metabox: 此metabox中的四种可能性:

在此处输入图片说明

Once the SMS is sent once, the feature becomes inactive, to avoid any repetitions. SMS发送一次后,该功能将变为非活动状态,以避免重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义管理员 Email 主题为 Woocommerce - Custom Admin Email Subject for Woocommerce 基于 WooCommerce 中自定义字段值的电子邮件收件人 - Email recipient based on a custom field value in WooCommerce WooCommerce 中新订单电子邮件通知的自定义主题 - Custom subject for New Order Email notification in WooCommerce 对Woocommerce电子邮件主题启用自定义字段占位符 - Enable a custom field placeholder to Woocommerce email subject 在 WooCommerce 中为电子邮件主题添加自定义占位符 - Add a custom placeholder to email subject in WooCommerce 无法将WooCommerce自定义字段用作自定义电子邮件中的收件人 - Unable to use WooCommerce custom field as recipient in custom email 向 WooCommerce 中的其他隐藏收件人发送处理电子邮件通知 - Send processing email notification to an additional hidden recipient in WooCommerce Woocommerce电子邮件通知收件人有条件地基于自定义字段 - Woocommerce email notification recipient conditionally based on custom field 为 WooCommerce 中的特定产品属性添加自定义 email 订单收件人? - Adding a custom email order recipient for specific product attributes in WooCommerce? 将WooCommerce处理订单电子邮件通知收件人更改为自定义地址 - Change WooCommerce processing order email notification recipient to a custom address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM