【问题标题】:WooCommerce send new order email to customerWooCommerce 向客户发送新订单电子邮件
【发布时间】:2017-08-08 18:46:12
【问题描述】:

我有一个问题,听起来很愚蠢,但我真的被困住了。

我还需要向客户发送“新订单”电子邮件。我尝试将函数添加到functions.php 文件,它会发送电子邮件,但没有显示大部分变量。我将相同的代码插入到示例 php 文件中,在 wordpress 中添加为页面,它工作得非常好。真不知道怎么回事。

有什么解决办法吗?也许有更简单的方法,比如在woocommerce中将recipent添加到“新订单”电子邮件?我检查了 class-wc-email-new-order.php 文件,但我无法更改其中的任何内容,我不明白它是如何工作的。

screen: code working on page VS what is visible in received email

      function create_email_body($order_id){

    global $woocommerce;
    if ( !$order_id )
    return;
    $order = new WC_Order( $order_id );
    $body = '';
    $product_list = '';
    $order_item = $order->get_items();

    foreach( $order_item as $product ) {
        $body .= $product['name']; 
    }

$order = new WC_Order( $order_id );


$body ='        <div style = "width: 100%; clear: both;  color: #000;">';
$body .='           <div style="width: 50%; float: left;">';

$body .='               <p> Numer zamówienia:<b> '. $order->get_order_number() .'</b>   </p>';
$body .='               <p> Imię i nazwisko:<b> '. $order->billing_first_name . " " . $order->billing_last_name . '</b> </p>';
$body .='               <p> Sposób dostawy: <b>  '. $order->get_shipping_method() .'</b>    </p>';
$body .='               <p> Status zamówienia:<b>   przyjęte</b>    </p>';
$body .='               <p> Adres dostawy: <b> '. $order->shipping_address_1 . " " . $order->shipping_address_2  . ", " . $order->shipping_postcode . " "    . $order->shipping_city . ", " . $order->shipping_state  . $order->shipping_country .'</b> </p>';

$body .='           </div>';
$body .='           <div style="width: 50%; float: left;">';

$body .='               <p> Data zamówienia:<b> '. $order->order_date . '</b>   </p>';
$body .='               <p> Email:  <b><span style = "color: #000 !important"> '. $order->billing_email . '</b> </span></p> ';
$body .='               <p> Metoda płatności: <b>    '. $order->payment_method_title .'</p>';
$body .='               <p> Numer telefonu:<b> '. $order->billing_phone .'</b>  </p>    ';
$body .='           </div>';
$body .='       </div>';
$body .='       <div style = "width: 100%; clear: both; color: #000; margin-top: 30px;">';

$body .=' <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: "Myriad", sans-serif;" border="1">';
$body .=' <thead>';
$body .='   <tr>';
$body .='       <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;"></th>';
$body .='       <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;">Produkt</th>';
$body .='       <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;">Ilość</th>';
$body .='       <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;">Cena</th>';
$body .='       <th class="td" scope="col" style="text-align:left;" style = "border: none; color: #000;">Wartość</th>';
$body .='   </tr>';
$body .=' </thead>';
$body .=' <tbody>';

            $items = $order->get_items();
            foreach ( $items as $item_id => $item ) :
            $_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
            $item_meta    = new WC_Order_Item_Meta( $item, $_product );

            if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {

$body .='               <tr class=' . esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ) .'">';
$body .='                   <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; word-wrap:break-word; color: #000;">';


$body .='                   </td><td style = "border: none; color: #000">';
                        // Product name
$body .=$item['name'];///'                      <p style = "display: inline-block; float: left">' . apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );
                        if ( ! empty( $item_meta->meta ) ) {
$body .=                             nl2br( $item_meta->display( true, true, '_', "\n" ) );
                        }
$body .=                        "</p>";

$body .=                    '</td>';

$body .='                   <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ) .'</td>';
$body .='                   <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . $_product->get_price();
$body .='                   </td>';
$body .='                   <td class="td" style="text-align:left; vertical-align:middle; border: none; font-family: "Myriad", sans-serif; color: #000">' . $order->get_formatted_line_subtotal( $item );
$body .='               </td></tr>';

            }
        endforeach; 

$body .='               </tbody>';
$body .='               <tfoot><td style = "border: none;"></td><td style = "border: none;"></td><td style = "border: none; color: #000;"></td>';
$body .='                   <td style = "border: none; color: #000;">';
$body .='                       <p> Wartość produktów </p> ';
$body .='                       <p> Cena wysyłki </p>';
$body .='                       <p> Do zapłaty </p>';
$body .='                   </td>';
$body .='                   <td style = "border: none; color: #000;">';
$body .='                       <p>' . number_format((float)$order->get_subtotal(), 2, '.', '')  . " zł" . '</p>';
$body .='                       <p>' . number_format((float)$order->get_total_shipping(), 2, '.', '') . " zł" . '</p>';
$body .='                       <p>' . $order->get_formatted_order_total(). '</p>';
$body .='                   </td>';
$body .='               </tfoot>';
$body .='           </table>';
$body .='       </div>';

$body .= file_get_contents(get_stylesheet_directory_uri() . '/woocommerce/emails/customer-on-hold.order.php');

return $body;
    } 

    function send_email_also_to_customer($order_id){

$order = new WC_Order( $order_id );

$to_email = $order->billing_email;
$headers = 'Od: AB.com <a@b.com>' . "\r\n";
$mailer = WC()->mailer();
$subject = 'Potwierdzenie przyjęcia zamówienia numer # ' . $order_id;
$body = create_email_body($order_id);
$mailer->send( $to_email, $subject, $mailer->wrap_message( $subject, $body), '', '' );
    }
    add_action( 'woocommerce_new_order', 'send_email_also_to_customer');

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    您无需再次执行所有这些操作即可发送多封电子邮件。您可以简单地将收件人添加到“新订单”电子邮件(带有客户的电子邮件)。以下是您可以尝试的代码:

    <?php
    /**
     * Add another email recipient for admin New Order emails if a shippable product is ordered
     *
     * @param string $recipient a comma-separated string of email recipients (will turn into an array after this filter!)
     * @param \WC_Order $order the order object for which the email is sent
     * @return string $recipient the updated list of email recipients
     */
    function sv_conditional_email_recipient( $recipient, $order ) {
        // Bail on WC settings pages since the order object isn't yet set yet
        // Not sure why this is even a thing, but shikata ga nai
        $page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
        if ( 'wc-settings' === $page ) {
            return $recipient; 
        }
    
        // just in case
        if ( ! $order instanceof WC_Order ) {
            return $recipient; 
        }
        $items = $order->get_items();
    
        // check if a shipped product is in the order   
        foreach ( $items as $item ) {
            $product = $order->get_product_from_item( $item );
    
            // add our extra recipient if there's a shipped product - commas needed!
            // we can bail if we've found one, no need to add the recipient more than once
            if ( $product && $product->needs_shipping() ) {
                $recipient .= ', warehouse-manager@example.com';
                return $recipient;
            }
        }
    
        return $recipient;
    }
    add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );
    

    【讨论】:

    • 非常感谢!我更改了行 $recipient .= ', warehouse-manager@example.com';到 $recipient .= ', ' 。 $order->billing_email;现在新的订单电子邮件正在发送给客户。那是我的观点。再次感谢,干杯!
    • 我需要更改哪个文件?
    【解决方案2】:

    错误:

    $recipient .= ', ' . $order->billing_email
    

    正确:

    $recipient .= "," . $order->get_billing_email();
    

    我测试和调试了一整天。

    【讨论】:

      【解决方案3】:

      2018-2019 年更新 - 适用于 Woocommerce 3+

      接受的答案代码自 Woocommerce 3 以来已过时,代码中有一些错误。

      • get_product_from_item() 方法已被$item-&gt;get_product() 弃用并替换
      • 客户电子邮件丢失
      • 有不必要的代码(删除和替换)。

      这是一个类似的最新和干净的版本:

      add_filter( 'woocommerce_email_recipient_new_order', 'custom_new_order_email_recipient', 10, 2 );
      function custom_new_order_email_recipient( $recipient, $order ) {
          // Avoiding backend displayed error in Woocommerce email settings for undefined $order
          if ( ! is_a( $order, 'WC_Order' ) ) 
              return $recipient;
      
          // Check order items for a shipped product is in the order   
          foreach ( $order->get_items() as $item ) {
              $product = $item->get_product(); // Get WC_Product instance Object
      
              // When a product needs shipping we add the customer email to email recipients
              if ( $product->needs_shipping() ) {
                  return $recipient . ',' . $order->get_billing_email();
              }
          }
          return $recipient;
      }
      

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

      【讨论】:

      • 对于 WooCommerce 4+ 这似乎不再起作用。你知道为什么吗?
      • @KrystianManthey Nop,这周我会检查一下……
      猜你喜欢
      • 2017-08-19
      • 2020-06-03
      • 2016-10-26
      • 2014-03-09
      • 1970-01-01
      • 2018-10-24
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多