我正在尝试在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\' );
运行此代码时,它会执行以下操作: