首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce中动态同步自定义签出选择字段

WooCommerce中动态同步自定义签出选择字段
EN

Stack Overflow用户
提问于 2018-02-10 20:11:35
回答 1查看 2.7K关注 0票数 2

在Woocommerce中,我能够在结帐页面中添加2个自定义下拉列表:

代码语言:javascript
复制
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');

function wps_add_select_checkout_field( $checkout) {
    echo '<h2>'.__('Next Day Delivery').'</h2>';
    woocommerce_form_field( 'City', array(
        'type'          => 'select',
        'class'         => array( 'wps-drop' ),
        'label'         => __( 'Delivery options' ),
        'options'       => array(
            'blank'     => __( 'Select a day part', 'wps' ),
            'A' => __( 'A', 'wps' ),
            'B' => __( 'B', 'wps' ),
            'C'     => __( 'C', 'wps' )
        )
    ),   
    $checkout->get_value( 'City' ));
}

add_action('woocommerce_before_order_notes', 'wps_add_select_checkout1_field');

function wps_add_select_checkout1_field( $checkout1) {
    //echo '<h2>'.__('Next Day Delivery').'</h2>';
    woocommerce_form_field( 'Dis', array(
        'type'          => 'select',
        'class'         => array( 'wps-drop' ),
        'label'         => __( 'Delivery options' ),
        'options'       => array(
            'blank'     => __( 'Select a day part', 'wps' ),
            'A' => __( 'ro', 'wps' ),
            'B' => __( 'wa', 'wps' ),
            'C'     => __( 'da', 'wps' )
        )
    ),
    $checkout1->get_value( 'Dis' ));
}

//* Process the checkout
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
    global $woocommerce;
    // Check if set, if its not set add an error.
    if ($_POST['City'] == "blank")
    wc_add_notice( '<strong>Please select a day part under Delivery options</strong>', 'error' );
}

我希望根据从第一个下拉列表中选择的动态生成第二个下拉列表的<option>选项。

我需要的例子:

  • 如果<option> "A“是在第一个下拉列表中选择的,则第二个下拉列表将动态显示特定的选项集。
  • 如果<option> "C“是在第一个下拉列表中选择的,则第二个下拉列表将动态显示不同的选项集。
  • …等

这有可能吗?我要从哪里开始?

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-11 01:58:08

我已经合并了你的两个第一个函数,因为他们使用相同的钩子。

在这个合并的函数中,我添加了:

  • 这两个字段的“必需”选项。
  • 更改两个字段的弹格,例如"City"抛出woocommerce错误。
  • 传递给javascript的不同的“选项”数组(第一个select字段中每个可用的<option> )。
  • 一些jQuery代码,根据第一个选择字段的选定<option>动态地在第二个select字段中创建选项集。

在最后一个函数中,我稍微改变了If语句的条件。

下面是重新访问的代码:

代码语言:javascript
复制
// Add checkout custom select fields
add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_fields', 20, 1 );
function add_checkout_custom_fields( $checkout) {
    $domain = 'woocommerce'; // The domain slug

    echo '<h2>'.__( 'Next Day Delivery', $domain ).'</h2>';

    // First Select field (Master)
    woocommerce_form_field( 'delivery_one', array(
        'type'          => 'select',
        'label'         => __( 'Delivery options one' , $domain),
        'class'         => array( 'form-row-wide' ),
        'required'       => true,
        'options'       => array(
            ''  => __( 'Please select a value', $domain ),
            'A' => __( 'A', $domain ),
            'B' => __( 'B', $domain ),
            'C' => __( 'C', $domain ),
        ),
    ),
    $checkout->get_value( 'delivery_one' ) );

    // Default option value
    $default_option2 = __( 'Please select a value', $domain );

    // Dynamic select field options for Javascript/jQuery
    $options_0 = array( '' => $default_option2 );
    $options_a = array(
        ''  => $default_option2,
        'D' => __( 'D', $domain ),
        'E' => __( 'E', $domain ),
        'F' => __( 'F', $domain ),
        'G' => __( 'G', $domain ),
    );
    $options_b = array(
        ''  => $default_option2,
        'H' => __( 'H', $domain ),
        'I' => __( 'I', $domain ),
        'J' => __( 'J', $domain ),
    );
    $options_c = array(
        ''  => $default_option2,
        'K' => __( 'K', $domain ),
        'L' => __( 'L', $domain ),
        'M' => __( 'M', $domain ),
    );

    // Second Select field (Dynamic Slave)
    woocommerce_form_field( 'delivery_two', array(
        'type'          => 'select',
        'label'         => __( 'Delivery options two', $domain ),
        'class'         => array( 'form-row-wide' ),
        'required'       => true,
        'options'       => $options_0,
    ),
    $checkout->get_value( 'delivery_two' ) );

    $required = esc_attr__( 'required', 'woocommerce' );

    // jQuery code
    ?>
    <script>
    jQuery(function($){
        var op0 = <?php echo json_encode($options_0); ?>,
            opa = <?php echo json_encode($options_a); ?>,
            opb = <?php echo json_encode($options_b); ?>,
            opc = <?php echo json_encode($options_c); ?>,
            select1 = 'select[name="delivery_one"]',
            select2 = 'select[name="delivery_two"]';

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

        // 1. When dom is loaded we add the select field option for "A" value
        // => Disabled (optional) — Uncomment below to enable
        // dynamicSelectOptions( opa );

        // 2. On live selection event on the first dropdown
        $(select1).change(function(){
            if( $(this).val() == 'A' )
                dynamicSelectOptions( opa );
            else if( $(this).val() == 'B' )
                dynamicSelectOptions( opb );
            else if( $(this).val() == 'C' )
                dynamicSelectOptions( opc );
            else
                dynamicSelectOptions( op0 ); // Reset to default
        });
    });
    </script>
    <?php
}

// Check checkout custom fields
add_action( 'woocommerce_checkout_process', 'wps_check_checkout_custom_fields', 20 ) ;
function wps_check_checkout_custom_fields() {
    // if custom fields are empty stop checkout process displaying an error notice.
    if ( empty($_POST['delivery_one']) || empty($_POST['delivery_two']) ){
        $notice = __( 'Please select a day part under Delivery options' );
        wc_add_notice( '<strong>' . $notice . '</strong>', 'error' );
    }
}

代码在您的活动子主题(活动主题)的function.php文件中。

这是一个充分发挥作用和测试的例子。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48725121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档