FB Pixel

Display product-specific role-based prices with the Dynamic Pricing extension

The Dynamic Pricing extension for WooCommerce is a great plugin that allows you to change prices on the fly under a number of different circumstances. One way in which we see this plugin being used quite often is per-product pricing for registered customers with a specific role – wholesalers, for example. However, when this approach is used the adjusted price is not displayed in the catalog – only in the cart. We’re going to fix that.

The Scenario

You’re using the Dynamic Pricing extension on your WooCommerce shop to give a special subset of your customers different pricing for specific products. For example, your best flavour of granola (peach) has a regular price of $8, but for Wholesalers you’ve defined a dynamic price of $6. When Wholesale customers shop, they still see the regular price, until their special price is displayed in the cart.

The Fix

We’re going to hook into the woocommerce_get_price action which is applied when the price of every product is calculated. Our hook function will receive two arguments: the retail price and the product as an object. We’ll first abort the price change in the admin dashboard (line 5) and the cart (line 7). Then, we’ll check if the current product has pricing rules defined (line 11). If it does, we’ll get the current user’s details (line 14) and then check each rule to see if it’s role-based (line 17). If it is (line 19), and the current user has the same role (line 22), we’ll add the rule price to an array (line 23). We’ll then use the lowest price from this array instead of the retail price.

add_action( 'woocommerce_get_price', 'gowp_display_role_price', 10, 2 );  function gowp_display_role_price( $price, $product ) {    	// we don't want to modify prices in the admin dashboard  	if ( is_admin() ) return $price;    	// we'll let Dynamic Pricing do its own thing on the cart  	if ( is_cart() ) return $price;    	// does the product have any pricing rules defined?  	if ( $pricing_rules = get_post_meta( $product->id, '_pricing_rules', TRUE ) ) {    		// get the current user's details  		$current_user = wp_get_current_user();    		// find all role-based rules that apply to our user and build an array of discounted prices  		foreach ( $pricing_rules as $pricing_rule ) {  			$conditions = $pricing_rule['conditions'];  			foreach ( $conditions as $key => $condition ) {  				$roles = $condition['args']['roles'];  				foreach ( $roles as $role ) {  					if ( in_array( $role, $current_user->roles ) ) {  						$role_rules[] = $key;  					}  				}  			}  			foreach ( $role_rules as $role_rule ) {  				$role_prices[] = $pricing_rule['rules'][$role_rule]['amount'];  			}  		}    		// use the lowest of available discounted prices  		if ( $role_prices ) {  			asort($role_prices);  			$price = array_shift( $role_prices );  		}    	}    	return $price;    }

The Landing Page Builds service includes:

  • Unlimited page builds for one monthly rate
  • Dedicated account manager
  • Dedicated WordPress developer
  • At least 2 hours of daily dev time
  • Daily progress reports
  • Find out more here

The Content Edits Plan includes:

  • Unlimited content edits
  • White label help desk
  • Support ticket dashboard
  • 24/7 team of WordPress experts
Plus, everything in our Maintenance Plan:
  • Visual Validator WordPress updates
  • 90 days of off-site backups
  • Daily security scans and malware cleanup
  • Maintenance dashboard
  • Find out more here

The Maintenance Plan includes:

  • Visual Validator WordPress updates
  • 90 days of off-site backups
  • Daily security scans and malware cleanup
  • Maintenance dashboard
  • Find out more here