Heim > Fragen und Antworten > Hauptteil
Ich habe diese Funktion, um benutzerdefinierte Metafelder zu Produktdetails in allen WooCommerce-E-Mails hinzuzufügen. Ich brauche es aber erst, wenn es nach der Bezahlung der Bestellung erscheint (das kann auch nur eine „abgeschlossene“ E-Mail sein).
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 ); function email_confirmation_display_order_items( $item_id, $item, $order ) { // On email notifications for line items if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) { $ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true ); if ( ! empty($ot_address) ) { printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address ); } } }
Ich wünschte, ich könnte es darin verschachteln if ( $email->id == 'customer_completed_order' ) {}
, sodass der endgültige Code so aussehen würde:
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 ); function email_confirmation_display_order_items( $item_id, $item, $order ) { if ( $email->id == 'customer_completed_order' ) { // On email notifications for line items if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) { $ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true ); if ( ! empty($ot_address) ) { printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address ); } } } }
Aber nach dem Ändern funktionierte es nicht mehr. Irgendwelche Vorschläge?
P粉0142937382024-01-09 18:05:49
正如您在代码尝试中看到的,$email
不是 woocommerce_order_item_meta_start
挂钩的一部分。因此,要定位某些 WooCommerce 电子邮件通知,您需要一种解决方法。
步骤 1) 通过另一个仅适用于 WooCommerce 电子邮件通知的挂钩创建并添加全局变量。
// Setting global variable function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) { $GLOBALS['email_id'] = $email->id; } add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
步骤 2) 在挂钩 woocommerce_order_item_meta_start
中,使用全局变量,以便我们可以定位某些 WooCommerce 电子邮件通知
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) { // On email notifications for line items if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) { // Getting the email ID global variable $ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : ''; $email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : ''; // NOT empty and targeting specific email. Multiple statuses can be added, separated by a comma if ( ! empty ( $email_id ) && in_array( $email_id, array( 'new_order', 'customer_completed_order' ) ) ) { // Get meta $ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true ); // OR use to get meta // $ot_address = $item->get_meta( 'ot_address' ); if ( ! empty( $ot_address ) ) { printf( '' . __( 'Terms: %s', 'woocommerce' ) . '', $ot_address ); } } } } add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );