【问题标题】:Send a custom email with a dynamic subject and recipient in Woocommerce在 Woocommerce 中发送带有动态主题和收件人的自定义电子邮件
【发布时间】:2018-10-27 11:47:24
【问题描述】:

在 Woocommerce 中,我已经能够使用 Send a custom email when WooCommerce checkout button is pressed 现有的答案代码创建带有按钮的自定义元框,该按钮可以发送电子邮件。它显示以下元框:

所以当我点击按钮时,它会发送一封电子邮件,效果很好。

我的问题:
我如何自定义该代码以发送自定义电子邮件,因为我只需要将“主题”(带有订单 ID 和付款总额)发送到电子邮件地址,其格式应为:mobilenumber@domain.com(因此它将被发送到 SMS 网关和在移动设备上作为 SMS 发送)?

例如:
邮箱:123456789@smsgateway.com
主题:您的订单 {order_id} 已完成。总计:{order_total}。谢谢

【问题讨论】:

    标签: php wordpress woocommerce sms orders


    【解决方案1】:

    更新

    第一个功能是可选的,将显示手机的必填结帐字段。

    第二个功能代码在 woocommerce 管理订单编辑页面中显示一个元框,其中包含一个触发操作的自定义按钮。按下时,它将向 SMS 网关发送自定义电子邮件:

    • 包含订单号和总金额的特定动态主题
    • 由客户手机和特定短信网关域名组成的特定电子邮件地址,例如'smsgateway.com'

    注意:在使用wp_mail()功能时,留言为必填项(如果没有留言或为空则不发送邮件)。

    代码:

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

    代码进入活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    此元框内的四种可能性:

    一旦 SMS 发送一次,该功能就会变为非活动状态,以避免任何重复。

    【讨论】:

    • 不知道会发生什么,但我丢失了 metabox 的 sn-p ...如果我可以问你如何为 metabox 创建一个按钮来执行该操作(发送电子邮件)?这就是我正确知道元框出现的原因:
    • add_action( 'add_meta_boxes', 'add_meta_box_odoslatsms' ); function add_meta_box_odoslatsms() { add_meta_box( 'custom_order_meta_box', __( 'Odoslat SMS' ), 'custom_metabox_content', 'shop_order', 'side', 'high'); }
    • function custom_metabox_content(){ $post_id = isset($_GET['post']) ? $_GET['post'] : false; if(! $post_id ) return; // Exit }
    • 看起来不错,但在每种情况下我都会收到:抱歉,SMS 未处理。是因为我从后端手动创建订单吗?问题是我使用 WooCommerce 进行订单管理,我只从后端创建订单(没有前端交互)。谢谢
    • 我正在寻找有关此“元更新”的建议,因为我希望能够发送 SMS 2-3 次或能够在此 sn-p 中设置此值。 // 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 = '&lt;span style="color:red;"&gt;' ; $result .= __( "Chyba, SMS nemohla byt odoslana." );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2020-03-11
    • 2020-11-20
    • 2018-01-07
    相关资源
    最近更新 更多