Custom checkout field validation on Woocommerce












1















My code:



add_action( 'woocommerce_before_checkout_form', 'fruit_field' );

function fruit_field( $checkout ) {

woocommerce_form_field( 'fruit', array(
'type' => 'select',
'required' => true,
'options' => array(
'apple' => __('Apple'),
'banana' => __('Banana'),
'watermelon' => __('Watermelon'),
'other' => __('Other'),
),
'class' => array('my-class'),
'label' => __('Best fruit?'),
), $checkout->get_value( 'fruit' ));
}


And the validation:



add_action('woocommerce_checkout_process', 'process_checkout');

function process_checkout() {
if ($_POST['fruit'] === null) {
wc_add_notice( __( 'No fruits?' ), 'error' );
}
}


After submitting the form, it always displays my custom error "No fruits?", no matter what was selected. Is $_POST['fruit'] somehow not available in the process_checkout function?










share|improve this question

























  • What is the error that it displays?

    – Robert
    Nov 21 '18 at 14:57











  • @Robert It always displays my custom error "No fruits?". I have updated the question. Thanks

    – tadadadadi
    Nov 21 '18 at 15:08
















1















My code:



add_action( 'woocommerce_before_checkout_form', 'fruit_field' );

function fruit_field( $checkout ) {

woocommerce_form_field( 'fruit', array(
'type' => 'select',
'required' => true,
'options' => array(
'apple' => __('Apple'),
'banana' => __('Banana'),
'watermelon' => __('Watermelon'),
'other' => __('Other'),
),
'class' => array('my-class'),
'label' => __('Best fruit?'),
), $checkout->get_value( 'fruit' ));
}


And the validation:



add_action('woocommerce_checkout_process', 'process_checkout');

function process_checkout() {
if ($_POST['fruit'] === null) {
wc_add_notice( __( 'No fruits?' ), 'error' );
}
}


After submitting the form, it always displays my custom error "No fruits?", no matter what was selected. Is $_POST['fruit'] somehow not available in the process_checkout function?










share|improve this question

























  • What is the error that it displays?

    – Robert
    Nov 21 '18 at 14:57











  • @Robert It always displays my custom error "No fruits?". I have updated the question. Thanks

    – tadadadadi
    Nov 21 '18 at 15:08














1












1








1








My code:



add_action( 'woocommerce_before_checkout_form', 'fruit_field' );

function fruit_field( $checkout ) {

woocommerce_form_field( 'fruit', array(
'type' => 'select',
'required' => true,
'options' => array(
'apple' => __('Apple'),
'banana' => __('Banana'),
'watermelon' => __('Watermelon'),
'other' => __('Other'),
),
'class' => array('my-class'),
'label' => __('Best fruit?'),
), $checkout->get_value( 'fruit' ));
}


And the validation:



add_action('woocommerce_checkout_process', 'process_checkout');

function process_checkout() {
if ($_POST['fruit'] === null) {
wc_add_notice( __( 'No fruits?' ), 'error' );
}
}


After submitting the form, it always displays my custom error "No fruits?", no matter what was selected. Is $_POST['fruit'] somehow not available in the process_checkout function?










share|improve this question
















My code:



add_action( 'woocommerce_before_checkout_form', 'fruit_field' );

function fruit_field( $checkout ) {

woocommerce_form_field( 'fruit', array(
'type' => 'select',
'required' => true,
'options' => array(
'apple' => __('Apple'),
'banana' => __('Banana'),
'watermelon' => __('Watermelon'),
'other' => __('Other'),
),
'class' => array('my-class'),
'label' => __('Best fruit?'),
), $checkout->get_value( 'fruit' ));
}


And the validation:



add_action('woocommerce_checkout_process', 'process_checkout');

function process_checkout() {
if ($_POST['fruit'] === null) {
wc_add_notice( __( 'No fruits?' ), 'error' );
}
}


After submitting the form, it always displays my custom error "No fruits?", no matter what was selected. Is $_POST['fruit'] somehow not available in the process_checkout function?







php wordpress woocommerce checkout custom-fields






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 21:37









LoicTheAztec

93.4k1367106




93.4k1367106










asked Nov 21 '18 at 14:55









tadadadaditadadadadi

155




155













  • What is the error that it displays?

    – Robert
    Nov 21 '18 at 14:57











  • @Robert It always displays my custom error "No fruits?". I have updated the question. Thanks

    – tadadadadi
    Nov 21 '18 at 15:08



















  • What is the error that it displays?

    – Robert
    Nov 21 '18 at 14:57











  • @Robert It always displays my custom error "No fruits?". I have updated the question. Thanks

    – tadadadadi
    Nov 21 '18 at 15:08

















What is the error that it displays?

– Robert
Nov 21 '18 at 14:57





What is the error that it displays?

– Robert
Nov 21 '18 at 14:57













@Robert It always displays my custom error "No fruits?". I have updated the question. Thanks

– tadadadadi
Nov 21 '18 at 15:08





@Robert It always displays my custom error "No fruits?". I have updated the question. Thanks

– tadadadadi
Nov 21 '18 at 15:08












1 Answer
1






active

oldest

votes


















0















You can not use custom checkout fields in woocommerce_before_checkout_form hook as your field is outside the checkout form, and it is not posted on submit.




Instead you should use woocommerce_checkout_before_customer_details action hook instead:



add_action( 'woocommerce_checkout_before_customer_details', 'fruit_custom_checkout_field' );
function fruit_custom_checkout_field() {

woocommerce_form_field( '_fruit', array(
'type' => 'select',
'label' => __('Best fruit?'),
'class' => array('my-fruit'),
'required' => true,
'options' => array(
'' => __('Chose a fruit'),
'Apple' => __('Apple'),
'Banana' => __('Banana'),
'Watermelon' => __('Watermelon'),
'Other' => __('Other'),
),
), WC()->checkout->get_value('_fruit') );
}

add_action('woocommerce_checkout_process', 'process_fruit_custom_checkout_field');
function process_fruit_custom_checkout_field() {
if (isset($_POST['_fruit']) && empty($_POST['_fruit']) ) {
wc_add_notice( __( 'please choose a "fruits"' ), 'error' );
}
}

// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_create_order', 'save_fruit_custom_field_as_meta_data', 10, 2 );
function save_fruit_custom_field_as_meta_data( $order, $data ) {
if (isset($_POST['_fruit']) && ! empty($_POST['_fruit']) ) {
$order->update_meta_data('_custom_field', esc_attr( $_POST['_fruit'] ) );
}
}


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






share|improve this answer























    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%2f53414742%2fcustom-checkout-field-validation-on-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









    0















    You can not use custom checkout fields in woocommerce_before_checkout_form hook as your field is outside the checkout form, and it is not posted on submit.




    Instead you should use woocommerce_checkout_before_customer_details action hook instead:



    add_action( 'woocommerce_checkout_before_customer_details', 'fruit_custom_checkout_field' );
    function fruit_custom_checkout_field() {

    woocommerce_form_field( '_fruit', array(
    'type' => 'select',
    'label' => __('Best fruit?'),
    'class' => array('my-fruit'),
    'required' => true,
    'options' => array(
    '' => __('Chose a fruit'),
    'Apple' => __('Apple'),
    'Banana' => __('Banana'),
    'Watermelon' => __('Watermelon'),
    'Other' => __('Other'),
    ),
    ), WC()->checkout->get_value('_fruit') );
    }

    add_action('woocommerce_checkout_process', 'process_fruit_custom_checkout_field');
    function process_fruit_custom_checkout_field() {
    if (isset($_POST['_fruit']) && empty($_POST['_fruit']) ) {
    wc_add_notice( __( 'please choose a "fruits"' ), 'error' );
    }
    }

    // Save the custom checkout field in the order meta
    add_action( 'woocommerce_checkout_create_order', 'save_fruit_custom_field_as_meta_data', 10, 2 );
    function save_fruit_custom_field_as_meta_data( $order, $data ) {
    if (isset($_POST['_fruit']) && ! empty($_POST['_fruit']) ) {
    $order->update_meta_data('_custom_field', esc_attr( $_POST['_fruit'] ) );
    }
    }


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






    share|improve this answer




























      0















      You can not use custom checkout fields in woocommerce_before_checkout_form hook as your field is outside the checkout form, and it is not posted on submit.




      Instead you should use woocommerce_checkout_before_customer_details action hook instead:



      add_action( 'woocommerce_checkout_before_customer_details', 'fruit_custom_checkout_field' );
      function fruit_custom_checkout_field() {

      woocommerce_form_field( '_fruit', array(
      'type' => 'select',
      'label' => __('Best fruit?'),
      'class' => array('my-fruit'),
      'required' => true,
      'options' => array(
      '' => __('Chose a fruit'),
      'Apple' => __('Apple'),
      'Banana' => __('Banana'),
      'Watermelon' => __('Watermelon'),
      'Other' => __('Other'),
      ),
      ), WC()->checkout->get_value('_fruit') );
      }

      add_action('woocommerce_checkout_process', 'process_fruit_custom_checkout_field');
      function process_fruit_custom_checkout_field() {
      if (isset($_POST['_fruit']) && empty($_POST['_fruit']) ) {
      wc_add_notice( __( 'please choose a "fruits"' ), 'error' );
      }
      }

      // Save the custom checkout field in the order meta
      add_action( 'woocommerce_checkout_create_order', 'save_fruit_custom_field_as_meta_data', 10, 2 );
      function save_fruit_custom_field_as_meta_data( $order, $data ) {
      if (isset($_POST['_fruit']) && ! empty($_POST['_fruit']) ) {
      $order->update_meta_data('_custom_field', esc_attr( $_POST['_fruit'] ) );
      }
      }


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






      share|improve this answer


























        0












        0








        0








        You can not use custom checkout fields in woocommerce_before_checkout_form hook as your field is outside the checkout form, and it is not posted on submit.




        Instead you should use woocommerce_checkout_before_customer_details action hook instead:



        add_action( 'woocommerce_checkout_before_customer_details', 'fruit_custom_checkout_field' );
        function fruit_custom_checkout_field() {

        woocommerce_form_field( '_fruit', array(
        'type' => 'select',
        'label' => __('Best fruit?'),
        'class' => array('my-fruit'),
        'required' => true,
        'options' => array(
        '' => __('Chose a fruit'),
        'Apple' => __('Apple'),
        'Banana' => __('Banana'),
        'Watermelon' => __('Watermelon'),
        'Other' => __('Other'),
        ),
        ), WC()->checkout->get_value('_fruit') );
        }

        add_action('woocommerce_checkout_process', 'process_fruit_custom_checkout_field');
        function process_fruit_custom_checkout_field() {
        if (isset($_POST['_fruit']) && empty($_POST['_fruit']) ) {
        wc_add_notice( __( 'please choose a "fruits"' ), 'error' );
        }
        }

        // Save the custom checkout field in the order meta
        add_action( 'woocommerce_checkout_create_order', 'save_fruit_custom_field_as_meta_data', 10, 2 );
        function save_fruit_custom_field_as_meta_data( $order, $data ) {
        if (isset($_POST['_fruit']) && ! empty($_POST['_fruit']) ) {
        $order->update_meta_data('_custom_field', esc_attr( $_POST['_fruit'] ) );
        }
        }


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






        share|improve this answer














        You can not use custom checkout fields in woocommerce_before_checkout_form hook as your field is outside the checkout form, and it is not posted on submit.




        Instead you should use woocommerce_checkout_before_customer_details action hook instead:



        add_action( 'woocommerce_checkout_before_customer_details', 'fruit_custom_checkout_field' );
        function fruit_custom_checkout_field() {

        woocommerce_form_field( '_fruit', array(
        'type' => 'select',
        'label' => __('Best fruit?'),
        'class' => array('my-fruit'),
        'required' => true,
        'options' => array(
        '' => __('Chose a fruit'),
        'Apple' => __('Apple'),
        'Banana' => __('Banana'),
        'Watermelon' => __('Watermelon'),
        'Other' => __('Other'),
        ),
        ), WC()->checkout->get_value('_fruit') );
        }

        add_action('woocommerce_checkout_process', 'process_fruit_custom_checkout_field');
        function process_fruit_custom_checkout_field() {
        if (isset($_POST['_fruit']) && empty($_POST['_fruit']) ) {
        wc_add_notice( __( 'please choose a "fruits"' ), 'error' );
        }
        }

        // Save the custom checkout field in the order meta
        add_action( 'woocommerce_checkout_create_order', 'save_fruit_custom_field_as_meta_data', 10, 2 );
        function save_fruit_custom_field_as_meta_data( $order, $data ) {
        if (isset($_POST['_fruit']) && ! empty($_POST['_fruit']) ) {
        $order->update_meta_data('_custom_field', esc_attr( $_POST['_fruit'] ) );
        }
        }


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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 21:37









        LoicTheAztecLoicTheAztec

        93.4k1367106




        93.4k1367106
































            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%2f53414742%2fcustom-checkout-field-validation-on-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

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents