I have a simple WordPress site that is using WooCommerce.
I would like to add the functionality onto the /checkout page a custom woocommerce field. This field would ideally be of type Select with multiple options. This is not a problem as I am able to add the below code into my child theme's functions.php to create this.
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 );
}
This will produce a single select option with the above attributes.
The issue is, I would like to have say X3 of these 'woocommerce_form_field's, each with different a different label/question and different options. For example;
Question 1: Is an apple a:
Option 1: Fruit Option 2: Meat Option 3: Veg
Question 2: Some question
Option 1: lorem Option 2: lorem Option 3: lorem
And then each time the page is loaded or refreshed etc a different question is loaded.
I have tried adding multiple 'woocommerce_form-field's into an array and using array_rand etc however this does not work. Here is some example code I have in place which currently is not working, but you get the idea of how I would like it to work.
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');
Any help would be greatly appreciated. WordPress, woocommerce & php is fairly new to me as this is not my main language to use.