Show a different price to multiple WooCommerce customers

In a previous snippet we looked at how to set up customer specific pricing for a single customer in WooCommerce. But what if you wanted to show a different price for multiple customers? Well, it’s just as easy and in this snippet we will show you how to do just that.

We’re going to use a similar code base as the previous example but modify it for multiple customers.

Step 1: Find the customer ID’s for the different prices

We firstly need to get a list of customers who we would like to show a special price to. We can use the WordPress admin interface for this. Click on Users on the admin sidebar and search the list of users for the ones that you would like to show a custom price to. On each user that you select, click them and fetch the user ID from the URL parameter as per the previous example. Below is a screenshot that will help you identify where to find the user ID once you’ve clicked on the user from the user list.

Different Price For Multiple Users - User ID From URL

Step 2: Use the woocommerce_product_get_price to show a different price for the customer group

Below is the code snippet that you should modify. The most important piece is the PHP array which contains a list of user ID’s. Replace the list in the snippet with your list of users that you’ll be giving a different price to.

<?php
/**
 * Set a different price for multiple different WooCommerce customers.
 *
 * @param float $price The current price of the item.
 * @return float
 */
function thenga_customer_specific_pricing( $price ) {
   // Add your list of user ID's here.
   $special_customer_ids = [ 1, 2, 3 ];

   if ( in_array( get_current_user_id(), $special_customer_ids, true ) ) {
      // Give these customers a 20% discount.
      return $price * 0.8;
   }

   return $price;
}

add_filter( 'woocommerce_product_get_price', 'thenga_customer_specific_pricing', 10, 1 );

Step 3: Add the customized snippet to your functions.php and save

Once you’ve updated the customer ID list, save the snippet to your functions.php file and upload it to your server.

This snippet can be extended to only change the price of a specific product or multiple products by changing the amount of arguments that get passed via woocommerce_product_get_price to 2. This then passes through the product object which you can use to adjust your conditional logic.

If you prefer not to have to write the code yourself, then check out some of our WooCommerce pricing products.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.