WooCommerce: Allow Customers To Define the Product Price

This is a great customization for those WooCommerce store owners who are willing to accept donations, custom amounts, or need anyway that the customer enters a custom price on the product page for paying an invoice or a bill.

This is as simple as creating a simple product with $0 price, and after that using the snippet below to display an input field on the single product page, where customers can enter their custom amount. Enjoy!

With this neat snippet, you can let customers pick their product price and add it to the cart. The system will automatically override the product price in the Cart / Checkout / Order.

PHP Snippet: Pick Your Product Price @ WooCommerce Single Product Page

Note: you first need to create a simple product called anything you like e.g. “Donation” or “Pay Your Bill”, give it a $0.00 price, and get its product ID so that this can be used in the code below (ID 241982 in my case).

 * @snippet       Customer Price Input @ WooCommerce Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/

add_action( 'woocommerce_before_add_to_cart_button', 'bbloomer_product_price_input', 9 );
function bbloomer_product_price_input() {
	global $product;
	if ( 241982 !== $product->get_id() ) return;
	woocommerce_form_field( 'set_price', array(
		'type' => 'text',
		'required' => true,
		'label' => 'Set price ' . get_woocommerce_currency_symbol(),
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_product_add_on_validation', 9999, 3 );
function bbloomer_product_add_on_validation( $passed, $product_id, $qty ) {
	if ( isset( $_POST['set_price'] ) && sanitize_text_field( $_POST['set_price'] ) == '' ) {
		wc_add_notice( 'Set price is a required field', 'error' );
		$passed = false;
	return $passed;
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_product_add_on_cart_item_data', 9999, 2 );
function bbloomer_product_add_on_cart_item_data( $cart_item, $product_id ) {
   if ( 241982 !== $product_id ) return $cart_item;    
	$cart_item['set_price'] = sanitize_text_field( $_POST['set_price'] );
	return $cart_item;

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_alter_price_cart', 9999 );
function bbloomer_alter_price_cart( $cart ) {
   if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
   if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
   foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
      $product = $cart_item['data'];
      if ( 241982 !== $product->get_id() ) continue;
      $cart_item['data']->set_price( $cart_item['set_price'] );

Is There a Plugin For That?

If you’d love to code but don’t feel 100% confident with PHP, I decided to look for a reliable plugin that achieves the same result. But in case you hate plugins and wish to code (or wish to try that), then skip this section.

1. Name Your Price Plugin (WooCommerce.com)

On top of allowing customers to pick their price, you can also hide the product price, enable it for multiple product types such as variable, grouped, bundle, composite, etc., make it compatible with WooCommerce Subscriptions, and much more.

2. WooCommerce Product Options Plugin (Barn2)

This comes with more than 10 field types that store owners can use to create extra product options for their customers including the ‘Customer-Defined Price’ field. This feature adds a price field where customers can type an additional amount to be added to the cost of the product. The plugin also comes with a Conditional Logic option so users can dynamically show/hide the price field depending on which other product options the customer has selected. 

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]
  • WooCommerce: Add Prefix / Suffix to Product Prices
    Sometimes you may want to add a prefix or a suffix to your prices. It could be something like “From…”, “Only…”, “…tax free” and so on. The first good news is this is very easy to do with a WooCommerce filter (remember, filters change the value of an existing variable, while actions add content). The […]
  • WooCommerce Conditional Logic – Tags, Examples & PHP
    The WooCommerce and WordPress conditional tags (“WooCommerce and WordPress Conditional Logic”) can be used in your functions.php to display content based on certain conditions. For example, you could display different content for different categories within a single PHP function.

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

12 thoughts on “WooCommerce: Allow Customers To Define the Product Price

  1. Nice – but when I tried it, I got

    “Donation” cannot be purchased. Please remove it from your cart.

    Any ideas?

    I need to be able to process donations for a user-created amount so this would be ideal if I can get it working.

    1. Not 100% sure, works for me

  2. This works great. I made a few notable changes to the code. Like adding a check to make sure the user entered price is actually a number.

    add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_product_add_on_validation', 9999, 3 );
    	function bbloomer_product_add_on_validation( $passed, $product_id, $qty ) {
    	   if ( isset( $_POST['set_price'] ) && sanitize_text_field( $_POST['set_price'] ) == '' ) {
    	      wc_add_notice( 'Amount is required', 'error' );
    	      $passed = false;
    	   } else if ( !is_numeric($_POST['set_price']) ) {
    	      wc_add_notice( 'Please enter a valid Amount', 'error' );
    	      $passed = false;
    	   return $passed;
  3. Great simple solution. Would be nice to maybe have some validation to stop people from adding to cart with a price value like “I love cheese” (or use a number field instead of a text field but then you’d still have to add some validation for allowing 0, 1 or 2 decimal places) but otherwise, awesome.

    Not super keen on the hardcoded ID but I can make that a variable + custom field / setting somewhere if I want easily enough using ACF or something.

    1. Very good points!

      For the price validation thing, you could check if the output is_numeric()

      For the hardcoded product ID, you could either set a global, and then call it inside each one – or look into the use() function statement for passing a variable that was defined earlier: https://stackoverflow.com/a/11086831/4110665

  4. Hi, great and works well. Is there a simple way to make this field visible when woocommerce product is called via shortcode. For instance [[product id="7108"]] will return price: 0 and add to cart by default but not your field. Thanks!

    1. Hello Mantas, I guess you refer to the [[product_page id="123"]] shortcode, which shows a product’s single product page? In that case, my code works perfectly.

      The [[product]] shortcode you mentioned shows a product loop with only 1 product, and not the single product page.

  5. Hello,

    I like this but I need to add custom note to below the cart button.

    I want to add this note to only this product id.

    1. Hello Arun, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Nice work. The snippet nicely explains what is a rather complicated data flow. Thanks, James

Questions? Feedback? Customization? Leave your comment now!

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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