Email fields comparison validation in Woocommerce checkout











up vote
1
down vote

favorite












I have a custom field called gift and I am now trying to validate that against the billing_email field. In other words, if the customer fills in the gift field and it is the same email as the billing email, an error should be shown when clicking "complete order" button.



It does not work. Any ideas why? Here's the code.



// verify that billing email and gift email are not the same
function billing_email_and_gift_validation( $posted ) {
$checkout = WC()->checkout;
if ( strcmp( $posted['billing_email'] == $posted['gift'] )) {
wc_add_notice( __( 'To send as gift, you cannot send it to yourself. That's the point of a gift, is it not?', 'woocommerce' ), 'error' ); } }
add_action( 'woocommerce_after_checkout_validation', 'billing_email_and_gift_validation', 10, 2 );


Thank you all if anyone can help.










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a custom field called gift and I am now trying to validate that against the billing_email field. In other words, if the customer fills in the gift field and it is the same email as the billing email, an error should be shown when clicking "complete order" button.



    It does not work. Any ideas why? Here's the code.



    // verify that billing email and gift email are not the same
    function billing_email_and_gift_validation( $posted ) {
    $checkout = WC()->checkout;
    if ( strcmp( $posted['billing_email'] == $posted['gift'] )) {
    wc_add_notice( __( 'To send as gift, you cannot send it to yourself. That's the point of a gift, is it not?', 'woocommerce' ), 'error' ); } }
    add_action( 'woocommerce_after_checkout_validation', 'billing_email_and_gift_validation', 10, 2 );


    Thank you all if anyone can help.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a custom field called gift and I am now trying to validate that against the billing_email field. In other words, if the customer fills in the gift field and it is the same email as the billing email, an error should be shown when clicking "complete order" button.



      It does not work. Any ideas why? Here's the code.



      // verify that billing email and gift email are not the same
      function billing_email_and_gift_validation( $posted ) {
      $checkout = WC()->checkout;
      if ( strcmp( $posted['billing_email'] == $posted['gift'] )) {
      wc_add_notice( __( 'To send as gift, you cannot send it to yourself. That's the point of a gift, is it not?', 'woocommerce' ), 'error' ); } }
      add_action( 'woocommerce_after_checkout_validation', 'billing_email_and_gift_validation', 10, 2 );


      Thank you all if anyone can help.










      share|improve this question















      I have a custom field called gift and I am now trying to validate that against the billing_email field. In other words, if the customer fills in the gift field and it is the same email as the billing email, an error should be shown when clicking "complete order" button.



      It does not work. Any ideas why? Here's the code.



      // verify that billing email and gift email are not the same
      function billing_email_and_gift_validation( $posted ) {
      $checkout = WC()->checkout;
      if ( strcmp( $posted['billing_email'] == $posted['gift'] )) {
      wc_add_notice( __( 'To send as gift, you cannot send it to yourself. That's the point of a gift, is it not?', 'woocommerce' ), 'error' ); } }
      add_action( 'woocommerce_after_checkout_validation', 'billing_email_and_gift_validation', 10, 2 );


      Thank you all if anyone can help.







      php wordpress validation woocommerce checkout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 14:24









      LoicTheAztec

      80.1k125992




      80.1k125992










      asked Nov 18 at 13:38









      WooDevil

      1077




      1077
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I have merged this validation with the gift validation from this question code you have made before, so you will remove it from your actual code as this function handle both of them:



          add_action('woocommerce_after_checkout_validation', 'gift_custom_field_validation', 20, 2 );
          function gift_custom_field_validation( $data, $errors ) {
          // Validating that "Gift" is not empty when selected
          if( isset($_POST['gift_msg']) && isset($_POST['gift']) && empty($_POST['gift']) )
          $errors->add( 'gift', __( "You've chosen to send this as a gift, but did not submit a recipient email address.", "woocommerce" ) );

          // Validating that "Gift" imputed email is different from billing email
          if ( isset($_POST['gift']) && $data['billing_email'] === $_POST['gift'] ) {
          $errors->add( 'gift', __( "To send as gift, you cannot send it to yourself. That's the point of a gift, isn't it?", "woocommerce" ) );
          }
          }


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






          share|improve this answer





















          • thank you very much. Works.
            – WooDevil
            Nov 18 at 14:34











          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',
          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%2f53361483%2femail-fields-comparison-validation-in-woocommerce-checkout%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








          up vote
          1
          down vote



          accepted










          I have merged this validation with the gift validation from this question code you have made before, so you will remove it from your actual code as this function handle both of them:



          add_action('woocommerce_after_checkout_validation', 'gift_custom_field_validation', 20, 2 );
          function gift_custom_field_validation( $data, $errors ) {
          // Validating that "Gift" is not empty when selected
          if( isset($_POST['gift_msg']) && isset($_POST['gift']) && empty($_POST['gift']) )
          $errors->add( 'gift', __( "You've chosen to send this as a gift, but did not submit a recipient email address.", "woocommerce" ) );

          // Validating that "Gift" imputed email is different from billing email
          if ( isset($_POST['gift']) && $data['billing_email'] === $_POST['gift'] ) {
          $errors->add( 'gift', __( "To send as gift, you cannot send it to yourself. That's the point of a gift, isn't it?", "woocommerce" ) );
          }
          }


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






          share|improve this answer





















          • thank you very much. Works.
            – WooDevil
            Nov 18 at 14:34















          up vote
          1
          down vote



          accepted










          I have merged this validation with the gift validation from this question code you have made before, so you will remove it from your actual code as this function handle both of them:



          add_action('woocommerce_after_checkout_validation', 'gift_custom_field_validation', 20, 2 );
          function gift_custom_field_validation( $data, $errors ) {
          // Validating that "Gift" is not empty when selected
          if( isset($_POST['gift_msg']) && isset($_POST['gift']) && empty($_POST['gift']) )
          $errors->add( 'gift', __( "You've chosen to send this as a gift, but did not submit a recipient email address.", "woocommerce" ) );

          // Validating that "Gift" imputed email is different from billing email
          if ( isset($_POST['gift']) && $data['billing_email'] === $_POST['gift'] ) {
          $errors->add( 'gift', __( "To send as gift, you cannot send it to yourself. That's the point of a gift, isn't it?", "woocommerce" ) );
          }
          }


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






          share|improve this answer





















          • thank you very much. Works.
            – WooDevil
            Nov 18 at 14:34













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I have merged this validation with the gift validation from this question code you have made before, so you will remove it from your actual code as this function handle both of them:



          add_action('woocommerce_after_checkout_validation', 'gift_custom_field_validation', 20, 2 );
          function gift_custom_field_validation( $data, $errors ) {
          // Validating that "Gift" is not empty when selected
          if( isset($_POST['gift_msg']) && isset($_POST['gift']) && empty($_POST['gift']) )
          $errors->add( 'gift', __( "You've chosen to send this as a gift, but did not submit a recipient email address.", "woocommerce" ) );

          // Validating that "Gift" imputed email is different from billing email
          if ( isset($_POST['gift']) && $data['billing_email'] === $_POST['gift'] ) {
          $errors->add( 'gift', __( "To send as gift, you cannot send it to yourself. That's the point of a gift, isn't it?", "woocommerce" ) );
          }
          }


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






          share|improve this answer












          I have merged this validation with the gift validation from this question code you have made before, so you will remove it from your actual code as this function handle both of them:



          add_action('woocommerce_after_checkout_validation', 'gift_custom_field_validation', 20, 2 );
          function gift_custom_field_validation( $data, $errors ) {
          // Validating that "Gift" is not empty when selected
          if( isset($_POST['gift_msg']) && isset($_POST['gift']) && empty($_POST['gift']) )
          $errors->add( 'gift', __( "You've chosen to send this as a gift, but did not submit a recipient email address.", "woocommerce" ) );

          // Validating that "Gift" imputed email is different from billing email
          if ( isset($_POST['gift']) && $data['billing_email'] === $_POST['gift'] ) {
          $errors->add( 'gift', __( "To send as gift, you cannot send it to yourself. That's the point of a gift, isn't it?", "woocommerce" ) );
          }
          }


          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 18 at 14:22









          LoicTheAztec

          80.1k125992




          80.1k125992












          • thank you very much. Works.
            – WooDevil
            Nov 18 at 14:34


















          • thank you very much. Works.
            – WooDevil
            Nov 18 at 14:34
















          thank you very much. Works.
          – WooDevil
          Nov 18 at 14:34




          thank you very much. Works.
          – WooDevil
          Nov 18 at 14:34


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361483%2femail-fields-comparison-validation-in-woocommerce-checkout%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

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga