-1

In WooCommerce, based on "Dynamic synched custom checkout select fields in WooCommerce" answer code, I am adding some custom fields on checkout which will be displayed under the billing section on the e-mail confirmation

Here is my actual code:

add_action( 'woocommerce_after_checkout_billing_form', 'add_checkout_custom_fields', 20, 1 );
function add_checkout_custom_fields( $checkout) {
    $domain = 'woocommerce'; // The domain slug

    // First Select field (Master)
    woocommerce_form_field( 'delivery_one', array(
        'type'          => 'select',
        'label'         => __( 'Art der Lieferung' , $domain),
        'class'         => array( 'form-row-first' ),
        'required'       => true,
        'options'       => array(
            ''  => __( 'Wählen Art der Lieferung.', $domain ),
            'A' => __( 'Hauszustellung', $domain ),
            'B' => __( 'Selbst Abholung', $domain ),

        ),
    ), $checkout->get_value( 'delivery_one' ) );

    // Default option value
    $default_option2 = __( 'Wählen Sie Zeitbereich.', $domain );

    // Dynamic select field options for Javascript/jQuery
    $options_0 = array( '' => $default_option2 );
    $options_a = array(
        ''  => $default_option2,
        '1' => __( '09:00-11:00', $domain ),
        '2' => __( '10:00-12:00', $domain ),
        '3' => __( '11:00-13:00', $domain ),
        '4' => __( '12:00-14:00', $domain ),
        '5' => __( '13:00-15:00', $domain ),
        '6' => __( '14:00-16:00', $domain ),
        '7' => __( '15:00-17:00', $domain ),
    );

    $options_b = array(
        ''  => $default_option2,
        '1' => __( '01:00', $domain ),
        '2' => __( '02:00', $domain ),
        '3' => __( '03:00', $domain ),
        '4' => __( '04:00', $domain ),
        '5' => __( '05:00', $domain ),
        '6' => __( '06:00', $domain ),
        '7' => __( '07:00', $domain ),
        '8' => __( '08:00', $domain ),
        '9' => __( '09:00', $domain ),
        '10' => __( '10:00', $domain ),
        '11' => __( '11:00', $domain ),
        '12' => __( '12:00', $domain ),
        '13' => __( '13:00', $domain ),
        '14' => __( '14:00', $domain ),
        '15' => __( '15:00', $domain ),
        '16' => __( '16:00', $domain ),
        '17' => __( '17:00', $domain ),
        '18' => __( '18:00', $domain ),
        '19' => __( '19:00', $domain ),
        '20' => __( '20:00', $domain ),
        '21' => __( '21:00', $domain ),
        '22' => __( '22:00', $domain ),
        '23' => __( '23:00', $domain ),
        '24' => __( '24:00', $domain ),
    );

    // Second Select field (Dynamic Slave)
    woocommerce_form_field( 'delivery_two', array(
        'type'          => 'select',
        'label'         => __( 'Zeitspanne', $domain ),
        'class'         => array( 'form-row-last' ),
        'required'       => true,
        'options'       => $options_0,
    ), $checkout->get_value( 'delivery_two' ) );

    $required = esc_attr__( 'required', 'woocommerce' );

    // jQuery code
    ?>
    <script>
    jQuery(function($){
        var op0 = <?php echo json_encode($options_0); ?>,
            opa = <?php echo json_encode($options_a); ?>,
            opb = <?php echo json_encode($options_b); ?>,

            select1 = 'select[name="delivery_one"]',
            select2 = 'select[name="delivery_two"]';

        // Utility function to fill dynamically the select field options
        function dynamicSelectOptions( opt ){
            var options = '';
            $.each( opt, function( key, value ){
                options += '<option value="'+key+'">'+value+'</option>';
            });
            $(select2).html(options);
        }

        // 1. When dom is loaded we add the select field option for "A" value
        // => Disabled (optional) — Uncomment below to enable
        // dynamicSelectOptions( opa );

        // 2. On live selection event on the first dropdown
        $(select1).change(function(){
            if( $(this).val() == 'A' )
                dynamicSelectOptions( opa );
            else if( $(this).val() == 'B' )
                dynamicSelectOptions( opb );

            else
                dynamicSelectOptions( op0 ); // Reset to default
        });
    });
    </script>
    <?php
}

// Check checkout custom fields
add_action( 'woocommerce_checkout_process', 'wps_check_checkout_custom_fields', 20 ) ;
function wps_check_checkout_custom_fields() {
    // if custom fields are empty stop checkout process displaying an error notice.
    if ( empty($_POST['delivery_one']) || empty($_POST['delivery_two']) ){
        $notice = __( 'Bitte wählen Sie die Versandart oder den Stundenbereich' );
        wc_add_notice( '<strong>' . $notice . '</strong>', 'error' );
    }
}  

My custom fields and their values, are shown on the checkout form and on the order pages on back end. So far everything works great.

But the problem is that the e-mail that I receive does not contain the custom fields and their values.

How can I display that custom checkout billing fields on email notifications?

Is this code correct?

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
  function add_custom_email_format_string( $string, $email ) {
// The post meta key used to save the value in the order post meta data
$meta_key = '_delivery_one';

// Get the instance of the WC_Order object
$order    = $email->object;

// Get the value
$value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';

// Additional subject placeholder
$new_placeholders = array( '{delivery_one}' => $value );


return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
 }
İlker A.
  • 1
  • 6

2 Answers2

0

You have to define a new placeholder that can be parsed.

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
function add_custom_email_format_string( $string, $email ) {
    // The post meta key used to save the value in the order post meta data
    $meta_key = '_billing_field_newfield';

    // Get the instance of the WC_Order object
    $order    = $email->object;

    // Get the value
    $value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';

    // Additional subject placeholder
    $new_placeholders = array( '{billing_field_newfield}' => $value );

    // Return the clean replacement value string for "{billing_field_newfield}" placeholder
    return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}

Code goes in function.php file of your active child theme (or active theme). It will works.

Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {billing_field_newfield}…

arnebr
  • 545
  • 1
  • 6
  • 17
0

You should first save input value at the database as an order meta see here and then you can get the meta value in email template. All the email template of woocommerce is customizable and located at plugins/woocommerce/emails/

You can use this hook to store custom input field at order meta

do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );

Thanks

Sushil Adhikari
  • 764
  • 6
  • 12