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!
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.Â
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.
Not 100% sure, works for me
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.
Thanks!
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.
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
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!
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.
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.
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!
Nice work. The snippet nicely explains what is a rather complicated data flow. Thanks, James
Thank you!