Rumah  >  Soal Jawab  >  teks badan

Menyasarkan pemberitahuan e-mel WooCommerce tertentu apabila menggunakan cangkuk "woocommerce_order_item_meta_start".

Saya mempunyai ciri ini untuk menambah medan meta tersuai pada butiran produk dalam semua e-mel WooCommerce. Tetapi saya hanya memerlukannya untuk muncul selepas pesanan telah dibayar (ini juga boleh menjadi e-mel "selesai" sahaja).

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 );
        }
    }
}

Saya harap saya boleh meletakkannya di dalam if ( $email->id == 'customer_completed_order' ) {} supaya kod akhir akan kelihatan seperti ini:

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 );
            }
        }
    }
}

Tetapi selepas menukar ia berhenti berfungsi. Ada apa-apa cadangan?

P粉805922437P粉805922437259 hari yang lalu490

membalas semua(1)saya akan balas

  • P粉014293738

    P粉0142937382024-01-09 18:05:49

    Seperti yang anda lihat dalam kod cuba, $email 不是 woocommerce_order_item_meta_start bahagian cangkuk. Jadi, untuk menyasarkan pemberitahuan e-mel WooCommerce tertentu, anda memerlukan penyelesaian.

    Langkah 1) Buat dan tambah pembolehubah global melalui cangkuk lain yang hanya berfungsi untuk pemberitahuan e-mel 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 );
    

    Langkah 2) Dalam cangkuk woocommerce_order_item_meta_start, gunakan pembolehubah global supaya kami boleh menyasarkan pemberitahuan e-mel WooCommerce tertentu

    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 );

    balas
    0
  • Batalbalas