WooCommerce Checkout - Validate user roles and cart contents
P粉545910687
P粉545910687 2024-04-03 11:58:52
0
1
738

I'm having challenges with the checkout process in WooCommerce.

I use the "B2B for WooCommerce" plugin to differentiate between regular products and B2B products. The scene is as follows:

1 - An unregistered visitor adds a product from the "General" category (available to unregistered visitors by default) to their shopping cart.

2 - On the checkout page, the visitor decides to register as a B2B customer (via a form selection field on the checkout page).

3 - The registration and checkout processes occur simultaneously on this page.

I want to prevent an order from being placed if the user registers as a B2B customer and has a "regular" product in the cart. Since these two actions (signup and checkout) happen at the same time, typical WooCommerce hooks don't work as expected.

How to verify the user role and shopping cart contents being registered during the checkout process and block the order when the conditions are met? Or maybe there's a better, easier way to do this?

I tried the functionality of resetting the cart and reloading the page.

edit:

User role: Wwp_wholesaler

I created two WooCommerce product categories: General and Wholesale. "Normal" is visible to all visitors. After registering the role, "Wwp_wholesaler" will be able to see "wholesaler".

The name attribute of the selection field is: "afreg_select_user_role". The value attributes of the options are "customer" (for regular customers) and "wwp_wholesaler" (for wholesalers).

P粉545910687
P粉545910687

reply all(1)
P粉014293738

The following code will stop the checkout process early when a regular item is detected in a B2B customer's shopping cart. In this case, the regular items are removed from the cart and an error message is thrown, preventing the order from being placed.

Note: The user role alias provided is incorrect because the user role alias is not capitalized.

Code:

add_action( 'woocommerce_checkout_create_order', 'process_checkout_creating_order',  10, 2  );
function process_checkout_creating_order( $order, $data ) {
    global $current_user;

    $targeted_field = 'afreg_select_user_role'; // Checkout field key to target
    $targeted_role  = 'wwp_wholesaler'; // User role slug (for B to B)
    $targeted_term  = 'Normal'; // Category term for Regular items

    // Targeting B to B user role only
    if( ( isset($data[$targeted_field]) && $data[$targeted_field] === $targeted_role ) 
    || in_array( $targeted_role, $current_user->roles ) ) {
        $cart = WC()->cart; //  Cart live object
        $item_keys_found = array(); // Initializing

        // Loop through cart items to search for "regular" items
        foreach ( $cart->get_cart() as $item_key => $item ) {
            if ( has_term( $targeted_term, 'product_cat', $item['product_id']) ) {
                $item_keys_found[] = $item_key;
            }
        }
        // If regular items are found
        if ( count($item_keys_found) > 0 ) {
            // Loop through regular item keys (and remove each)
            foreach ( $item_keys_found as $item_key ) {
                $cart->remove_cart_item( $item_key );
            }
            // Throw an error message, avoiding saving and processing the order
            throw new Exception( 
                sprintf( __('You are not allowed to purchase "Regular" items.'.
                ' %d "Regular" %s been removed from cart. %s', 'woocommerce'),
                    count($item_keys_found),
                    _n('item has', 'items have', count($item_keys_found), 'woocommerce'),
                    sprintf( '%s', get_permalink(wc_get_page_id('shop')), __('Continue shopping', 'woocommerce') )
            ) );
        }
    }
}

The code is in the child theme’s functions.php file (or in the plugin). Tested and working.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!