实现的结果:在客户下新订单时,将订单相关信息邮件发送到woocommerce电子邮件字段值的客户电子邮件中。即:客户下订单时为其发送一封确认邮件。
实现代码:
元数据不需要自定义 SQL 查询,您可以使用 get_meta
function action_woocommerce_new_order( $order_id ) { // 获取woocommerce订单 $order = wc_get_order( $order_id ); // 判断是否时WC【woocommerce】订单 if ( is_a( $order, 'WC_Order' ) ) { // 获取元数据 $gift_to_person_email = $order->get_meta( 'gift_to_person_email' ); $gift_to_organization_email = $order->get_meta( 'gift_to_organization_email' ); // 初始化 $to = ''; // 值不为空 if( ! empty( $gift_to_person_email ) ) { $to = $gift_to_person_email; } elseif( ! empty( $gift_to_organization_email ) ) { $to = $gift_to_organization_email; } // 获取账单名称 $name = $order->get_billing_first_name(); // 判断不为空 if( ! empty ( $to ) ) { $to = $to; $subject = 'The subject'; $email_content = 'hello'; $headers = array( 'Content-Type: text/html; charset=UTF-8' ); wp_mail( $to, $subject, $email_content, $headers ); } else { $to = 'test@gmail.com'; $subject = 'Email Failed'; $email_content = 'Failed'; $email_content .= $gift_to_person_email; $email_content .= $gift_to_organization_email; $headers = array( 'Content-Type: text/html; charset=UTF-8' ); wp_mail( $to, $subject, $email_content, $headers ); } } } // 添加动作 add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
注意,上述代码中邮件字段为自定义值,如没有配置,需要进行配置。
本文链接:http://78moban.cn/post/14401.html
版权声明:站内所有文章皆来自网络转载,只供模板演示使用,并无任何其它意义!