WooCommerce have two pricing method for Products – Regular
and Sale
price. These methods are used regardless of registered users or visitors. Now, what if you wanna add pricing for some specific user role ? there is plugin for that ofcourse. But how can you make it without using any plugin ? Well, we have a solution for that, and this article describe that.
Adding some PHP codes, we can add an extra pricing method, lets call it Wholesale Price. And then set this price for a specific user role. In this article we are calling the new pricing as – Wholesale Pricing & Editor is the specific user role to whom the price will be applicable for.
Table of Contents
- Determine if current user can avail the Wholesale Pricing
- Get the Wholesale Pricing value for a Product
- Add Wholesale price for Simple product
- Add Wholesale price for Variable product
Determine if current user can avail the Wholesale Pricing
// this checks if the current user is capable to have the wholesale pricing
function w4dev_wholesale_applicable() {
return (bool) ( current_user_can('editor') && ( !is_admin() || is_ajax() ) );
}
Get the Wholesale Pricing value for a Product
// this get the wholesale price when available for both Simple & Variable product type
function w4dev_get_wholesale_price( $product ) {
if (
$product->is_type( array('simple', 'variable') )
&& get_post_meta( $product->id, '_wholesale_price', true ) > 0
) {
return get_post_meta( $product->id, '_wholesale_price', true );
} elseif (
$product->is_type('variation')
&& get_post_meta( $product->variation_id, '_wholesale_price', true ) > 0
) {
return get_post_meta( $product->variation_id, '_wholesale_price', true );
}
return 0;
}
Wholesale Pricing for Simple Product
Step One – Adding the input field
First, we need to add Wholesale Pricing input to the product editing page. Here’s the code –
function w4dev_woocommerce_product_options_pricing() {
woocommerce_wp_text_input( array(
'id' => '_wholesale_price',
'class' => 'wc_input_wholesale_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')',
'type' => 'text'
));
}
add_action( 'woocommerce_product_options_pricing', 'w4dev_woocommerce_product_options_pricing' );
After you have added this in your themes functions.php or plugin file, you would see a new input appears under the sale price input (note: Simple Product only).
Step Two – Saving the price
Next we need to save the wholesale price value.
add_action( 'woocommerce_process_product_meta_simple', 'w4dev_woocommerce_process_product_meta_simple', 10, 1 );
function w4dev_woocommerce_process_product_meta_simple( $product_id ) {
if( isset($_POST['_wholesale_price']) && $_POST['_wholesale_price'] > 0 )
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );
}
This code will save the entered value for wholesale price with the product with a custom meta ‘_wholesale_price’.
Step Three – Frontend pricing filter
Now, to Assign the wholesale price for ‘Editor’ we will need to hook into woocommerce_get_price filter.
add_filter( 'woocommerce_get_price', 'w4dev_woocommerce_get_price', 10, 2);
function w4dev_woocommerce_get_price( $price, $product ) {
if ( w4dev_wholesale_applicable() && w4dev_get_wholesale_price($product) > 0 ) {
$price = w4dev_get_wholesale_price($product);
}
return $price;
}
All done, Now you can check how it is working by login with any Editor’s credentials and you should see the product is valued with the wholesale price rather than the sale/regular price. However, all other user/customer should get the sale price as usual.
After you have added this in your themes functions.php or plugin file, you would see a new input appears under the sale price input (note: Simple Product only).
Wholesale Pricing for Variable Product
The above described method is only for Simple Product. The same feature can be added for Variable Product also, read further below.
Step One – Adding the input field
add_action( 'woocommerce_product_after_variable_attributes', 'w4dev_woocommerce_product_after_variable_attributes', 10, 3 );
function w4dev_woocommerce_product_after_variable_attributes( $loop, $variation_data, $variation ){ ?>
<tr class="wholesale_price_row">
<td>
<div>
<label><?php _e( 'Wholesale Price:', 'woocommerce' ); ?></label>
<input type="text" size="5" name="variable_wholesale_price[<?php echo $loop; ?>]" value="<?php if ( isset( $variation_data['_wholesale_price'][0] ) ) echo esc_attr( $variation_data['_wholesale_price'][0] ); ?>" step="1" min="0" />
</div>
</td>
</tr><?php
}
Step Two – Saving the price
add_action( 'woocommerce_save_product_variation', 'w4dev_woocommerce_save_product_variation', 10, 2 );
function w4dev_woocommerce_save_product_variation($variation_id, $i)
{
if( isset($_POST['variable_wholesale_price'][$i]) )
update_post_meta( $variation_id, '_wholesale_price', $_POST['variable_wholesale_price'][$i]);
}
Step Three – Frontend pricing filter
The first function w4dev_woocommerce_get_price()
and the filter woocommerce_get_price
is the same as we have used for the simple product. So, if you add this pricing method for both product type, just ignore the next function and hook.
if ( ! function_exists( 'w4dev_woocommerce_get_price' ) ) {
function w4dev_woocommerce_get_price( $price, $product ) {
if( w4dev_wholesale_applicable() && w4dev_get_wholesale_price($product) > 0 )
$price = w4dev_get_wholesale_price($product);
return $price;
}
}
add_filter( 'woocommerce_get_price', 'w4dev_woocommerce_get_price', 10, 2);
function w4dev_woocommerce_variation_prices_html( $price, $product ) {
if ( isset( $product->product_custom_fields['_wholesale_price'][0] ) ) {
$wholesale_price = $product->product_custom_fields['_wholesale_price'][0];
} else {
$wholesale_price = '';
}
if ( w4dev_wholesale_applicable() && $wholesale_price > 0 ) {
return woocommerce_price( $wholesale_price );
}
return $price;
}
add_filter( 'woocommerce_variation_price_html', 'w4dev_woocommerce_variation_prices_html', 10, 2);
add_filter( 'woocommerce_variation_sale_price_html', 'w4dev_woocommerce_variation_prices_html', 10, 2);
Thanks.