douxing9228 2016-06-24 05:02
浏览 129
已采纳

WooCommerce - 从functions.php文件发送新订单/客户发票电子邮件

I am trying to send new order email and customer invoice email through functions.php file.

How to fire complete order object to send as an email?

I am trying two different type of code.

add_action('woocommerce_payment_complete', 'my_custom_checkout_field_looking');
function my_custom_checkout_field_looking( $order_id ) {
    $order = new WC_Order( $order_id );

    // first type of Code. Send Email but did not send complete order object.

    $mailer = WC()->mailer();
    $subject = 'New Order Email # ' . $order_id;
    $mailer->send( 'example@gmail.com', $subject, $mailer->wrap_message( $subject, $order ), '', '' );

    // Second type of Code. Do not send email and doing nothing

    global $woocommerce;
    $mailer2 = $woocommerce->mailer();
    // Email customer with order-processing receipt
    $email = $mailer2->emails['WC_Email_Customer_Invoice'];
    $email->trigger( $order );
    // Email admin with new order email
    $email = $mailer2->emails['WC_Email_New_Order'];
    $email->trigger( $order );  
}
  • 写回答

1条回答 默认 最新

  • drll42469 2016-08-25 11:19
    关注

    I think you need such type of code.

    add_action('woocommerce_thankyou', 'my_custom_new_order_email');
    function my_custom_new_order_email( $order_id ) {
    
            $order = new WC_Order( $order_id );
    
            $billing_address = $order->get_formatted_billing_address(); // for printing or displaying on web page
            $shipping_address = $order->get_formatted_shipping_address();
            $email = $order->billing_email;
            $name = $order->billing_first_name.' '.$order->billing_last_name;
            $billing_phone = $order->billing_phone;
            $date = date('M d, Y');
    
            $data   = '';
            $data   .= "<table border='0' cellpadding='0' cellspacing='0' width='600'><tbody><tr>
            <td valign='top' style='background-color:#fdfdfd'>
            <table border='0' cellpadding='20' cellspacing='0' width='100%'>
            <tbody>
            <tr>
            <td valign='top' style='padding:48px'>
            <div style='color:#737373;font-family:&quot;Helvetica Neue&quot;,Helvetica,Roboto,Arial,sans-serif;font-size:14px;line-height:150%;text-align:left'>
            <span>
            <p style='margin:0 0 16px'>
            You have received an order from $name. The order is as follows:
            </p>
            </span>
            <h2 style='color:#557da1;display:block;font-family:&quot;Helvetica Neue&quot;,Helvetica,Roboto,Arial,sans-serif;font-size:18px;font-weight:bold;line-height:130%;margin:16px 0 8px;text-align:left'>
            Order # $order_id ( $date )
            </h2>
            <div>
            <div>";
            if( sizeof( $order->get_items() ) > 0 ) {           
                $data   .=    "<table cellspacing='0' cellpadding='6' style='width:100%;border:1px solid #eee' border='1'>
                <thead>
                <tr>
                <th scope='col' style='text-align:left;border:1px solid #eee;padding:12px'>
                Product
                </th>
                <th scope='col' style='text-align:left;border:1px solid #eee;padding:12px'>
                Quantity
                </th>
                <th scope='col' style='text-align:left;border:1px solid #eee;padding:12px'>
                Price
                </th>
                </tr>
                </thead>
                <tbody>";
                $data   .= $order->email_order_items_table( false, true );            
                $data   .=  "</tbody><tfoot>";
                if ( $totals = $order->get_order_item_totals() ) {
                    $i = 0;
                    foreach ( $totals as $total ) {
                    $i++;
                    $label =    $total['label'];
                    $value = $total['value'];
                    $data .= "<tr>
                    <th scope='row' colspan='2' style='text-align:left; border: 1px solid #eee;'>$label</th>
                    <td style='text-align:left; border: 1px solid #eee;'>$value</td>
                    </tr>";
                    }
                }
                $data .= "</tfoot></table>";
            }
    
            $data .=        
            "<span>
            <h2 style='color:#557da1;display:block;font-family:&quot;Helvetica Neue&quot;,Helvetica,Roboto,Arial,sans-serif;font-size:18px;font-weight:bold;line-height:130%;margin:16px 0 8px;text-align:left'>
            Customer details
            </h2>
            <p style='margin:0 0 16px'>
            <strong>Email:</strong>
            <a href='mailto:' target='_blank'>
            $email
            </a>
            </p>
            <p style='margin:0 0 16px'>
            <strong>Tel:</strong>
            $billing_phone
            </p>
            <table cellspacing='0' cellpadding='0' style='width:100%;vertical-align:top' border='0'>
            <tbody>
            <tr>
            <td valign='top' width='50%' style='padding:12px'>
            <h3 style='color:#557da1;display:block;font-family:&quot;Helvetica Neue&quot;,Helvetica,Roboto,Arial,sans-serif;font-size:16px;font-weight:bold;line-height:130%;margin:16px 0 8px;text-align:left'>Billing address</h3>
            <p style='margin:0 0 16px'> $billing_address </p>
            </td>
            <td valign='top' width='50%' style='padding:12px'>
            <h3 style='color:#557da1;display:block;font-family:&quot;Helvetica Neue&quot;,Helvetica,Roboto,Arial,sans-serif;font-size:16px;font-weight:bold;line-height:130%;margin:16px 0 8px;text-align:left'>Shipping address</h3>
            <p style='margin:0 0 16px'> $shipping_address </p>
            </td>
            </tr>
            </tbody>
            </table>
            </span>
            </div>
            </td>
            </tr>
            </tbody>
            </table>
            </td>
            </tr>
            </tbody>
            </table>";
    
            $mailer = WC()->mailer();
            $subject = 'New Customer Order';
            $mailer->send( 'abc@example.com,def@example.com', $subject, $mailer->wrap_message( $subject, $data ), '', '' );     
    }
    

    Hopefully It will work for you.

    Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面