Ausrichtung auf bestimmte WooCommerce-E-Mail-Benachrichtigungen bei Verwendung des Hooks „woocommerce_order_item_meta_start'.
P粉805922437
P粉805922437 2024-01-08 19:37:47
0
1
491

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粉805922437
P粉805922437

Antworte allen(1)
P粉014293738

正如您在代码尝试中看到的,$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 );
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!