我正在尝试使用 PHP 为 Woocommerce 按下结账按钮时发送自定义电子邮件。
此电子邮件将与 wooCommerce 的电子邮件通知一起发送。
我用过这个answer https://stackoverflow.com/questions/32512552/woocommerce-execute-php-per-product-on-successful-checkout#32512745,并编辑代码如下:
//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] /cdn-cgi/l/email-protection","My subject",$msg);
}
}
}
但似乎什么也没有发生。有任何想法吗?
Thanks
这不起作用,因为这个钩子是仅当订单状态完成时触发 …
也比较好用wp_mail()
than 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] /cdn-cgi/l/email-protection";
$subject = "the subject here";
$content = "Here goes your message";
// Sending your custom email notification
wp_mail( $to, $subject, $content );
}
代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
此代码在 WooCommerce 3+ 上经过测试并且有效。
The woocommerce_thankyou
钩子在订单接收页面被触发...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)