PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 13, 2022

[FIXED] Display a custom field in WooCommerce email notifications for admin

 March 13, 2022     custom-fields, email-notifications, php, woocommerce, wordpress     No comments   

Issue

I installed Checkout Field Editor on my wordpress site. I created the custom field. However, using the following code, the pcustomer field appears both in the "new order" email that arrives to me and to the customer. Instead I want it to come exclusively to me. I tried to edit the code, but it still doesn't work.

add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $fields['meta_key'] = array(
        'label' => __( 'Label' ),
        'value' => get_post_meta( $order->id, 'meta_key', true ),
    );
    return $fields;
}

Solution

Since WooCommerce 3, your code is a bit outdated, with some mistakes Try the following:

add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    $meta_key = '_meta_key1'; // <= Here define the correct meta key
    $meta_value = $order->get_meta( $meta_key1 );
    
    if ( ! empty( $meta_value1 ) ) {
        $fields[ $meta_key1 ] = array(
            'label' => __( 'My label 1', "text-domain' ),
            'value' => $meta_value1,
        );
    }
    return $fields;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.


Now to restrict the code only to admin email notifications, you will use $sent_to_admin argument variable as follows:

add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
    if ( $sent_to_admin ) {
        $meta_key1 = '_meta_key1'; // <= Here define the correct meta key
        $meta_value1 = $order->get_meta( $meta_key1 );
        
        if ( ! empty( $meta_value1 ) ) {
            $fields[ $meta_key1 ] = array(
                'label' => __( 'My label 1', "text-domain' ),
                'value' => $meta_value1,
            );
        }
    }
    return $fields;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

This time this custom field will not be displayed on customer email notifications.

Note:

  • $order->idis uncorrect since WooCommerce 3. use instead $order->get_id().
  • WordPress get_post_meta() function can be replace by WooCommerce WC_Data get_meta() method.


Answered By - LoicTheAztec
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing