Ciblage de notifications par e-mail WooCommerce spécifiques lors de l'utilisation du hook "woocommerce_order_item_meta_start"
P粉805922437
P粉805922437 2024-01-08 19:37:47
0
1
492

J'ai cette fonctionnalité pour ajouter des champs méta personnalisés aux détails du produit dans tous les e-mails WooCommerce. Mais je n'en ai besoin qu'après le paiement de la commande (cela peut aussi être simplement un e-mail « complété »).

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

J'aimerais pouvoir l'imbriquer à l'intérieur if ( $email->id == 'customer_completed_order' ) {} pour que le code final ressemble à ceci :

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

Mais après l'avoir changé, il a cessé de fonctionner. Aucune suggestion?

P粉805922437
P粉805922437

répondre à tous(1)
P粉014293738

Comme vous pouvez le voir dans le code try, $email 不是 woocommerce_order_item_meta_start fait partie du hook. Ainsi, pour cibler certaines notifications par e-mail WooCommerce, vous avez besoin d'une solution de contournement.

Étape 1) Créez et ajoutez une variable globale via un autre hook qui ne fonctionne que pour les notifications par e-mail 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 );

Étape 2) Dans le hook woocommerce_order_item_meta_start, utilisez une variable globale afin que nous puissions cibler certaines notifications par e-mail 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 );
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!