4

After customer registration, admin will verify his identity and activates that customer. For maintaining customer verify status I'm using IsVerified as 1 in wp_usermeta table.

Now, If customer adds product to his cart, I want to check that customer is verified or not. If verified then only I need actual cart process. If customer is not-verified then I want to redirect him/her to upload specified documents and the cart functionality no need to work. If not logged in need to redirect him to login page.

I'm new to woocommerce and wordpress so if any example it'll be easy to understand.

Updated Code is as below:

add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
    $userId=get_current_user_id();
    if($userId>0) {
        $UserROW = get_user_meta($userId);
        if( $UserROW['woo_VerifyStatus'][0] == 0 ) {
            wp_redirect( get_home_url().'/gld/my-account');
        } else {
            //Normal cart functionality
        }
    } else {
        wp_redirect( get_home_url().'/gld/my-account');
    }
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Deva AP
  • 43
  • 6
  • try with the filter `woocommerce_add_to_cart_product_id` which is defined here : https://docs.woocommerce.com/wc-apidocs/source-class-WC_Form_Handler.html#665 – mmm Jan 28 '18 at 18:24

2 Answers2

2

Is not possible to make a redirection before add to cart in a simple way, but you can use woocommerce_add_to_cart_validation hook to avoid add to cart and display a custom notice with a linked button to your specific verification page.

Check that the correct meta_key for your verified status in the code is 'woo_VerifyStatus'

The code:

add_action( 'woocommerce_add_to_cart_validation', 'custome_add_to_cart_validation', 10, 1 );
function custome_add_to_cart_validation( $passed ){
    // When user is logged in in we get his verified status
    if( is_user_logged_in() )
        // Get user 'woo_VerifyStatus' postmeta value
        $verified_user = get_user_meta( get_current_user_id(), 'woo_VerifyStatus', true );

    // When user is not logged in we avoid add to cart and display a custom message
    if( ! is_user_logged_in() ){
        $message = __( "Please, you need to be registered and a verified user.", "woocommerce" );
        $button_text = __("Login or register", "woocommerce");
        $url = home_url('/gld/my-account');
        $message .= ' <a href="'.$url.'" class="login-register button" style="float:right;">'.$button_text.'</a>';
        $passed = false; // Set to false

        $message .= ' <a href="#" class="login-register button" style="float:right;">'.$button_text.'</a>';
    }

    // When is not a verified user we avoid add to cart and display a custom message
    elseif( $verified_user != 1 ) {
        $message = __( "Please, you need to be a verified user.", "woocommerce" );
        $button_text = __("proceed", "woocommerce");
        $url = home_url('/gld/my-account');
        $message .= ' <a href="'.$url.'" class="login-register button" style="float:right;">'.$button_text.'</a>';
        $passed = false; // Set to false
    }
    if( ! $passed )
        wc_add_notice( $message, 'error' );

    return $passed;
}

Code goes in function.php file of your active child theme (or active theme).

It should work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
1

You should use Hook as below:

add_action( 'action_name', 'your_function_name' );

function your_function_name() {
// Your code
}

for example for your problem you should use the same code as belw:

add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
// Your code
}
Mohammadreza Khedri
  • 2,523
  • 1
  • 11
  • 22