This article is for any developer, WP agency or non-coder business owner that is using WooCommerce for their project, but needs a way to set up complex wholesale pricing (which WooCommerce doesn't provide by default).
We'll look at all relevant solutions: plugin solutions, as well as writing custom code and the right hooks to use for custom development. We'll also share several code snippets.
Let's start with the easiest way: using a premium plugin. B2BKing is a complete wholesale and B2B plugin with over 137+ features, including:
B2BKing offers multiple methods to set wholesale prices, either directly, through CSV import or through discounts (product, category and bulk order discounts). The plugin is built for versatility and easy integration with any WooCommerce business, and you can even set discounts and prices for single specific users.
The first step is going to the groups panel and setting up business groups.
Go to "business groups" and click on "create new group". The following panel will open and you can also control which payment methods and shipping methods should be available to that group.
Once you have created a group, price fields for each group show up in the product page backend, for each product and for each variation. As you can see, it becomes incredibly simple to set prices for each group.
The process above can become cumbersome if you have a large number of products. That's why setting wholesale prices in B2BKing is done through meta-data. This means the plugin is compatible with any import/export tool such as WP All Import.
The plugin also has its own proprietary import/export tool that allows you export price lists, modify them directly in CSV/Excel and then import the file again to set prices.
Besides setting product price directly for each product, you can also create dynamic rules for discounts, which can be global discounts, category discounts or product/variation discounts. The plugins introduces a powerful dynamic pricing component which also supports quantity/value conditions.
The example above is a configuration for a 50% discount, for the product Xiaomi Smart Vacuum for the user spencerllc.
Through the "show discount everywhere" setting, you can choose whether the discount should be visible in cart, or visible directly in the product page. Let's see what a wholesale discount visible in the product page looks like:
The name of the discount, in this case "B2B Discount" is also visible in the product page.
When creating discounts, you can also choose conditions based on the quantity/value of the product, category, or cart total that is being ordered.
For example:
A discount of 10% for all wholesale users, when they order at least $1000 worth of products.
You can take this a step further and create complex tiered pricing structures.
For example:
This can be set up by creating dynamic rules for either discount or fixed price with 2 or more conditions.
Configuration example for $0.95 price for 200-1000 pieces ordered:
In the example above, price is set through a Fixed Price rule rather than a discount, which allows you to set a specific price for a product under certain conditions, for certain users or groups.
Update: B2BKing version 2.2.0 introduces Tiered Pricing in the Product Page + Pricing Table
In conclusion:
The options of setting price in the product page, through discounts, bulk discounts, fixed price rules, for groups or for individual users, give you an unparalleled level of flexibility that allows you to take on even the most complex projects.
If you are a developer exploring a wholesale or b2b project, custom coding it could be the right solution for you. Let's explore the relevant items, hooks and filters that you need to use to modify WooCommerce pricing.
The 2 hooks needed to set up regular price are
add_filter( 'woocommerce_product_get_regular_price', 'b2bking_dynamic_rule_discount_regular_price', 9999, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'b2bking_dynamic_rule_discount_regular_price', 9999, 2 );
function b2bking_dynamic_rule_discount_regular_price( $regular_price, $product ){
// run pricing logic
return $regular_price;
}
Sale price is more complex to generate dynamically, and the following filters are needed:
The price can be set up similarly to the regular price in the previous section
An important step when dealing with variable products in particular, is to clear the WooCommerce price cache, otherwise there will be a delay between setting the price and seeing the change displayed.
To clear the WooCommerce pricing cache use the following code:
add_filter( 'woocommerce_get_variation_prices_hash', 'b2bking_dynamic_rule_discount_sale_price_variation_hash', 99, 1);
function b2bking_dynamic_rule_discount_sale_price_variation_hash( $hash ) {
if ($custom_pricing_logic){
// clear cache
WC_Cache_Helper::get_transient_version( 'product', true );
}
$hash[] = get_current_user_id();
return $hash;
}
To display a sale price correctly, you should make use of the woocommerce_get_price_html filter and the wc_format_sale_price function.
add_filter( 'woocommerce_get_price_html', 'b2bking_dynamic_rule_discount_display_dynamic_price', 9999, 2 );
function b2bking_dynamic_rule_discount_display_dynamic_price( $price_html, $product ) {
// run pricing logic
$price_html = wc_format_sale_price( wc_get_price_to_display( $product,
array( 'price' => $product->get_regular_price() ) ),
wc_get_price_to_display( $product, array( 'price' => $product-
>get_sale_price() ) ) ) . $product->get_price_suffix();
return $price_html;
}
To modify the sale price in cart, the price must be set before totals are calculated, which is best done using the woocommerce_before_calculate_totals hook.
Prices in the minicart must also be updated separately, by using the woocommerce_cart_item_price filter