Basic customer specific pricing snippet for WooCommerce

Do you need to treat your best customer to a discount on a WooCommerce product? Or, maybe you want to increase the price of your products for your worst customer? Whatever your situation, if you need to change the price of your product to something higher or lower than the current price for a specific customer, then you’re at the right place. In today’s ecommerce environment, implementing customer specific pricing can improve sales and loyalty.

In this snippet, we’ll show you how to do exactly that. And the good news is that it’s really easy to do.

Step 1: Find the customer ID to apply the customer specific pricing to

We firstly need to know the ID of the customer that we want to apply the custom pricing to. So log in to WooCommerce, and click Users on the sidebar, then find and click on the user you would like to apply the pricing to.

Once you’ve reached the edit page of that customer, take a look at your browsers URL bar and you’ll see that there is a parameter that specifies the ID of the customer. The format of the parameter will look like this: user_id=123 where the ID is 123. See the screenshot below for an example.

Customer Specific Pricing - User ID From URL

Step 2: Add a new woocommerce_product_get_price filter to override the price for the specific customer

Now on to the actual code itself. In the snippet below, all you need to do to enable customer specific pricing is update the User ID as indicated by the comment, and replace it with the User ID that you fetched in the previous step.

<?php
/**
 * Adds customer specific pricing for a given User ID.
 *
 * @param float $price The current price of the item.
 * @return float
 */
function thenga_customer_specific_pricing( $price ) {
	// Update the ID here to the ID of the customer you would like to apply the special price to.
	$special_customer_id = 123;

	if ( get_current_user_id() === $special_customer_id ) {
		// Give the customer a 20% discount.
		return $price * 0.8;
	}

	return $price;
}

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

Step 3: Add to your functions.php file, save and upload

All that’s left to do now, is add the code to your functions.php file in your theme, save it and upload it to your server. You have now enabled a very basic form of customer specific pricing. You can extend the snippet to only adjust the price of a specific product by changing the arguments passed by woocommerce_product_get_price to 2. This will pass the product object through to our custom function and you can use that to advance your logic on when to change the price or set a specific price.

If you’re looking for something less advanced than a code snippet, check out our pricing products for WooCommerce that do all the heavy lifting for you. All that is needed is for you to install the plugin and apply the rules on the settings page.

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.