wordpress - Woocommerce:订单商品未显示在订单电子邮件中

标签 wordpress woocommerce

我所有的订单都是用 on-hold 创建的默认状态,直到确认付款,然后状态变为processing .我创建了一个 Hook 来发送 on-hold每次创建新订单时发送电子邮件,以确保每个用户在创建订单后都会收到电子邮件。

//Force on-hold email notification bug workaround
add_action('woocommerce_new_order', 'new_order_on_hold_notification', 30, 1 );
function new_order_on_hold_notification( $order_id ) {
    //global $woocommerce;
    //$mailer = $woocommerce->mailer();
    $order = wc_get_order( $order_id );

    // Send Customer On-Hold Order notification
    WC()->mailer()->get_emails()['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
}

电子邮件正在发送,但由于某种原因,订单项目未包含在电子邮件中,这很奇怪,因为 woocommerce 发送的其余电子邮件(在订单完成、已发货和已交付状态)与订单项目一起正确发送。

更新
根据要求,我附上了 customer-on-hold-order.php我在我的主题中使用的模板,它与默认的 woocommerce 模板基本相同,只是在电子邮件顶部添加了一个图像。
<?php
/**
 * Customer on-hold order email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-on-hold-order.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates/Emails
 * @version 3.7.0
 */

defined( 'ABSPATH' ) || exit;

/*
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<!--[if gte MSO 9]>
  <table width="640">
     <tr>
        <td>
<![endif]-->
  <table width="100%" style="max-width:640px;">
    <tr>
      <td>
        <img src="https://ebani.com.co/wp-content/themes/martfury-child/woocommerce/emails/images/pedido-en-espera.jpg" width="100%" />
      </td>
    </tr>
  </table>
<!--[if gte MSO 9]>
      </td>
    </tr>
  </table>
<![endif]-->

<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $order->get_billing_first_name() ) ); ?></p>
<p><?php esc_html_e( 'Thanks for your order. It’s on-hold until we confirm that payment has been received. In the meantime, here’s a reminder of what you ordered:', 'woocommerce' ); ?></p>

<?php

/*
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
 * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
 * @since 2.5.0
 */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::order_meta() Shows order meta data.
 */
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::customer_details() Shows customer details
 * @hooked WC_Emails::email_address() Shows email address
 */
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

/**
 * Show user-defined additional content - this is set in each email's settings.
 */
if ( $additional_content ) {
    echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}

/*
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );

更新 #2
调试后email-order-items.php模板我发现 $items数组是空的,但这很奇怪,因为我告诉过您所有其他电子邮件对于订单项都是正确的。

最佳答案

终于,我找到了问题的根源,感谢 question ,似乎当woocommerce_new_order 被称为订单尚未完全填充,这就是为什么在通知电子邮件中订单项目数组为空的原因。使用 woocommerce_checkout_order_processed反而成功了!

add_action('woocommerce_checkout_order_processed', 'new_order_on_hold_notification');   
    function new_order_on_hold_notification( $order_id ) {
                    $order = wc_get_order( $order_id );

                    // Only for on hold new orders
                    if( ! $order->has_status('on-hold') )
                        return; // Exit

                    // Send Customer On-Hold Order notification
                    WC()->mailer()->get_emails()['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
     }

关于wordpress - Woocommerce:订单商品未显示在订单电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60232559/

相关文章:

php - 自定义结账字段启用或禁用 Woocommerce 3 中的付款方式

php - 如果在 WooCommerce Checkout 中选中自定义复选框,则删除运费

WordPress 需要 FTP 凭据来更新插件

php - 仅在WooCommerce中获取当前产品的产品标签

wordpress - 允许管理员在某些情况下使用 ACF 绕过必填字段?

php - 在 WooCommerce 订单和电子邮件通知上显示自定义送货通知

php - 点击 "Place Order"后转换货币

wordpress - WooCommerce 法典 : How to create an order from an existing cart (wc_create_order)

wordpress - 将WordPress切换为https后重定向过多

php - 如何获取wordpress帖子的描述和标题?