wordpress – Add new rows with custom checkout fields to order totals in Woocommerce new order email

On my website I’m using Checkout Fields Manager plugin, where I have made some custom fields at checkout – billing section. The fields are: „Czy na fakturę?”, „Nazwa firmy”, „NIP”. They mean consecutively „Do you want an invoice?”, „Company name” and „Tax number”. screenshot1

I want to display the custom fields „Czy na fakturę?”, „Nazwa firmy” and „NIP“, whenever someone fills in those fields and make them appear in order email (for admin) in the totals table. But they should appear only when someone fills in these fields, otherwise not.

For now I have a code by the plugin creator in functions.php, which adds all the additional billing fields to the email sent to admin. However, this code shows everything – all of the billing details typed by customer. But I only want those 3 fields: Czy na fakturę?, Nazwa firmy and NIP added to the totals table. This code shows the fields below the totals table. Here is the code:

/**
 * Plugin Name: WooCommerce Checkout Manager Email Fix
 * Description: Manages WooCommerce Checkout, the advanced way.
 * Version:     1.0.0
 * Author:      QuadLayers
 * Author URI:  https://www.quadlayers.com
 */
add_action('woocommerce_email_after_order_table', 'wooccm_order_receipt_checkout_details', 10, 3);

function wooccm_order_receipt_checkout_details($order, $sent_to_admin, $plain_text="") {

  if (class_exists('WOOCCM')) {

    $billing_defaults = WOOCCM()->billing->get_defaults();
    $shipping_defaults = WOOCCM()->shipping->get_defaults();
    $additional_defaults = WOOCCM()->additional->get_defaults();

    $billing = WOOCCM()->billing->get_fields();
    $shipping = WOOCCM()->shipping->get_fields();
    $additional = WOOCCM()->additional->get_fields();

    if (!empty($billing)) {

      foreach ($billing as $btn) {

        if (!in_array($btn['name'], $billing_defaults)) {

          if (( get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true) !== '' ) &&
                  !empty($btn['label']) &&
                  $btn['type'] !== 'heading' &&
                  $btn['type'] !== 'multiselect' &&
                  $btn['type'] !== 'file' &&
                  $btn['type'] !== 'multicheckbox'
          ) {
            echo $btn['label'] . ': ' . nl2br(get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true));
            echo "n";
          } elseif (
                  !empty($btn['label']) &&
                  $btn['type'] == 'heading' &&
                  $btn['type'] !== 'multiselect' &&
                  $btn['type'] !== 'file' &&
                  $btn['type'] !== 'multicheckbox'
          ) {
            echo $btn['label'];
            echo "n";
          } elseif (
                  ( get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true) !== '' ) &&
                  !empty($btn['label']) &&
                  $btn['type'] !== 'heading' &&
                  $btn['type'] !== 'file' &&
                  (
                  $btn['type'] == 'multiselect' || $btn['type'] == 'multicheckbox'
                  )
          ) {
            $value = get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true);
            $strings = maybe_unserialize($value);
            echo $btn['label'] . ': ';
            if (!empty($strings)) {
              if (is_array($strings)) {
                $iww = 0;
                $len = count($strings);
                foreach ($strings as $key) {
                  if ($iww == $len - 1) {
                    echo $key;
                  } else {
                    echo $key . ', ';
                  }
                  $iww++;
                }
              } else {
                echo $strings;
              }
            } else {
              echo '-';
            }
            echo "n";
          }
        }
      }
    }

    if (!empty($shipping)) {

      foreach ($shipping as $btn) {

        if (!in_array($btn['name'], $shipping_defaults)) {

          if (( get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true) !== '' ) &&
                  !empty($btn['label']) &&
                  $btn['type'] !== 'heading' &&
                  $btn['type'] !== 'multiselect' &&
                  $btn['type'] !== 'file' &&
                  $btn['type'] !== 'multicheckbox'
          ) {
            echo $btn['label'] . ': ' . nl2br(get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true));
            echo "n";
          } elseif (
                  !empty($btn['label']) &&
                  $btn['type'] == 'heading' &&
                  $btn['type'] !== 'multiselect' &&
                  $btn['type'] !== 'file' &&
                  $btn['type'] !== 'multicheckbox'
          ) {
            echo $btn['label'];
            echo "n";
          } elseif (
                  ( get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true) !== '' ) &&
                  !empty($btn['label']) &&
                  $btn['type'] !== 'heading' &&
                  $btn['type'] !== 'file' &&
                  (
                  $btn['type'] == 'multiselect' || $btn['type'] == 'multicheckbox'
                  )
          ) {
            $value = get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true);
            $strings = maybe_unserialize($value);
            echo $btn['label'] . ': ';
            if (!empty($strings)) {
              if (is_array($strings)) {
                $iww = 0;
                $len = count($strings);
                foreach ($strings as $key) {
                  if ($iww == $len - 1) {
                    echo $key;
                  } else {
                    echo $key . ', ';
                  }
                  $iww++;
                }
              } else {
                echo $strings;
              }
            } else {
              echo '-';
            }
            echo "n";
          }
        }
      }
    }
    
    if (!empty($additional)) {

      foreach ($additional as $btn) {

        if (!in_array($btn['name'], $additional_defaults)) {

          if (( get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true) !== '' ) &&
                  !empty($btn['label']) &&
                  $btn['type'] !== 'heading' &&
                  $btn['type'] !== 'multiselect' &&
                  $btn['type'] !== 'file' &&
                  $btn['type'] !== 'multicheckbox'
          ) {
            echo $btn['label'] . ': ' . nl2br(get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true));
            echo "n";
          } elseif (
                  !empty($btn['label']) &&
                  $btn['type'] == 'heading' &&
                  $btn['type'] !== 'multiselect' &&
                  $btn['type'] !== 'file' &&
                  $btn['type'] !== 'multicheckbox'
          ) {
            echo $btn['label'];
            echo "n";
          } elseif (
                  ( get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true) !== '' ) &&
                  !empty($btn['label']) &&
                  $btn['type'] !== 'heading' &&
                  $btn['type'] !== 'file' &&
                  (
                  $btn['type'] == 'multiselect' || $btn['type'] == 'multicheckbox'
                  )
          ) {
            $value = get_post_meta($order->get_id(), sprintf('_%s', $btn['key']), true);
            $strings = maybe_unserialize($value);
            echo $btn['label'] . ': ';
            if (!empty($strings)) {
              if (is_array($strings)) {
                $iww = 0;
                $len = count($strings);
                foreach ($strings as $key) {
                  if ($iww == $len - 1) {
                    echo $key;
                  } else {
                    echo $key . ', ';
                  }
                  $iww++;
                }
              } else {
                echo $strings;
              }
            } else {
              echo '-';
            }
            echo "n";
          }
        }
      }
    }
  }
}

And the current new order email to admin looks like this:

screenshot2

So I found a code which lets me add a custom field to the totals table, but it doesn’t work when I want to show my custom fields. It can show only static value which I input, not a dynamic value which changes according to what customer inputs. This is the code:

/**
 * @snippet       Add Row to Order Totals Table - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=21589
 * @author        Rodolfo Melogli
 * @compatible    WC 2.6.14, WP 4.7.2, PHP 5.5.9
 */
 
add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 10, 2 );
 
function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {
 
$total_rows['recurr_not'] = array(
   'label' => __( 'Rec:', 'woocommerce' ),
   'value'   => 'blabla'
);
 
return $total_rows;
}

I’m pretty green at programming, so there would be anyone who knows how to solve this? 🙂

Leave a Comment