Custom checkout random select field in Woocommerce










1















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.










share|improve this question




























    1















    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.










    share|improve this question


























      1












      1








      1








      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.










      share|improve this question
















      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.







      php wordpress woocommerce html-select checkout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 0:59









      LoicTheAztec

      86.7k136399




      86.7k136399










      asked Nov 12 '18 at 21:02









      Ashley Redman BScAshley Redman BSc

      1261216




      1261216






















          1 Answer
          1






          active

          oldest

          votes


















          1














          To make a random checkout select field (as a random question) with validation and saving the data as custom order meta data, use the following:



          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>';





          Code goes in function.php file of your active child theme (active theme). Tested and works.






          share|improve this answer























          • You sir are a god amongst men. I have added and tested, works perfectly! Thank you!

            – Ashley Redman BSc
            Nov 13 '18 at 9:23






          • 1





            Upvoted, thanks again.

            – Ashley Redman BSc
            Nov 13 '18 at 14:35










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270052%2fcustom-checkout-random-select-field-in-woocommerce%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          To make a random checkout select field (as a random question) with validation and saving the data as custom order meta data, use the following:



          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>';





          Code goes in function.php file of your active child theme (active theme). Tested and works.






          share|improve this answer























          • You sir are a god amongst men. I have added and tested, works perfectly! Thank you!

            – Ashley Redman BSc
            Nov 13 '18 at 9:23






          • 1





            Upvoted, thanks again.

            – Ashley Redman BSc
            Nov 13 '18 at 14:35















          1














          To make a random checkout select field (as a random question) with validation and saving the data as custom order meta data, use the following:



          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>';





          Code goes in function.php file of your active child theme (active theme). Tested and works.






          share|improve this answer























          • You sir are a god amongst men. I have added and tested, works perfectly! Thank you!

            – Ashley Redman BSc
            Nov 13 '18 at 9:23






          • 1





            Upvoted, thanks again.

            – Ashley Redman BSc
            Nov 13 '18 at 14:35













          1












          1








          1







          To make a random checkout select field (as a random question) with validation and saving the data as custom order meta data, use the following:



          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>';





          Code goes in function.php file of your active child theme (active theme). Tested and works.






          share|improve this answer













          To make a random checkout select field (as a random question) with validation and saving the data as custom order meta data, use the following:



          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>';





          Code goes in function.php file of your active child theme (active theme). Tested and works.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 0:58









          LoicTheAztecLoicTheAztec

          86.7k136399




          86.7k136399












          • You sir are a god amongst men. I have added and tested, works perfectly! Thank you!

            – Ashley Redman BSc
            Nov 13 '18 at 9:23






          • 1





            Upvoted, thanks again.

            – Ashley Redman BSc
            Nov 13 '18 at 14:35

















          • You sir are a god amongst men. I have added and tested, works perfectly! Thank you!

            – Ashley Redman BSc
            Nov 13 '18 at 9:23






          • 1





            Upvoted, thanks again.

            – Ashley Redman BSc
            Nov 13 '18 at 14:35
















          You sir are a god amongst men. I have added and tested, works perfectly! Thank you!

          – Ashley Redman BSc
          Nov 13 '18 at 9:23





          You sir are a god amongst men. I have added and tested, works perfectly! Thank you!

          – Ashley Redman BSc
          Nov 13 '18 at 9:23




          1




          1





          Upvoted, thanks again.

          – Ashley Redman BSc
          Nov 13 '18 at 14:35





          Upvoted, thanks again.

          – Ashley Redman BSc
          Nov 13 '18 at 14:35

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270052%2fcustom-checkout-random-select-field-in-woocommerce%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo