【问题标题】:Dynamic select field options based on selected day in Woocommerce checkout基于 Woocommerce 结帐中选定日期的动态选择字段选项
【发布时间】:2018-04-15 18:11:38
【问题描述】:

我在 Woocommerce 结帐中添加了 2 个自定义字段:

  1. 一个 jQuery-ui 日期选择器字段,
  2. 时间的选择字段(下拉)。

现在,我希望时间选择字段下拉选项取决于所选日期。基本上如果选择周六和周日,我希望限制时间间隔(只到 15:00)。

我尝试了很多不同的策略,但似乎没有一个是正确的方法。

这是两个自定义字段:

//Datepicker 
add_action( 'woocommerce_before_order_notes', 'my_custom_checkout_field_DP' );


function my_custom_checkout_field_DP($checkout)
{
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-ui', "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/smoothness/jquery-ui.css", '', '', false);
wp_enqueue_style('datepicker', plugins_url('/css/datepicker.css', __FILE__), '', '', false);

/*print(' <link rel="stylesheet" type="text/css" href="' . plugins_url() . '/order-delivery-date-for-woocommerce/datepicker.css">

<script type="text/javascript" src="' . plugins_url() . '/order-delivery-date-for-woocommerce/datepicker.js"></script>'
);
print('<script type="text/javascript" src="' . plugins_url() . '/order-delivery-date-for-woocommerce/initialize-datepicker.js"></script>');*/


echo '<script language="javascript">jQuery(document).ready
(function(){
jQuery("#e_deliverydate").width("150px");
var formats = ["MM d, yy","MM d, yy"];
jQuery("#e_deliverydate").val("").datepicker({dateFormat: formats[1], minDate:1});
jQuery("#e_deliverydate").parent().after("<div id=\'order-desc\'></div>");
});   </script>';

echo '<div id="my_custom_checkout_field_DP" style="width: 202%; float: left;">';

woocommerce_form_field('e_deliverydate', array(
'type' => 'text', 
'class'=> array('my-field-class form-row-first'),
'label' => __('Your Delivery Date'), 
'required' => true, 
'placeholder' => __('Pick a date')
), $checkout->get_value('e_deliverydate'));

 echo '</div>';

//Time selection field


add_action( 'woocommerce_before_order_notes', 'my_custom_checkout_field_TIME' ); 
unction my_custom_checkout_field_TIME( $checkout ) {

woocommerce_form_field( 'my_custom_field_TIME', array(
'type'          => 'select',
'class'         => array('my-field-class form-row-wide'),
'label'         => __('Your time'),
'required'      => true, 
'placeholder'   => __('Time to pick'),

'options'       => array(
'10:00'   => __( '10:00', 'wps' ),
'10:30'   => __( '10:30', 'wps' ),
'11:00'   => __( '11:00', 'wps' ),
'11:30'   => __( '11:30', 'wps' ),
'12:00'   => __( '12:00', 'wps' ),
'12:30'   => __( '12:30', 'wps' ),
'13:00'   => __( '13:00', 'wps' ),
'13:30'   => __( '13:30', 'wps' ),
'14:00'   => __( '14:00', 'wps' ),
'14:30'   => __( '14:30', 'wps' ),
'15:00'   => __( '15:00', 'wps' ),
'15:30'   => __( '15:30', 'wps' ),
'16:00'   => __( '16:00', 'wps' ),
'16:30'   => __( '16:30', 'wps' ),
'17:00'   => __( '17:00', 'wps' ),
'17:30'   => __( '17:30', 'wps' ),
'18:00'   => __( '18:00', 'wps' ),
)
), $checkout->get_value( 'my_custom_field_TIME' ));

echo '</div>';
}

感谢任何帮助。

【问题讨论】:

  • 1. $globalText 是干什么用的? ... 2. 还没有定义$order_parent_id...此外,结帐时没有可用的订单 ID 或父订单 ID。
  • 嗨,我想将所选日期保存在该变量 ($globalText) 中,然后根据所选日期显示时间选择下拉菜单,但它不起作用... $globalText 应该已经从代码中删除了,它现在没有做任何事情:-)
  • 我现在删除了那行......时间字段(my_custom_field_TIME)如何依赖于日期选择器中的选定日期?这可能吗?

标签: php jquery woocommerce jquery-ui-datepicker checkout


【解决方案1】:

下面的代码将根据结帐页面中jQuery-ui Datepicker字段中选择的日期,通过jQuery动态加载时间选择字段选项。

我还为选择字段启用了selectWooselect2 库的克隆)。

我已将您的函数和单独的脚本合并到一个专用挂钩中。

您重新访问的代码:

// 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' );
    wp_enqueue_style('jquery-ui', "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/smoothness/jquery-ui.css", '', '', false);
    ## wp_enqueue_style('datepicker', plugins_url('/css/datepicker.css', __FILE__), '', '', false);
}

// Datepicker field and select time field
add_action( 'woocommerce_before_order_notes', 'datepicker_custom_field' );
function datepicker_custom_field($checkout) {

    // $globalText = get_post_meta( $order_parent_id, 'e_deliverydate', true);

    echo '<div id="date-time-wrapper">';

    woocommerce_form_field('delivery_date', array(
        'type' => 'text',
        'class'=> array('delivery-date-class form-row-first'),
        'label' => __('Your Delivery Date'),
        'required' => true,
        'placeholder' => __('Pick a date')
    ), $checkout->get_value('delivery_date') );

    $options = array('' => __('Time to pick…') );

    woocommerce_form_field( 'delivery_time', array(
        'type'          => 'select',
        'class'         => array('delivery-time-class form-row-last'),
        'label'         => __('Your Delivery time'),
        'required'      => true,
        'options'       => $options,
    ),  $checkout->get_value( 'delivery_time' ) );

    // Restricted options array
    $options1 = array(
        '10:00'   => __( '10:00', 'wps' ),
        '10:30'   => __( '10:30', 'wps' ),
        '11:00'   => __( '11:00', 'wps' ),
        '11:30'   => __( '11:30', 'wps' ),
        '12:00'   => __( '12:00', 'wps' ),
        '12:30'   => __( '12:30', 'wps' ),
        '13:00'   => __( '13:00', 'wps' ),
        '13:30'   => __( '13:30', 'wps' ),
        '14:00'   => __( '14:00', 'wps' ),
        '14:30'   => __( '14:30', 'wps' ),
        '15:00'   => __( '15:00', 'wps' ),
    );

    // The other part of options array
    $options2 = array(
        '15:30'   => __( '15:30', 'wps' ),
        '16:00'   => __( '16:00', 'wps' ),
        '16:30'   => __( '16:30', 'wps' ),
        '17:00'   => __( '17:00', 'wps' ),
        '17:30'   => __( '17:30', 'wps' ),
        '18:00'   => __( '18:00', 'wps' ),
    );

    // Merging options arrays 
    $options1 = array_merge($options, $options1); // Partial
    $options  = array_merge($options1,$options2); // Full

    echo '<br clear="all"></div>';

    ?>
    <script language="javascript">
    jQuery( function($){
        var a = <?php echo json_encode($options); ?>,
            b = <?php echo json_encode($options1); ?>,
            c = new Date(),
            s = 'select#delivery_time';

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

        // Once DOM is loaded
        if( c.getDay() == 6 || c.getDay() == 0 )
            dynamicSelectOptions( b );
        else
            dynamicSelectOptions( a );

        // Select time to selectWoo
        $(s).selectWoo();

        // Datepicker
        $('#delivery_date').datepicker({
            dateFormat: 'MM d, yy',
            minDate:1,
            onSelect: function(){
                // Live event: On selected date event
                var d = new Date($(this).val());
                if( d.getDay() == 6 || d.getDay() == 0 )
                    dynamicSelectOptions( b );
                else
                    dynamicSelectOptions( a );
            }
        }).parent().after('<div id="order-desc"></div>');
    });
    </script>
    <?php
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。测试和工作

【讨论】:

  • 太棒了!非常感谢!这只是一个小问题。当我在 4 月选择周六或周日时,时间选择字段工作正常,但对于 7 月和 6 月的相同选择,时间选择字段显示到 18:00 ($option2) 并且在 9 月再次正常工作......看不到问题出在哪里。 getDay() 函数每个月都一样,不是吗? dateFormat: 'MM d, yy' 是否正确?不应该有一个额外的“d”吗?谢谢。
  • 很抱歉没有接受。这是我在这里的第一篇文章,所以我是stackoverflow的新手。现在已经接受了你的答案..
  • 我可以看到你的服务器没有这个问题。也许我的其他一些代码导致了这种情况。有什么想法我应该去看看吗?
  • 我怀疑 Datepicker 的本地化功能可能会导致此问题,并尝试禁用该功能,但问题仍然存在。有什么建议吗?
  • @s_83 很抱歉,因为我真的无法知道您的安装中发生了什么导致您描述的问题。我无法重现该问题。您应该尝试禁用您的其他自定义、插件(之前进行备份)并重新启用它们以找到有罪...如果您找不到任何东西,也许您可​​以提出一个新问题,将我的代码添加到您的问题中并尝试清楚地解释您的问题……
猜你喜欢
  • 2018-10-11
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2018-10-19
  • 2018-07-21
  • 2017-03-21
  • 1970-01-01
  • 2018-10-29
相关资源
最近更新 更多