简体   繁体   English

在 WooCommerce Checkout 自定义文本字段中启用日期选择器

[英]Enable Datepicker in a WooCommerce Checkout custom text field

In WooCommerce, I can get a field hooked into my WooCommerce cart page & showing up OK using the following in my theme's (Storefront) functions PHP:在 WooCommerce 中,我可以将一个字段连接到我的 WooCommerce 购物车页面并在我的主题(店面)PHP 函数中使用以下内容显示 OK:

<?
            // ADD Custom Fields to Checkout Page
            /**
             * Add the field to the checkout
             **/

            add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

            function my_custom_checkout_field( $checkout ) {

                date_default_timezone_set('America/Los_Angeles');
                $mydateoptions = array('' => __('Select PickupDate', 'woocommerce' )); 

                echo '<div id="my_custom_checkout_field"><h3>'.__('Delivery Info').'</h3>';

               woocommerce_form_field( 'order_pickup_date', array(
                    'type'          => 'text',
                    'class'         => array('my-field-class form-row-wide'),
                    'id'            => 'datepicker',
                    'required'      => true,
                    'label'         => __('Delivery Date'),
                    'placeholder'       => __('Select Date'),
                    'options'     =>   $mydateoptions
                    ),$checkout->get_value( 'order_pickup_date' ));

                echo '</div>';
            }

            /**
             * Process the checkout
             **/
            add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

            function my_custom_checkout_field_process() {
                global $woocommerce;

                // Check if set, if its not set add an error.
                if (!$_POST['order_pickup_date'])
                     wc_add_notice( '<strong>PickupDate</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
            }

            /**
             * Update the order meta with field value
             **/
            add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

            function my_custom_checkout_field_update_order_meta( $order_id ) {
                if ($_POST['order_pickup_date']) update_post_meta( $order_id, 'PickupDate', esc_attr($_POST['order_pickup_date']));
            }
            ?>

However, the datepicker widget is not initiated when you click into the field.但是,当您单击该字段时,不会启动日期选择器小部件。 I know the following code is required in the header for JQuery to work:我知道 JQuery 的标头中需要以下代码才能工作:

<script>
$( function() {
$( "#datepicker" ).datepicker(); } );
</script>

This did not work for me, so I thought to put this function into the checkout.js file in Storefront theme, but the added field didn't have calendar widget functionality.这对我不起作用,所以我想把这个功能放到店面主题的 checkout.js 文件中,但添加的字段没有日历小部件功能。

There's a lot of .js in the them do I need to start a new one for includes?它们中有很多.js ,我需要为包含开始一个新的吗?

First you will need to: 首先,您需要:

  • Enqueu main jquery-ui datepicker script Enqueu主要的jquery-ui datepicker脚本
  • Change your custom script starting it with jQuery(function($){ instead of $(function(){ 更改以jQuery(function($){而不是$(function(){

So to enable datepicker for your custom text field your code will be: 因此,要为您的自定义文本字段启用datepicker ,您的代码应为:

// Register main datepicker jQuery plugin script
add_action( 'wp_enqueue_scripts', 'enabling_date_picker' );
function enabling_date_picker() {

    // Only on front-end and checkout page
    if( is_admin() || ! is_checkout() ) return;

    // Load the datepicker jQuery-ui plugin script
    wp_enqueue_script( 'jquery-ui-datepicker' );
}

// Call datepicker functionality in your custom text field
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field', 10, 1);
function my_custom_checkout_field( $checkout ) {

    date_default_timezone_set('America/Los_Angeles');
    $mydateoptions = array('' => __('Select PickupDate', 'woocommerce' ));

    echo '<div id="my_custom_checkout_field">
    <h3>'.__('Delivery Info').'</h3>';

    // YOUR SCRIPT HERE BELOW 
    echo '
    <script>
        jQuery(function($){
            $("#datepicker").datepicker();
        });
    </script>';

   woocommerce_form_field( 'order_pickup_date', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'id'            => 'datepicker',
        'required'      => true,
        'label'         => __('Delivery Date'),
        'placeholder'       => __('Select Date'),
        'options'     =>   $mydateoptions
        ),$checkout->get_value( 'order_pickup_date' ));

    echo '</div>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

Tested and works. 经过测试和工作。 On storefront theme you will get that: 在店面主题中,您将获得:

在此处输入图片说明

You may need to style it… 您可能需要设置样式...

Just an update that this is no longer necessary. 只是更新,它不再需要。 With Woocommerce you can simply change the style to 'date' and it deals with the rest for you - yahoo! 使用Woocommerce,您只需将样式更改为“日期”,即可为您处理剩下的一切-yahoo!

有谁知道如何在订单上看到这一点,当客户购买产品并添加日期时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM