1

Good morning everybody,

I'm trying to introduce a fiscal code verification during my checkout. The verification is similar to what Wordpress does with email.

I created a similar "email_exists()" function to check if the fiscal code in the order already exists and then I put a function to give a warning if this fiscal code exists.

Here's the code:

function cf_exists( $order_cf ) {
   $order_cf =  $order->billing_address_2;
   $user = get_user_by( 'fiscalcode', $billing_address_2 );
   if ( $user ) {
       $user_id = $user->ID;
   } else {
       $user_id = false;

function cf_check(){
if ( cf_exists( $order_cf ) ) {
       return new WP_Error( 'registration-error-fc-exists', __( 'An account is already registered with your fiscal code. Please login.', 'woocommerce' ) ); 
}

As usual, Wordpress breaks itself and I don't understand what I'm missing. Hope to get some help, thanks in advance for your time!

  • Just a little note: I used the Billing Address 2 as Fiscal Code Field intentionally, it's not a mistypo – Luca Farina Oct 07 '21 at 06:32
  • 2
    `$order` is not defined, so that's where the problem seems to be. You also do not indicate in your question from where you call the function? Please make sure your question contains [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – 7uc1f3r Oct 07 '21 at 07:57
  • Good morning 7uc1f3r, thanks for the link! I'm still new to php and woocommerce programming so sometimes I made this kind of mistake, I'll provide more correct informations from now! – Luca Farina Oct 07 '21 at 08:41
  • On the checkout page, the $order object doesn't exist yet. As I mentioned, **edit your question** and **add the necessary details** needed so that someone can answer your question. Where did you add this code? are you calling it through a hook? from a template file? – 7uc1f3r Oct 08 '21 at 11:52

1 Answers1

1

Just missing a few closing brackets and you messed a bit with function arguments. Also it's get_billing_address_2() and you need to return something:

function cf_exists( $order ) {
   $order_cf =  $order->get_billing_address_2();
   $user = get_user_by( 'fiscalcode', $billing_address_2 );
   if ( $user ) {
       return true;
   } else {
       return false;
   }
}

function cf_check( $order ){
   if ( ! cf_exists( $order ) ) {
       wc_add_notice( __( 'An account is already registered with your fiscal code. Please login.', 'woocommerce' ), 'error' );
   }
}
businessbloomer
  • 1,115
  • 7
  • 11
  • 1
    Thank you Businessbloomer for the correction and the information! I tried to implement it but it doesn't show any error message. I tried to replace the "return new WP_Error" with "return $errors->add( 'validation', 'Error' );" but it doesn't show anything too. Do you have some suggestions to make this message appear? Thanks in advance for your time – Luca Farina Oct 07 '21 at 10:17
  • Ciao Luca :) I'd need more context i.e. when are you calling the function and where? Basically the rest of the code so that I know if/when it triggers – businessbloomer Oct 07 '21 at 10:46
  • I'll try my best to give you more context! I'm calling the function during the checkout phase, when you make the order. The error message I want to show is similar to the ones when you don't input a required field. The flow is like this: User inserts his data in the checkout fields and then it clicks the order button. The function will check if the fiscal code already exists: if yes, it will displays the error message above the checkout form, where all the other errors appear (just to give the context) – Luca Farina Oct 07 '21 at 12:40
  • Ok, try this new version – businessbloomer Oct 07 '21 at 13:54