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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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. 

Was this article helpful?
YesNo

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

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

  1. 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.

  2. 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!

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

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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