2017-07-23 100 views
1

我试图发送一个自定义电子邮件,只要使用PHP的Woocommerce按结账按钮。按WooCommerce结账按钮时发送自定义电子邮件

此电子邮件将与wooCommerce的电子邮件通知一起发送。 我已经使用这个answer,和编辑一样代码:

//execute some php on successfull checkout 
add_action('woocommerce_payment_complete', 'so_32512552_payment_complete'); 
function so_32512552_payment_complete($order_id){ 
    $order = wc_get_order($order_id); 

    foreach ($order->get_items() as $item) { 

     if ($item['product_id'] > 0) { 
      $_product = $order->get_product_from_item($item); 

      // the message 
      $msg = "First line of text\nSecond line of text"; 

      // use wordwrap() if lines are longer than 70 characters 
      $msg = wordwrap($msg,70); 

      // send email 
      mail("[email protected]","My subject",$msg); 


     } 
    } 
} 

,但似乎没有发生。有任何想法吗?

感谢

回答

0

这不起作用,因为这个钩子当订单状态为完成只烧...
也最好使用wp_mail()mail()功能。

相反,你可以尝试使用woocommerce_thankyou行动钩勾住了自定义函数:

add_action('woocommerce_thankyou', 'custom_email_notification', 10, 1); 
function custom_email_notification($order_id) { 

    if (! $order_id) return; 

    ## THE ORDER DATA ## 

    // Get an instance of the WC_Order object 
    $order = wc_get_order($order_id); 

    // Iterating through each order items 
    foreach ($order->get_items() as $item_id => $order_item) { 

     // Accessing to the protected data of the WC_Order_Item_Product object 
     $order_item_data = $order_item->get_data(); 

     // Get the associated WC_Product object 
     $product = $order_item->get_product(); 

     // Accessing to the WC_Product object protected data 
     $product_data = $product->get_data(); 
    } 


    ## SENDING AN EMAIL (outside the loop is better to send it once) ## 

    $to = "[email protected]"; 
    $subject = "the subject here"; 
    $content = "Here goes your message"; 

    // Sending your custom email notification 
    wp_mail($to, $subject, $content); 
} 

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码在WooCommerce 3+上测试并正常工作。

woocommerce_thankyou钩,以便接收到的网页触发...

+0

感谢这么多的非常详细的和有用的代码。我只是试着用我的电子邮件地址替换[email protected],但我没有收到任何不幸的电子邮件。感谢页面已达成。 – xbass540

+0

@ xbass540你好,这段代码已经过测试,完全可以正常工作......所以你的托管与你的Wordpress安装相关的外发电子邮件存在问题...... – LoicTheAztec

+0

我想过,但目前默认的woocommerce邮件没有问题地发送。我应该在服务器设置中更改什么设置? – xbass540

相关问题