• Resolved Penelope

    (@penelopehathaway)


    Hey dear,

    I am using this function to change the product name but only need it for order emails sent to admin:

    // Add filter to modify order item names
    add_filter(‘woocommerce_order_item_name’, ‘custom_order_item_name’, 10, 3);

    function custom_order_item_name($item_name, $item, $is_visible) {
    $product_id = $item->get_product_id();

    if (has_term(146, 'product_cat', $product_id)){
    $item_name .= '<div class="adminnote"><strong>KLEINE FLASCHE</strong></div>'; // Append text
    }

    return $item_name;

    }

    I have tried hundreds of versions and hooks (not in the above code) to include this filter only in the admin new order email and not in any other emails and can’t make it work.

    Can you please assist.
    Kind regards,
    Penelope

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support lavendervu2510

    (@lavendervu2510)

    Hi,

    To change the order item name (woocommerce_order_item_name) only in the email sent to the admin, you can use the woocommerce_order_item_name hook and check if the current email is the admin email. Here is a code snippet to do this. You can add the below code snippet in your child theme or a code snippet plugin, see this guide.

    // Hook into the filter to modify the order item name
    add_filter('woocommerce_order_item_name', 'custom_order_item_name_for_admin_email', 10, 2);
    
    function custom_order_item_name_for_admin_email($item_name, $item) {
        // Check if the current email is the admin email
        if (is_admin_email()) {
            // Modify the order item name for admin email
            $item_name .= ' (Custom for Admin)';
        }
    
        return $item_name;
    }
    
    // Check if the current email is the admin email
    function is_admin_email() {
        // Get the current email object
        $current_email = WC()->mailer()->get_emails();
    
        // Check if the current email is the admin email
        if (isset($current_email['WC_Email_New_Order'])) {
            return true;
        }
    
        return false;
    }

    Explanation

    1. Hook woocommerce_order_item_name: Used to modify the name of the order item.
    2. Function custom_order_item_name_for_admin_email: Modifies the order item name if the current email is the admin email.
    3. Function is_admin_email: Checks if the current email is the admin email by examining the current email object in WooCommerce.

    Notes

    • The above code is a simple example. You can change the part that adds “(Custom for Admin)” to fit your specific needs.
    • To apply this code, add it to your theme’s functions.php file or a custom plugin.

    Please review the above code to ensure it works correctly with your current versions of WooCommerce and WordPress. If there are any issues, check the logic or provide more information so I can assist further.

    Best regards.

    Thread Starter Penelope

    (@penelopehathaway)

    Thank you for your support. It results also in the name change on both emails: admin new order & customer order confirmation.
    I used this code:

    add_filter('woocommerce_order_item_name', 'custom_order_item_name_for_admin_email', 10, 2);

    function custom_order_item_name_for_admin_email($item_name, $item) {

        $product_id = $item->get_product_id();

        // Check if the current email is the admin email

        if (is_admin_email() && has_term(146, 'product_cat', $product_id)){

            // Modify the order item name for admin email

            $item_name .= ' <strong>Single Sortiment</strong>';

        }

        return $item_name;

    }

    // Check if the current email is the admin email

    function is_admin_email() {

        // Get the current email object

        $current_email = WC()->mailer()->get_emails();

        // Check if the current email is the admin email

        if (isset($current_email['WC_Email_New_Order'])) {

            return true;

        }

        return false;

    }

    And I need this name change only in the admin new order email….can you adjust please?
    Thank you so much in advance.

    KInd regards,
    Penelope

    • This reply was modified 4 months, 1 week ago by Penelope.
    Plugin Support lavendervu2510

    (@lavendervu2510)

    Hi,

    Thank you for responding.

    We regret that we cannot offer a deeper suggestion on this matter. Please understand that supporting custom code is beyond the scope of our services.

    Best regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.