Check if Fiscal Code already exists during Checkout [WOOCOMMERCE]

288 Views Asked by At

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!

1

There are 1 best solutions below

4
On

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