Woocommerce中的自定义结帐随机选择字段

时间:2018-11-12 21:02:18

标签: php wordpress woocommerce html-select checkout

我有一个使用WooCommerce的简单WordPress网站。

我想将功能添加到/ checkout页面上的自定义woocommerce字段。理想情况下,该字段的类型应为“选择”,其中包含多个选项。这不是问题,因为我可以将以下代码添加到子主题的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_form_field”的X3,每个都有不同的标签/问题和不同的选项。例如;

问题1:是苹果吗?

选项1:水果 选项2:肉 选项3:蔬菜

问题2:一些问题

选项1:lorem 选项2:lorem 选项3:lorem

然后每次加载或刷新页面等时,都会加载一个不同的问题。

我尝试将多个“ woocommerce_form-field”添加到数组中,并使用array_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');

任何帮助将不胜感激。 WordPress,woocommerce和php对我来说还很新,因为这不是我要使用的主要语言。

1 个答案:

答案 0 :(得分:1)

要进行带有验证的随机结帐选择字段(作为随机问题)并将数据保存为自定义订单元数据,请使用以下命令:

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文件中。经过测试,可以正常工作。