The code snippet provided below demonstrates how to implement wholesale pricing functionality in WooCommerce using custom PHP functions. This functionality can be useful for e-commerce websites that want to offer special pricing to wholesale customers.
To achieve this, you can create a custom function that hooks into the woocommerce_product_get_price
filter. This filter allows you to modify the product price before it is displayed on the front-end. In the example below, we’ll reduce the product price by 10% for wholesale customers:
function wpsnippets_wholesale_pricing( $price, $product ) {
// Check if the current user is a wholesale customer
if ( is_user_wholesale() ) {
// Calculate the discounted price
$discount = $price * 0.1;
$wholesale_price = $price - $discount;
// Return the wholesale price
return $wholesale_price;
}
// Return the original price for non-wholesale customers
return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_wholesale_pricing', 10, 2 );
In this code snippet, we first check if the current user is a wholesale customer by calling the is_user_wholesale()
function (which you would need to define). If the user is a wholesale customer, we calculate the discounted price by subtracting 10% from the original price. Finally, we return the wholesale price.
Note that the woocommerce_product_get_price
filter passes two parameters: the original price and the product object. We use these parameters to calculate and return the appropriate price based on the user’s role.
Remember to replace is_user_wholesale()
with your own function that checks if the current user is a wholesale customer. This function could use various criteria to determine the user’s role, such as a specific user role or a custom user meta field.
By implementing this code snippet, you can offer wholesale pricing to specific customers on your WooCommerce store.
Examples
Example 1: Setting up wholesale pricing for specific user roles
This use case demonstrates how to set up wholesale pricing for specific user roles in WooCommerce. The code example below uses the woocommerce_product_get_price
filter to modify the product price based on the user role.
function wpsnippets_wholesale_pricing( $price, $product ) {
// Check if user has wholesale role
if ( in_array( 'wholesale_customer', wp_get_current_user()->roles ) ) {
// Modify the product price for wholesale customers
$price = $product->get_regular_price() * 0.8; // 20% discount
}
return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_wholesale_pricing', 10, 2 );
In this code example, we define a custom function wpsnippets_wholesale_pricing
that takes the current price and product object as parameters. We check if the current user has the “wholesale_customer” role using wp_get_current_user()->roles
. If the user has the wholesale role, we modify the price by applying a 20% discount. Finally, we return the modified price.
Example 2: Displaying wholesale price on product pages
This use case demonstrates how to display the wholesale price on product pages in WooCommerce. The code example below uses the woocommerce_get_price_html
filter to modify the displayed price based on the user role.
function wpsnippets_display_wholesale_price( $price_html, $product ) {
// Check if user has wholesale role
if ( in_array( 'wholesale_customer', wp_get_current_user()->roles ) ) {
// Get the wholesale price
$wholesale_price = $product->get_regular_price() * 0.8; // 20% discount
// Format and display the wholesale price
$price_html = '<span class="woocommerce-Price-amount amount">' . wc_price( $wholesale_price ) . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'wpsnippets_display_wholesale_price', 10, 2 );
In this code example, we define a custom function wpsnippets_display_wholesale_price
that takes the price HTML and product object as parameters. We check if the current user has the “wholesale_customer” role using wp_get_current_user()->roles
. If the user has the wholesale role, we calculate the wholesale price by applying a 20% discount. We then format the wholesale price using wc_price
and replace the original price HTML with the wholesale price.
Example 3: Applying wholesale pricing to specific product categories
This use case demonstrates how to apply wholesale pricing to specific product categories in WooCommerce. The code example below uses the woocommerce_product_get_price
filter to modify the product price for products in specific categories.
function wpsnippets_wholesale_pricing_by_category( $price, $product ) {
// Define the wholesale product categories
$wholesale_categories = array( 'clothing', 'accessories' );
// Check if product belongs to a wholesale category
if ( has_term( $wholesale_categories, 'product_cat', $product->get_id() ) ) {
// Modify the product price for wholesale categories
$price = $product->get_regular_price() * 0.8; // 20% discount
}
return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_wholesale_pricing_by_category', 10, 2 );
In this code example, we define a custom function wpsnippets_wholesale_pricing_by_category
that takes the current price and product object as parameters. We define an array of wholesale product categories. We then check if the product belongs to any of the wholesale categories using has_term
and the product_cat
taxonomy. If the product belongs to a wholesale category, we modify the price by applying a 20% discount. Finally, we return the modified price.