Fügen Sie benutzerdefinierten Text „pro Artikel“ basierend auf der Produktkategorie in WooCommerce-E-Mail-Benachrichtigungen hinzu

Lesezeit: 3 Minuten

Benutzer-Avatar
Brendan Friedrich

Wie füge ich einen benutzerdefinierten Text „pro Artikel“ basierend auf der Produktkategorie in allen WooCommerce-E-Mails hinzu?

Daher habe ich versucht, einen benutzerdefinierten Text für ein bestimmtes Produkt bei einer bestimmten E-Mail-Benachrichtigung in WooCommerce hinzuzufügen und einen benutzerdefinierten Text basierend auf der Produktkategorie im WooCommerce-E-Mail-Antwortcode an meine Bedürfnisse anzupassen.

// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
    $GLOBALS['email_id_str'] = $email->id;
}

// Displaying product description in new email notifications
add_action( 'woocommerce_order_item_meta_end', 'product_description_in_new_email_notification', 10, 3 );
function product_description_in_new_email_notification( $product_cat, $cat, $order = null ){
    // HERE define your targetted product CAT
    $targeted_id = 'x';

    // HERE define the text information to be displayed for the targeted product id
    $text_information = __("There is an offer in this particular item", "woocommerce");

    // Getting the email ID global variable
    $refNameGlobalsVar = $GLOBALS;
    $email_id = $refNameGlobalsVar['email_id_str'];

    // If empty email ID we exit
    if(empty($email_id)) return;

    // Only for "New Order email notification" for your targeted product ID
    if ( 'customer_completed_order' == $email_id && 'new_order' == $email_id 
        in_array( $targeted_id, array( $cat->get_product_cat(), $item->get_variation_id() ) ) ) {

        // Display the text
        echo '<div class="product-text" style="margin-top:10px"><p>' . $text_information . '</p></div>';
    }
}

Leider ohne das gewünschte Ergebnis. Ich möchte benutzerdefinierten Text unter einem Bestellartikelnamen für alle Produkte einer bestimmten Produktkategorie in allen E-Mail-Benachrichtigungen anzeigen (dies gilt pro Artikel, nicht pro E-Mail). Irgendein Rat?

Benutzer-Avatar
7uc1f3r

Einige Anmerkungen zu Ihrem Codeversuch:

  • $product_cat und $cat existieren nicht als Argumente für die woocommerce_order_item_meta_end Haken
  • $cat->get_product_cat() existiert nicht und ist falsch
  • Verwenden has_term() WordPress-Funktion, um zu überprüfen, ob der aktuelle Beitrag einen der angegebenen Begriffe enthält

Sie erhalten also:

// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
    $GLOBALS['email_data'] = array(
        'email_id'  => $email->id, // The email ID (to target specific email notification)
        'is_email'  => true // When it concerns a WooCommerce email notification
    );
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );

// Displaying description
function action_woocommerce_order_item_meta_end( $item_id, $item, $order, $plain_text ) {
    // Getting the custom 'email_data' global variable
    $ref_name_globals_var = $GLOBALS;
    
    // Isset & NOT empty
    if ( isset ( $ref_name_globals_var ) && ! empty( $ref_name_globals_var ) ) {
        // Isset
        $email_data = isset( $ref_name_globals_var['email_data'] ) ? $ref_name_globals_var['email_data'] : '';
        
        // NOT empty
        if ( ! empty( $email_data ) ) {
            // Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
            $categories = array( 108, 1, 'categorie-1' );

            // The text information
            $text_information = __( 'There is an offer in this particular item', 'woocommerce' );

            // Specific email notifications: multiple statuses can be added, separated by a comma
            $email_ids = array( 'new_order', 'customer_processing_order', 'customer_completed_order', 'customer_on_hold_order' );

            // Targeting specific email notifications AND check if the current post has any of given terms
            if ( in_array( $email_data['email_id'], $email_ids ) && has_term( $categories, 'product_cat', $item->get_product_id() ) ) {
                // Display the text
                echo '<p>' . $text_information . '</p>';
            }
        }
    }
}
add_action( 'woocommerce_order_item_meta_end', 'action_woocommerce_order_item_meta_end', 10, 4 );

  • Perfekte Antwort! Ich danke Ihnen für Ihre Worte. Ich bin ein absoluter Programmieranfänger – werde weiter lernen.

    – Brendan Friedrich

    29. April um 22:27 Uhr

1017800cookie-checkFügen Sie benutzerdefinierten Text „pro Artikel“ basierend auf der Produktkategorie in WooCommerce-E-Mail-Benachrichtigungen hinzu

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy