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).
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:
The code is in the child theme’s functions.php file (or in the plugin). Tested and working.