Ecommerce Shopify WordPress Discussion

Make registration field usermeta value unique for each user in WooCommerce

I'm trying to validate the Company Name field for a unique value for each user in the WooCommerce registration form, but I'm having trouble: // That's what I got, but it doesn't work... add_action( 'woocommerce_created_customer', 'custom_billing_company_validation'); function custom__billing_company_validation( $data, $errors ) { $billing_company = isset( $data['billing_company'] ) ? sanitize_text_field( $data['billing_company'] ) : ''; if ( ! empty( $billing_company ) ) { global $wpdb; $count =$wpdb->get_row("SELECT 1 FROM $wpdb->usermeta WHERE $wpdb->usermeta.meta_key = 'billing_company' AND $wpdb->usermeta.meta_value = '$billing_company' AND $wpdb->usermeta.user_id != $user->ID"); if ( $count > 0 ) { $errors->add( 'validation', __('The "Name Company " already exists, please contact us.') ); } } }
Update 2: Here below I use a generic function that checks I some user metadata exist. Add this function yo your existing code: // Conditional utility function function user_meta_field_value_exists( $meta_key, $meta_value ) { global $wpdb; $result = $wpdb->get_col( $wpdb->prepare( " SELECT um.user_id FROM {$wpdb->prefix}usermeta um WHERE um.meta_key = '%s' AND um.meta_value LIKE '%s' ", $meta_key, $meta_value ) ); return empty($result) ? false : true; } Then from Show or hide WooCommerce registration fields based on a selected field value answer code, replace account_registration_field_validation() function with the following: // registration Field validation add_filter( 'woocommerce_registration_errors', 'account_registration_field_validation', 10, 3 ); function account_registration_field_validation( $errors, $username, $email ) { if ( $_POST['role'] === 'customer' && isset($_POST['billing_company']) ) { if ( empty($_POST['billing_company']) ) { $errors->add( 'billing_company_error', __( 'Name Company is a required field', 'woocommerce' ) ); } elseif ( ! empty($_POST['billing_company']) && user_meta_field_value_exists( 'billing_company', sanitize_text_field($_POST['billing_company']) ) ) { $errors->add( 'billing_company_error', __( 'The "Name Company" already exists, please contact us.', 'woocommerce' ) ); } } if ( $_POST['role'] === 'subscriber' && isset($_POST['billing_product']) && empty($_POST['billing_product']) ) { $errors->add( 'billing_product_error', __( 'Company Product is a required field', 'woocommerce' ) ); } return $errors; } Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

December 29, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.