Set a custom user role based on registration email in WooCommerce - php

I added a custom filter to my functions.php file. When I first loaded it up which was a few versions of Woocommerce ago it worked perfectly.
Now it seems that all users are registered as the default 'customer' as opposed to 'student' when their emails do contain .ac.uk or .sch.uk
The filter function is as follows:
add_filter( 'woocommerce_new_customer_data', 'student_role', 10, 1 );
function student_role( $new_cust_data ) {
if ( strpos($new_cust_data['user_email'], '.ac.uk' ) >= 1 ) {
$new_cust_data['role'] = 'student';
}
elseif ( strpos($new_cust_data['user_email'], '.sch.uk') >= 1 ) {
$new_cust_data['role'] = 'student';
}
else {
$new_cust_data['role'] = 'customer';
}
return $new_cust_data;
}
Any help?

Try to replace >= 1 with !== false in your code (also it can be simplified) as follows:
add_filter( 'woocommerce_new_customer_data', 'set_new_customer_user_role_conditionaly' );
function set_new_customer_user_role_conditionaly( $args ) {
if ( strpos($args['user_email'], '.ac.uk') !== false || strpos($args['user_email'], '.sch.uk') !== false ){
$args['role'] = 'student';
}
return $args;
}
It should work.

Related

How to disable fields that are pre-filled in WooCommerce checkout?

I would like to prevent (make these fields readonly, for example) users from changing their billing information on the WooCommerce checkout form.
I'm currently using this code snippet:
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
function mycustom_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_email']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_phone']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}
But the problem is: If the user has not filled in any of these fields in the registration, he is unable to insert his data in the checkout form because these fields are not editable.
My question is:
If the fields are not empty, how to make them readonly (or disabled)
Someone who can help me with this?
The answer of 7uc1f3r certainly works getting the user data… But since WooCommerce 3, you can use WC_Checkout get_value() dedicated method as follow:
add_filter( 'woocommerce_billing_fields', 'filter_wc_billing_fields', 10, 1 );
function filter_wc_billing_fields( $fields ) {
// On checkout and if user is logged in
if ( is_checkout() && is_user_logged_in() ) {
// Define your key fields below
$keys_fields = ['billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone'];
// Loop through your specific defined fields
foreach ( $keys_fields as $key ) {
// Check that a value exist for the current field
if( ( $value = WC()->checkout->get_value($key) ) && ! empty( $value ) ) {
// Make it readonly if a value exist
$fields[$key]['custom_attributes'] = ['readonly'=>'readonly'];
}
}
}
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
If you want this code to be also active in My account > Addresses > edit…, you will just have to remove is_checkout() && from the first IF statement.
I believe this is what you are looking for, comment with explanation added in code
function mycustom_woocommerce_billing_fields( $fields ) {
// Get current user
$user = wp_get_current_user();
// User id
$user_id = $user->ID;
// User id is found
if ( $user_id > 0 ) {
// Fields
$read_only_fields = array ( 'billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone' );
// Loop
foreach ( $fields as $key => $field ) {
if( in_array( $key, $read_only_fields ) ) {
// Get key value
$key_value = get_user_meta($user_id, $key, true);
if( strlen( $key_value ) > 0 ) {
$fields[$key]['custom_attributes'] = array(
'readonly'=>'readonly'
);
}
}
}
}
return $fields;
}
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );

Admin user list custom column display based on custom user metadata in Wordpress

I have created a custom column for WordPress users called "Verification". Upon WooCommerce registration, an email is sent to the user asking them to verify their email and that way, account. This information is stored in the database under usermeta (wp_usermeta) as a meta_key and that key is called is_activated.
Here is the code for the custom column:
add_action('manage_users_columns','account_verification_status_column');
function account_verification_status_column($column_headers) {
unset($column_headers['posts']);
$column_headers['account_verification'] = 'Verification Status';
return $column_headers;
}
function make_verification_status_column_sortable( $columns ) {
$columns['account_verification'] = 'Verification Status';
return $columns;
}
add_filter( 'manage_users_sortable_columns', 'make_verification_status_column_sortable' );
I am now trying to figure out how to fetch data from the usermeta table and in this case, the meta_key is_activated. Here is what I tried:
add_action('manage_users_custom_column', 'account_verification_information', 10, 3);
function account_verification_information($status = '1') {
global $wpdb;
$has_user_verified = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->users
LEFT JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id
WHERE meta_key = 'is_activated'
AND meta_value = '$status';"
, $level ));
return $has_user_verified;
$check_verification = account_verification_information(1);
if ( $column_name === account_verification ) {
echo $check_verification;
} else {
echo '<span class="na">Not yet Verified</span>';
}
}
Hopefully, by looking at it, you "get the idea".
I need it to show "Verified" if the value is 1 and "Not Verified" if the value is 0.
I've tried so many different versions of this I am getting lost..
Any help is highly appreciated.
Try the following instead:
add_action('manage_users_columns','account_verification_status_column');
function account_verification_status_column($column_headers) {
unset($column_headers['posts']);
$column_headers['account_verification'] = __('Verification Status');
return $column_headers;
}
add_filter( 'manage_users_sortable_columns', 'make_verification_status_column_sortable' );
function make_verification_status_column_sortable( $vars ) {
$columns['account_verification'] = 'account_verification';
return $columns;
}
add_filter('manage_users_custom_column', 'add_user_column_value', 10, 3);
function add_user_column_value( $value, $column_name, $user_id ){
if ( 'account_verification' == $column_name ){
if( get_user_meta( $user_id, 'is_activated', true ) == 1 ){
$value = '<span style="color:green;font-weight:bold;">Verified</span>';
} else {
$value = '<span class="na" style="color:grey;"><em>Not Verified</em></span>';
}
}
return $value;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

wp_login_form authentication problems

Hello stackoverflow community,
I need help solving my problem with WordPress login authentication. So I've installed CAPATCHA by Best... And in order to put capatcha into custom wp_login_form I've needed to add validation. So I've created this:
add_filter( 'wp_authenticate_user', 'my_custom_authenticate', 10, 3 );
function my_custom_authenticate( $user, $username, $password ){
//Get POSTED value
if ( ( function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() !== true ) || ( function_exists( 'cptchpr_check_custom_form' ) && cptchpr_check_custom_form() !== true ) ) {
remove_action('authenticate', 'wp_authenticate_username_password', 20);
$user = new WP_Error( 'denied', __("<strong>ERROR</strong>: You're CAPTCHA field was wrong.") );
}
return $user;
}
But it always returns as error. I've added capatcha field with this:
add_filter( 'login_form_middle','cptch_custom_form' );
Have you ever had this problem, how can I've solve it?
Proceed with a "try and fail" approach.
Use var_dump() to check the values of each single condition in your if statement.
Something like this:
add_filter( 'wp_authenticate_user', 'my_custom_authenticate', 10, 3 );
function my_custom_authenticate( $user, $username, $password ){
/** THIS IS THE DEBUGGING PART */
var_dump(function_exists( 'cptch_check_custom_form' ));
var_dump(cptch_check_custom_form()));
var_dump(function_exists( 'cptchpr_check_custom_form' ));
var_dump(cptchpr_check_custom_form() !== true);
/** END DEBUGGING PART */
//Get POSTED value
if (
( function_exists( 'cptch_check_custom_form' ) && true !== cptch_check_custom_form() )
|| ( function_exists( 'cptchpr_check_custom_form' ) && true !== cptchpr_check_custom_form() ) )
{
remove_action('authenticate', 'wp_authenticate_username_password', 20);
$user = new WP_Error( 'denied', __("<strong>ERROR</strong>: You're CAPTCHA field was wrong.") );
}
return $user;
}
NOTE: I've changed the conditions using the Yoda's notation that is more secure to use.

WooCommerce orders page add customer role custom column

I want to add column to show the customer role on the WooCommerce orders, I search and everything I found is for one user. I also found this code on this link (WooCommerce show custom column) but I do not understand where I put what I need.
I also found this code (https://gist.github.com/corsonr/5975207)
but I do not managed to get the user role.
I added $user_role = $user->roles;
and
switch ($column)
{
case "user_role":
echo $user_role;
break;
}
but it not worked, I know it is array but use of [0] or [1] did not work.
I missing something?
It is possible to do what I what?
Add below code in your theme functions.php
add_filter('manage_edit-shop_order_columns', 'add_column_heading', 20, 1);
function add_column_heading($array) {
$res = array_slice($array, 0, 2, true) +
array("customer_role" => "Customer Role") +
array_slice($array, 2, count($array) - 1, true);
return $res;
}
add_action('manage_posts_custom_column', 'add_column_data', 20, 2);
function add_column_data($column_key, $order_id) {
// exit early if this is not the column we want
if ('customer_role' != $column_key) {
return;
}
$customer = new WC_Order( $order_id );
if($customer->user_id != ''){
$user = new WP_User( $customer->user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
echo $role;
}
}
}

Overriding WooCommerce function in includes folder

I am looking to make some modifications to a function in WooCommerce, on a file called class-wc-product-variation.php in woocommerce/includes/
The function I'm looking to modify is:
public function variation_is_visible() {
$visible = true;
// Published == enabled checkbox
if ( get_post_status( $this->variation_id ) != 'publish' ) {
$visible = false;
}
// Out of stock visibility
elseif ( get_option('woocommerce_hide_out_of_stock_items') == 'yes' && ! $this->is_in_stock() ) {
$visible = false;
}
// Price not set
elseif ( $this->get_price() === "" ) {
$visible = false;
}
return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );
}
I need to add another elseif line in there like so:
elseif ( get_option('woocommerce_hide_out_of_stock_items') != 'yes' && ! $this->is_in_stock() ) {
$visible = false;
}
How do I do this without making changes to the core files?
You should never modify the core files in plugin. In the given function, there is filter woocommerce_variation_is_visible available. Use that filter to customize as per your need.
Code in core file is:
return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id );

Categories