skip to Main Content

After an update of WooCommerce & WordPress a lot of custom settings was overwritten so I have tried to get back the same functionality as in the older version. (using childtheme so why it went missing in the first place I dont get)
Fixed everything except this. On the billing info at checkout several fields went missing, the default company_name I got working again , for some reason it was deactivated in the theme functions.php. However two custom fields are left for IVA number and Organization number.

So I used Checkout Manager for WooCommerce to add custom fields to the billing info.
It works on the check out page and the info end up on the order. But it doesn’t show up on the thank you page and more importantly it doesn’t show up on the email to customer.

Tried adding this to themes functions.php but no luck.

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

And also tried this:

add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {

    $output = '';
    $billing_field_testing = get_post_meta( $order->id, 'billing_wooccm13', true );

    if ( !empty($billing_wooccm13) )
        $output .= '<div><strong>' . __( "Some text:", "woocommerce" ) . '</strong> <span class="text">' . $billing_wooccm13 . '</span></div>';

    echo $output;
}

Any ideas how to go about this?

2

Answers


  1. Chosen as BEST ANSWER

    Thank you so much LoicTheAztec.

    Both solutions worked but with the first one the text ended up to high up, before invoice address. The second one ended up below , unfortunately with some blank lines before but no biggie. Assume I have to use an email-template to remove those lines. The code I ended up with:

    
    add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
    function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
        if ( $value  = $order->get_meta( '_billing_wooccm11' ) ) {
            echo '<div><strong>' . __( "Ert organisationsnummer", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';
    
        if ( $value  = $order->get_meta( '_billing_wooccm13' ) ) {
            echo '<div><strong>' . __( "Ert Moms/IVA:", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';
        }
        }
    }
    
    

  2. Since WoooCommerce 3 $order->id is replaced by $order->get_id()… Also there are some other ways.

    Be sure that the meta key for your custom field is billing_wooccm13 (as it could be instead starting with an underscore like _billing_wooccm13).

    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 ) {
        if ( $value  = $order->get_meta( 'billing_wooccm13' ) ) {
            $fields['billing_wooccm13'] = array(
                'label' => __( 'Moms/IVA' ),
                'value' => $value,
            );
        }
        return $fields;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should work.


    Or this too:

    add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
    function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
        if ( $value  = $order->get_meta( 'billing_wooccm13' ) ) {
            echo '<div><strong>' . __( "Some text:", "woocommerce" ) . '</strong> <span class="text">' . $value . '</span></div>';
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search