P粉7369355872023-08-16 10:40:06
WooCommerce already has a bunch of hooks that you can use in WooCommerce templates instead of adding your own...
A good development rule is to use existing hooks first. If there is no hook convenient or available, then you can override the WooCommerce template via a child theme. Why? Because templates are updated sometimes and then you need to update the edited template, whereas hooks don't.
For the "Customer Completed Order" notification, use the woocommerce_order_item_meta_end
action hook like this:
// 将email_id设置为全局变量 add_action('woocommerce_email_before_order_table', 'set_email_id_as_a_global', 1, 4); function set_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email = null ){ if ( $email ) { $GLOBALS['email_id_str'] = $email->id; } } // 在“客户完成订单”电子邮件通知中显示自定义订单项元数据 add_action( 'woocommerce_order_item_meta_end', 'custom_email_order_item_meta', 10, 2 ); function custom_email_order_item_meta( $item_id, $item ){ // 获取email ID全局变量 $refGlobalsVar = $GLOBALS; $email_id = isset($refGlobalsVar['email_id_str']) ? $refGlobalsVar['email_id_str'] : null; // 仅适用于“客户完成订单”电子邮件通知 if ( ! empty($email_id) && 'customer_completed_order' === $email_id ) { bis_show_kit_meta_contents( $item->get_product_id() ); } }
This will allow you to display custom metadata only in the "Customer Completed Order" notification.
Alternatively, you can replace the hook with woocommerce_order_item_meta_start
with the same function variable parameters.
Place the code in your child theme’s functions.php file or in a plugin.