Search code examples
phpwordpresswoocommercecartcheckout

Disable WooCommerce checkout payment for specific user meta data


My customer wants to enable for some specific customers to pay afterwards via invoice. So i've added an extra field in the user profiles, where he can select yes/no. This field works fine and saves properly.

Depending on the choice: no = default webshop behavior and customer needs to pay direct. yes = customer can order items without paying, he'll get an invoice later on.

Also tried different payment methods, but they are available for everybody, i only want them for specific users.

Now i've tried based on that field in the functions.php to add a conditional filter like so:

if (esc_attr( get_the_author_meta( 'directbetalen', $user->ID ) ) == 'no') {
    add_filter('woocommerce_cart_needs_payment', '__return_false');
}

But it doesn't seem to work?

I want payment to be skipped when the field is set to no. Else proceed as normal and customer needs to pay.


Solution

  • Using WordPress get_user_meta() function, try the following :

    add_filter( 'woocommerce_cart_needs_payment', 'disable_payment_for_specific_users' );
    function show_specific_payment_method_for_specific_users( $needs_payment ) {
        if ( get_user_meta( get_current_user_id(), 'directbetalen', true ) === 'no' ) {
            $needs_payment = false;
        }
        return $needs_payment;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). It should work.