简体   繁体   中英

Manage customers to be allowed to purchase in Woocommerce

We have wholesale web with eshop. We need to block new customer from buying anything until we verify him (because of shipping restrictions and so on). Is it possible (by setting him some different role or blocking customer role from buying)?

BTW I found this but I would prefer more changing user, not products, because we want the lock for all products. When inserting new ones, someone could forget about setting the proper product category. Restrict woocommerce product to certain customer to buy

The following code will disable product purchases (hidden add to cart button everywhere) until customer has been approved by the shop manager in the admin users section pages with a custom select field, to allow the customer purchasing products:

// Disable purcahses if customer is not approved
add_filter( 'woocommerce_is_purchasable', 'purchasable_products_based_on_city', 10, 2 );
function purchasable_products_based_on_city( $purchasable, $product ) {
    if( ! get_user_meta( get_current_user_id(), 'can_purchase', true ) )
        $purchasable = false;

    return $purchasable;
}

// Add allowed custom user field in admin
add_action( 'show_user_profile', 'add_user_validation_field', 1, 1 );
add_action( 'edit_user_profile', 'add_user_validation_field', 1, 1 );
function add_user_validation_field( $user )
{
    $value = get_user_meta( $user->ID, 'can_purchase', true );

    $selected0 = ! $value ? ' selected="selected"' : '';
    $selected1 = $value   ? ' selected="selected"' : '';

    ?><h3><?php _e("Customer validation (Allow purchase)", "woocommerce" ); ?></h3>
    <table class="form-table"><tr>
        <th><label for="can_purchase">Customer can purchase</label></th>
        <td><select style="" id="can_purchase" name="can_purchase" class="select short">
            <option value=""<?php  echo $selected0; ?>><?php _e("Not allowed", "woocommerce"); ?></option>
            <option value="1"<?php echo $selected1; ?>><?php _e("Yes (validated)", "woocommerce"); ?></option>
        </select></td></tr></table><br /><?php
}

// Save allowed custom user field in admin
add_action( 'personal_options_update', 'save_user_validation_field' );
add_action( 'edit_user_profile_update', 'save_user_validation_field' );
function save_user_validation_field( $user_id )
{
    if( isset($_POST['can_purchase']) )
        update_user_meta( $user_id, 'can_purchase', esc_attr( $_POST['can_purchase'] ) );
}

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM