电子邮件输出中的WooCommerce自定义域‘数组’

时间:2016-10-18 作者:Nienaber

我正在尝试在Woocommerce中添加一个自定义电子邮件字段。

将“收货日期”字段添加到签出的代码:

// ----------------------------------------------------------------------------
// 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>";
}
将提货日期与订单一起保存的代码:

// ----------------------------------------------------------------------------
// 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\'] ) );
    }
}
和发送电子邮件的代码:

// ----------------------------------------------------------------------------
// 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\' );
运行此代码时,它会执行以下操作:none

1 个回复
最合适的回答,由SO网友:Benoti 整理而成

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;
}