Home > Net >  Add field to Woocommerce emails and orders
Add field to Woocommerce emails and orders

Time:12-05

I've added a date picker to Woocommerce to allow customers to choose when they collect their orders from a store. By piecing together bits of code I've even managed to display the data in the back end so it can be seen on the order screen for admins.

I'm struggling however to get the data to display on Woocommerce emails or in the frontend orders of a client. Any suggestions?

The code I have for saving and displaying on the backend is as follows:

  
function showe_save_date_order( $order_id ) {
     
    global $woocommerce;
     
    if ( $_POST['collection_date'] ) update_post_meta( $order_id, '_collection_date', esc_attr( $_POST['collection_date'] ) );
     
}
  
add_action( 'woocommerce_admin_order_data_after_billing_address', 'showe_collection_weight_display_admin_order_meta' );
   
function showe_collection_weight_display_admin_order_meta( $order ) {    
     
   echo '<p><strong>Collection Date:</strong> ' . get_post_meta( $order->get_id(), '_collection_date', true ) . '</p>';
     
}'''

CodePudding user response:

You can use woocommerce_email_order_meta action hook. try the below code.

add_action( 'woocommerce_email_order_meta', 'add_custom_order_meta_to_email', 10, 3 );
function add_custom_order_meta_to_email( $order, $sent_to_admin, $plain_text ){

    $collection_date = get_post_meta( $order->get_order_number(), '_collection_date', true );
    
    // Don't display anything if it is empty
    if( empty( $collection_date ) )
        return;
    
    if ( $plain_text === false ) {
        echo '<ul>
                <li><strong>Collection Date: </strong>' . $collection_date . '</li>
            </ul>';
    } else {
        echo "Collection Date: ". $collection_date; 
    }
    
}
  • Related