代码之家  ›  专栏  ›  技术社区  ›  Ashley Redman BSc

电子商务中的自定义签出随机选择字段

  •  1
  • Ashley Redman BSc  · 技术社区  · 6 年前

    我想将该功能添加到/checkout页面的自定义商业字段中。理想情况下,此字段应为带有多个选项的Select类型。这不是问题,因为我可以将下面的代码添加到我的子主题的functions.php中来创建它。

    function customise_checkout_field($checkout)
    {
    // Heading for form
    echo '<p>Custom Question Heading</p>';
    
    woocommerce_form_field( 'questionOne', array(
        'type' => 'select',
        'class' => array( 'custom-dev-select'),
        'label' => 'This is the question',
        'options' => array(
            'blank' => 'Choose One',
            'value1' => 'Answer 1,
            'value2' => 'Answer 2
        ),
        'required' => true
        )
    );
    
    $checkout->get_value( $random_question );
    
    }
    

    问题是,我想说这些“woocommerce”表单字段中的X3,每个字段都有不同的标签/问题和不同的选项。例如

    问题1:苹果是一个:

    选择2:肉类 选项3:素食

    问题2:一些问题

    备选案文2:知识产权 备选案文3:知识产权

    我曾尝试将多个“woocommerce\u form-field”添加到一个数组中,并使用数组\u rand等,但这不起作用。这里有一些示例代码,我已经准备好了,但是目前还没有工作,但是您已经了解了我希望它如何工作。

    function customise_checkout_field($checkout)
    {
    // Heading for form
    echo '<p>Custom Question Heading</p>';
    
    $questions = array(
        "question1" => array(
            "This is question one",
            "Choice 1",
            "Choice 2"
        ),
    
        "question2" => array(
            "This is question Two",
            "Choice 1.1",
            "Choice 2.1"
        ),
    
        "question3" => array(
            "label" => "This is question Three",
            "Choice 1.2",
            "Choice 2.2"
        ),
     );
    
    $random_question = $questions[array_rand($questions)];
    
    
    $selected_label = $random_question[0];
    $selected_answer = $random_question[1];
    $selected_answer2 = $random_question[2];
    
    
    woocommerce_form_field( 'questionOne', array(
        'type' => 'select',
        'class' => array( 'custom-dev-select'),
        'label' => $selected_label,
        'options' => array(
            'blank' => 'Choose One',
            'value1' => $selected_answer,
            'value2' => $selected_answer2
        ),
        'required' => true
        )
    );
    
    
    $checkout->get_value( $random_question );
    }
    add_action('woocommerce_after_order_notes', 'customise_checkout_field');
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   LoicTheAztec    6 年前

    add_action( 'woocommerce_after_order_notes', 'custom_select_field_with_random_options', 10, 1 );
    function custom_select_field_with_random_options( $checkout )
    {
        // Heading for form
        echo '<h4>' . __("Custom Question Heading", "woocommerce") . '</h4>';
    
        $questions = array(
            '1' => array(
                'label'     => __("one", "woocommerce"),
                'options'   => array(
                    'value1'    => __("Choice 1.1", "woocommerce"),
                    'value2'    => __("Choice 1.2", "woocommerce"),
                ),
            ),
            '2' => array(
                'label'     => __("two", "woocommerce"),
                'options'   => array(
                    'value1'    => __("Choice 2.1", "woocommerce"),
                    'value2'    => __("Choice 2.2", "woocommerce"),
                ),
            ),
            '3' => array(
                'label'     => __("three", "woocommerce"),
                'options'   => array(
                    'value1'    => __("Choice 3.1", "woocommerce"),
                    'value2'    => __("Choice 3.2", "woocommerce"),
                ),
            ),
        );
    
        $key      = array_rand($questions); // Random key
        $question = $questions[$key]; // The question data array
        $label    = $question['label'];
        $default  = array( '' => __("Choose an answer", "woocommerce") );
        $options  = $default + $question['options'];
    
        woocommerce_form_field( 'question_'.$key, array(
            'type' => 'select',
            'class' => array( 'custom-dev-select'),
            'label' => __("This is the question", "woocommerce") . ' ' . $label,
            'options' => $options,
            'required' => true
        ), $checkout->get_value( 'question_'.$key ) );
    
        echo '<input type="hidden" name="question_key" value="'.$key.'">';
    }
    
    // Custom Checkout fields validation
    add_action('woocommerce_checkout_process', 'custom_checkout_select_field_validation');
    function custom_checkout_select_field_validation() {
        if ( isset($_POST['question_key']) ) {
            $key = esc_attr( $_POST['question_key'] );
    
            if ( isset($_POST['question_'.$key]) && empty($_POST['question_'.$key]) )
                wc_add_notice( '<strong>'. __("Please select a value", "woocommerce") . '</strong>', 'error' );
        }
    }
    
    // Save custom checkout fields the data to the order
    add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
    function custom_checkout_field_update_meta( $order, $data ){
        if ( isset($_POST['question_key']) ) {
            $key = esc_attr( $_POST['question_key'] );
    
            if ( isset($_POST['question_'.$key]) && ! empty($_POST['question_'.$key]) ) {
                $order->update_meta_data( '_question_value', esc_attr( $_POST['question_'.$key] ) );
                $order->update_meta_data( '_question_key', $key );
            }
        }
    }
    
    // display the random question data in the order admin panel
    add_action( 'woocommerce_admin_order_data_after_order_details', 'display_question_to_admin_order', 10, 1 );
    function display_question_to_admin_order( $order ){
        if( $key = $order->get_meta( '_question_key' ) ) {
            if( $value = $order->get_meta( '_question_value' ) ) {
                echo '<br style="clear:both">
                <p><strong>' . __( "Random question", "woocommerce" ) . ' '. $key . ':</strong> ' . $value . '</p>';
            }
        }
    }
    

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