skip to Main Content

I need add a custom data on Processing order email, but the data always update after the email is sent, like this:

Order status change ==> Send email ==> Insert data on custom table (plugin)

What I need instead is:

Order status change ==> Insert data on custom table (plugin) ==> Send email.

I have checked and this is done with the following hooked function:

add_action('woocommerce_order_status_changed', 'fun_order_status_changed', 10, 4);
function fun_order_status_changed($order_id, $from_status, $to_status, $order){

    // Some code

    // Then insert to database
}

How could I do or what files can I need to modify so that first the insert is saved in the database and then the e-mail is sent?

EDIT 1

I put deliberately a var_dump and first execute the mail templeateenter image description here

2

Answers


  1. You can try to use woocommerce_order_status_pending_to_processing_notification action hook with a lower priority, for example 5. So that it will get processed before the mail is sent.

    Login or Signup to reply.
  2. If you want to add a custom field to the order meta data, send the value of this field with the order confirmation mail, and additionally display it in the order detail and edit screen in the backend, you can use the follwing code. There are multiple steps to be done.

    1. Create a new field to show up in WooCommerce Checkout. Set it as required if you want to make sure, that there is a value entered. For this we are using 'woocommerce_after_checkout_billing_form'. (just a sidenote: If you have other purposes, you can also i.e. use a hidden field and a given value)
    2. Save the value inside order meta data using 'woocommerce_checkout_update_order_meta'
    3. Add the value to the email being send after completing order using 'woocommerce_email_order_meta_keys'
    4. Show the value in the order detail screen in the backend using 'woocommerce_order_details_after_order_table' and for the order edit screen 'woocommerce_admin_order_data_after_billing_address' This will place it below the billing address. Notice: The value will not show up (but still be saved in database), if the order is made within the backend, only works for orders placed in the frontend (Would go beyond the scope now).

    In my code example, I did this steps to add a VAT ID field which is important in europe for business to business transactions. The VAT ID is also added to the emails and backend screens.

    You can adjust the names (vat_number, or the "mrank" prefixes) to your needs, but remember to keep it consistent.

    /**
    * VAT Number in WooCommerce Checkout
    */
    function mrank_vat_field( $checkout ) {
    
          echo '<div id="mrank_vat_field">';
       
          woocommerce_form_field( 'vat_number', array(
                'type'          => 'text',
                'class'         => array( 'vat-number-field form-row-wide') ,
                'label'         => __( 'VAT-ID' ),
                'placeholder'   => __( 'Enter number' ),
                'description' => __( 'Please enter your VAT-ID' ),
                'required'    => true,
          ), $checkout->get_value( 'vat_number' ));
        
        echo '</div>';
    }
    add_action( 'woocommerce_after_checkout_billing_form', 'mrank_vat_field' );
    
    
    /**
    * Save VAT Number in the order meta
    */
    function mrank_checkout_vat_number_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['vat_number'] ) ) {
            update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
        }
    }
    add_action( 'woocommerce_checkout_update_order_meta', 'mrank_checkout_vat_number_update_order_meta' );
    
    
    /**
     * Display VAT Number in order details screen
     */
    function mrank_vat_number_display_order_details($order){
        echo '<p><strong>'.__('VAT-ID').':</strong> ' . get_post_meta( $order->get_id(), '_vat_number', true ) . '</p>';
    }
    add_action( 'woocommerce_order_details_after_order_table', 'mrank_vat_number_display_order_details', 10, 1 );
    
    
    /**
     * Display VAT Number in order edit screen
     */
    function mrank_vat_number_display_admin_order_meta( $order ) {
        echo '<p><strong>' . __( 'VAT-ID', 'woocommerce' ) . ':</strong> ' . get_post_meta( $order->get_id(), '_vat_number', true ) . '</p>';
    }
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'mrank_vat_number_display_admin_order_meta', 10, 1 );
    
    
    /**
    * VAT Number in emails
    */
    function mrank_vat_number_display_email( $keys ) {
         $keys['VAT-ID'] = '_vat_number';
         return $keys;
    }
    add_filter( 'woocommerce_email_order_meta_keys', 'mrank_vat_number_display_email' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search