0

I'm trying to add a custom email field in Woocommerce.

Code to add the pickup date field to checkout:

// ----------------------------------------------------------------------------
// Pickup Date Field
// ----------------------------------------------------------------------------

add_action( 'woocommerce_after_order_notes', 'gfm_pickup_date_field' );

function gfm_pickup_date_field( $checkout ) {

    // The pickup delay is the minimum number of days between when an order is placed
    // and when it can be picked up.

    $pickup_delay = gfm_get_min_pickup_delay();

    echo '<div id="gfm_pickup_date_field"><h3>' . __('Pickup') . '</h3>';

    woocommerce_form_field( 'gfm_pickup_date', array(
        'type'          => 'text',
        'class'         => array('gfm_pickup_date-class form-row-wide'),
        'label'         => __('Pickup Date'),
        'placeholder'   => __('month/day/year'),
        ), $checkout->get_value( 'gfm_pickup_date' ));

    echo '</div>';
    echo "<script type='text/javascript'>
            jQuery(document).ready(function() {
                jQuery('#gfm_pickup_date').datepicker({
                    dateFormat : 'mm/dd/yy',
                    minDate: {$pickup_delay}
                });
            });
            </script>";
}

Code to save pickup date with order:

// ----------------------------------------------------------------------------
// Save Pickup Date with Order
// ----------------------------------------------------------------------------

add_action( 'woocommerce_checkout_update_order_meta', 'gfm_checkout_field_update_order_meta' );

function gfm_checkout_field_update_order_meta( $order_id ) {

    // Save pickup date if user chose to pickup the order

    if (WC()->session->get('chosen_shipping_methods')[0] == 'local_pickup') {
        update_post_meta( $order_id, 'gfm_pickup_date', sanitize_text_field( $_POST['gfm_pickup_date'] ) );
    }
}

And code to send the emails:

// ----------------------------------------------------------------------------
// Add the Pickup Date to Emails
// ----------------------------------------------------------------------------

add_filter('woocommerce_email_order_meta_keys', 'gfm_order_meta_keys');

function gfm_order_meta_keys( $keys ) {
    $keys[] = 'gfm_pickup_date';
    return $keys;
}



  function gfm_enqueue_scripts() {

      wp_enqueue_style( 'jquery-ui-css', get_template_directory_uri() . '/jquery-ui-1.11.4/jquery-ui.min.css');

      wp_enqueue_script( 'jquery-ui-js', get_template_directory_uri() . '/jquery-ui-1.11.4/jquery-ui.min.js', array('jquery'));

  }



  add_action( 'wp_enqueue_scripts', 'gfm_enqueue_scripts' );

When I run this code, it does the following: none

1 Answer 1

0

You can try with:

function gfm_order_meta_keys( $keys ) {

    $new_keys = array('Pickup date'=>'gfm_pickup_date');
    // if the action is used by another plugin, we merge keys into one array
    $keys = array_merge($keys, $new_keys);

    return $keys;
}
13
  • Thanks for posting! It didn't work :P Do you know of anything else I can try with the code I've got?
    – Nienaber
    Commented Oct 19, 2016 at 15:22
  • I edit my answer, I confused with another action.
    – Benoti
    Commented Oct 19, 2016 at 15:42
  • No luck, same output as before :P
    – Nienaber
    Commented Oct 19, 2016 at 16:59
  • EDIT: The 'Array' it is spitting out seems to be the get_post_meta array. I'm going to go find out if the array contains anything called gfm_pickup_date.
    – Nienaber
    Commented Oct 19, 2016 at 17:22
  • You will get it in the source : woocommerce/includes/class-wc-emails.php starting to line 390. I think it miss [$key]
    – Benoti
    Commented Oct 19, 2016 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.