Home » WooCommerce B2B: How to Set Up a Wholesale Store

WooCommerce B2B: How to Set Up a Wholesale Store

by Tutor Aspire

The ecommerce sector is seeing incredible growth, year after year, with no foreseeable end in sight. The same is true for B2B ecommerce, yet there aren’t many good platform choices available for small-to-medium businesses that want to sell wholesale. There are several SaaS solutions on the market, but these are costly, closed-source, and mostly oriented towards larger businesses.

If you are a business owner or developer, WooCommerce is a solution that’s free, versatile and powerful. 

Is WooCommerce right for B2B shops?

“Out-of-the-box”, it isn’t. WooCommerce is a fantastic solution for ecommerce stores, but it wasn’t developed specifically for wholesale, so it lacks many important options at the outset. However, you can use a powerful wholesale plugin like B2BKing to extend WooCommerce and add all the business-to-business functionalities that you may need. 

There are two other aspects that you should be aware of when choosing WooCommerce for your B2B project:

  • WooCommerce will require regular plugin updates to ensure it’s secure and working correctly.
  • Depending on your hosting and website configuration, the WordPress environment can sometimes perform a little slowly. However, there are ways to optimize and speed it up such as through plugins like WP Rocket (or other caching and optimization plugins).

While it’s not perfect, WooCommerce is currently powering more than 20% of the world’s online stores, and there are good reasons for that: it’s free, open-source, powerful and secure. These same qualities also make it a great choice for wholesale stores.

How does a B2B store differ from a typical e-store?

Selling business-to-business is often a very different and more personal experience than selling directly to consumers. Business buyers are knowledgeable, open to negotiation, and want to get great offers and discounts for buying in bulk. Price catalogs, discount options and payment and shipping options can vary widely from customer to customer, depending on factors such as business size, order size or existing business relationships.

From a website development standpoint this translates into a necessity for a high degree of technical flexibility when it comes to pricing, discounts, shipping and order rules. 

Selling to businesses also introduces a need for features such as:

  • Hiding prices for guests
  • Business registration form 
  • VAT (or other tax ID) number support
  • Tax exemptions
  • Quote requests
  • Custom billing and checkout fields
  • Wholesale order form
  • Ability to support multiple users on a buyer account (for corporate structures)

Let’s go ahead and look at how a few of these features can be implemented in WooCommerce. The next sections will be geared more towards developers and I will share a few code snippets that I hope you will find helpful, as well as free plugins that you can use.

1. Hide prices for guest users

Let’s start off with an easy one. You can do this with two WooCommerce filters. First, let’s use woocommerce_get_price_html to change the displayed price to “Login to view prices”.

add_filter( 'woocommerce_get_price_html', 'b2bking_hide_prices_guest_users', 10, 2 );

function b2bking_hide_prices_guest_users( $price, $product ){
if ( ! is_user_logged_in() ){
return esc_html__( 'Login to view prices', 'your-plugin-text-domain' );
} else {
return $price;
}
}

Once we’ve done this, prices are no longer visible and will be replaced by our text. This is not enough though, since the user can still add these products to cart and see their price. A solution is provided to us by the aptly named woocommerce_is_purchasable filter.

add_filter( 'woocommerce_is_purchasable', 'b2bking_disable_purchasable_guest_users' );

function b2bking_disable_purchasable_guest_users( $purchasable ){
if ( ! is_user_logged_in() ){
return false;
} else {
return $purchasable;
}
}

After you add this, products should no longer be purchasable by guest users and the “add to cart” button will no longer be available. One more thing that’s worth mentioning is that you may get into trouble when using AJAX search forms, depending on how those are set up. A quick way to get that fixed is to also add the above code to your main code and check for AJAX by wrapping the code inside:

if ( wp_doing_ajax() ){ 
// code here
}

The end result:

If you are interested in a plugin alternative because you’re not familiar with coding, B2BKing has this and other guest access restriction functionalities such as an option to hide the website entirely, or hide prices for individual products or categories.

2. Business registration, or separate B2B and B2C registration forms

What you want to do here is add custom fields such as “Company Name”, “Address”, “VAT ID”, etc.

You can use this code to add a custom field for company name:

add_action( 'woocommerce_register_form', 'b2bking_custom_registration_field' );

function b2bking_custom_registration_field(){
echo '';
echo '';
}

If you want to sync this field with the WooCommerce billing field for company name on registration, you can do this by using the woocommerce_created_customer hook and saving the company name as user meta, using the same fields that WooCommerce uses: billing_first_name, billing_company, billing_city, etc:

add_action( 'woocommerce_created_customer', 'b2bking_save_custom_registration_fields' );

function b2bking_save_custom_registration_fields( $user_id ) {
$field_value = sanitize_text_field( filter_input( INPUT_POST, 'billing_company' ) ); 
if ( $field_value !== NULL ){
update_user_meta( $user_id, 'billing_company', $field_value );
}
}

How can you create separate B2B and B2C form fields? You can add a “Select” field to registration in the way explained above and use a bit of JavaScript to determine whether the user chose “Individual” or “Company”. Show or hide registration fields like Company name depending on what the user chooses.

If you wish to avoid coding, there are some free plugin solutions to extend registration, such as https://wordpress.org/plugins/user-registration/ that also have options for multiple registration forms, though creating B2B-specific registration may require a little extra work on your side.

If you’re looking for a premium solution, B2BKing provides some handy, easy-to-use shortcodes that you can add to any page and create a business registration form

3. Wholesale order form

Business customers often know exactly what they want, down to the SKU, so adding a wholesale order form to your website makes it quick to order for your customers, and makes you look professional.

How can you add one? There’s no quick code snippet that can do this, so I think a plugin is your best solution.

There’s a free plugin that I personally tested, which looks and works great: https://wordpress.org/plugins/woocommerce-bulk-order-form/

B2BKing also has its own proprietary implementation, which you can see in the next image:

4. Wholesale price structure

The relevant question here is: how to set up different prices for different users? There are 2 ways to go about this: change the price directly, or add a discount.

To add a cart discount for a user or category of users, use this code:

add_action( 'woocommerce_cart_calculate_fees', 'b2bking_cart_discount' );

function b2bking_cart_discount( $cart ){
$cart->add_fee( 'B2B Discount', -10 );
}

The code above uses a bit of a trick by adding a negative fee, which is a discount. The code above doesn’t do much, it just adds a 10 dollar discount for all users. Let’s expand the code a little:

function b2bking_cart_discount( $cart ){
$user_id = get_current_user_id();
$user_status = get_user_meta( $user_id, 'user_status', true );
if ( $user_status === 'b2b' ){
$cart->add_fee( 'B2B Discount', -10 );
}
}

How’s this? Now the code checks if the user’s meta status is ‘b2b’ and gives a discount only to b2b users.

How do you set the meta status? You could set that on registration using the woocommerce_created_customer hook that I used above in the 2nd article section, and a simple line of code. The function update_user_meta is used for both updating and creating user meta.

update_user_meta( $user_id, 'user_status', 'b2b' );

What if you want to set complex structures of different prices for different products for different users?

This gets a little more complicated but you can use the same principles. In WooCommerce, a product is a “post”, and you can set post meta data for it. For example, you can add a post meta named b2b_price, to have a separate price for b2b users. Here’s the code.

update_post_meta( $post_id, 'b2b_price', 15 ); // 15 is the price for b2b users

How do you show this price to b2b users only?

add_filter('woocommerce_product_get_price', 'b2bking_fixed_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'b2bking_fixed_price', 99, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'b2bking_fixed_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'b2bking_fixed_price', 99, 2 );

function b2bking_fixed_price( $price, $product ) {
// check if the user is B2B or not
$current_user_id = get_current_user_id();
$current_user_status = get_user_meta( $current_user_id, 'user_status', true );
if ( $current_user_status !== 'b2b' ){
// if user is not b2b show the normal price
return $price;
} else {
// get the current product’s price for B2B users
$current_product_id = $product->get_id();
$b2b_price = get_post_meta( $current_product_id, 'b2b_price', true );
return $b2b_price;
}
}

5. Setting up tax exemptions

This one’s easier than you think! There’s a very handy function that you can use in WooCommerce that will do this for you and even take care of price display in most situations: set_is_vat_exempt() . Good name, right?

This function will even make it so that B2B users see prices with the suffix “excluding tax”, while B2C users see prices saying “including tax”.

add_action( 'init', 'b2bking_tax_exemption' );

function b2bking_tax_exemption(){
// first we check if the user is tax exempt
$tax_exempt = get_user_meta( get_current_user_id(), 'is_tax_exempt', true );
if ( $tax_exempt === 'exempt' ){
$customer = WC()->customer;
$customer->set_is_vat_exempt( true );
} else {
// the next line is only necessary if the user’s exempt status changes dynamically, such as based on billing country
$customer->set_is_vat_exempt( false ); 
}
}

B2BKing – WooCommerce B2B & Wholesale Plugin

I hope some of what I shared above will be helpful to you. Equipping WooCommerce with B2B functionalities is a complex task though, and I wouldn’t fault you if you decided buying a premium plugin is a better use of your time than writing the code yourself 🙂 Really, I won’t judge you at all.

With that in mind, I’ll share a few words about B2BKing. This is a project me and my team have been working on for a while, with the goal of turning WooCommerce into a capable B2B solution, an alternative to the costly SAAS platforms.

We’re constantly developing and supporting it as a long-term project, and it currently has more than 137 features including business registration, tax exemptions, dynamic pricing rules, VAT support, built-in messaging system, multiple buyers on account, offers, a dedicated B2B&B2C hybrid mode, and much, much more.

This week, we are proud to have been hand-picked by Envato as its Featured Plugin of the Week and showcased on the CodeCanyon front page.

We offer a live demo that you can test at any time, both backend and frontend: Live Demo

If you have any questions about the article, about the plugin, or just want to say hi, don’t be shy! Feel welcome to reach out to your friendly neighbourhood plugin developer.

You may also like