【发布时间】:2022-06-14 23:30:06
【问题描述】:
我在新的 woocommerce 订单后发送额外的邮件。
我正在使用 woo commerce_new_order 钩子。
问题是当电子邮件到达时它没有产品信息。我认为 woocommerce_new_order 钩子会在所有内容都存储在数据库中之前触发。因为如果我使用现有订单运行此程序,则会包含所有信息。
问题是如何在获取数据和发送电子邮件之前添加延迟?
add_action( 'woocommerce_new_order', 'extra_mail_after_new_order', 20, 1 );
function extra_mail_after_new_order( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
$product_variation_id = $item->get_variation_id();
$product_data = $product->get_meta('extra_email')
}
add_filter('wp_mail_content_type', function( $content_type ) {
return 'text/html';
});
$to = 'mail.mail@gmail.com';
$subject = $product_name . ' uusi tilaus!';
$message = 'Order id: '. $order_id . '<br />product name: '. $product_name . '<br />product id: '. $product_id. '<br />product meta: '. $product_data. '<br />status: '. $status ;
wp_mail( $to, $subject, $message ); }
【问题讨论】:
-
为什么要添加延迟,只需使用在所有内容都存储到数据库后触发的另一个挂钩
-
谢谢,你知道稍后会触发什么钩子吗?我觉得合适吗?
-
有几个。对于这样的问题,请在 WooCommerce 源中查找您当前的钩子,然后查看接下来运行哪些钩子。例如
do_action( 'woocommerce_checkout_order_created', $order )- 创建订单后触发的操作挂钩。 -
ps。在您当前的 foreach 循环中,每次都会覆盖值,因此只有循环中的最后一个产品($item)才会存储值
标签: wordpress woocommerce wait woocommerce-bookings add-action