SimonUK
SimonUK

Reputation: 13

WooCommerce custom general fields - adding to email

I have the following code in my theme functions.php to add a Supplier field to a product:

// Add supplier to products
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields_supplier' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_supplier_save' );
function woo_add_custom_general_fields_supplier() {
global $woocommerce, $post;
// Text Field
woocommerce_wp_text_input(
array(
'id' => 'supplier',
'label' => __( 'Supplier', 'woocommerce' ),
'placeholder' => 'supplier',
'desc_tip' => 'true',
'description' => __( 'Product supplier.', 'woocommerce' )
)
);
}
function woo_add_custom_general_fields_supplier_save( $post_id ){
// Textarea
$woocommerce_supplier = $_POST['supplier'];
if( !empty( $woocommerce_supplier ) )
update_post_meta( $post_id, 'supplier', esc_html( $woocommerce_supplier ) );
}

This all works great. I now need to add this new supplier field to the admin order email. I'm not very fluent in code so I tried the following that I found without any success:

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['supplier'] = array(
        'label' => __( 'Supplier' ),
        'value' => get_post_meta( $order->id, 'supplier', true ),
    );
    return $fields;
}

What code would I need to be able to add the supplier field to the admin email?

many thanks

Upvotes: 1

Views: 111

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

Some comments and adjustments

  1. In step 1 and 2 (creating the field and saving it, you make use of the $product_id. However, in step 3 you use the $order_id. $order_id is not equal to $product_id
  2. Use woocommerce_admin_process_product_object to save instead of the outdated woocommerce_process_product_meta hook
  3. woocommerce_email_order_meta_fields has been replaced with woocommerce_checkout_create_order_line_item

So you get

// Add supplier to products
// Display Fields
function woo_add_custom_general_fields_supplier() {
    // Text Field
    woocommerce_wp_text_input( array(
        'id' => 'supplier',
        'label' => __( 'Supplier', 'woocommerce' ),
        'placeholder' => 'supplier',
        'desc_tip' => 'true',
        'description' => __( 'Product supplier.', 'woocommerce' )
    ));
}
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields_supplier', 10, 0 );

// Save Fields
function woo_add_custom_general_fields_supplier_save( $product ){
    if( isset($_POST['supplier']) ) {
        $product->update_meta_data( 'supplier', esc_html( $_POST['supplier'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'woo_add_custom_general_fields_supplier_save', 10, 1 );  

// Displaying custom fields in the WooCommerce order and email confirmations
function action_woocommerce_checkout_create_order_line_item($item, $cart_item_key, $values, $order) {
    // Get meta
    $supplier = $values['data']->get_meta('supplier');

    // True
    if ( $supplier ) {
        $item->update_meta_data( 'supplier', $supplier );
    }
}
add_action('woocommerce_checkout_create_order_line_item', 'action_woocommerce_checkout_create_order_line_item', 20, 4);

Upvotes: 1

Related Questions