How to Let Customers Set Their Own Product Price in WooCommerce

How to Let Customers Set Their Own Product Price in WooCommerce
On this page
  1. Why Allow a Customer-Defined Price
  2. Step 1: Create the Base Product
  3. Step 2: Add the Price Input Field
  4. Troubleshooting
  5. Frequently Asked Questions
  6. Conclusion

You can let customers set their own product price in WooCommerce by adding a text input field to a $0 simple product, validating that it’s filled in, and overriding the cart item price with whatever amount the customer enters — ideal for donations, custom invoices, or pay-what-you-want products.

Why Allow a Customer-Defined Price

  • Accepting donations of any amount through a normal WooCommerce checkout flow
  • Billing a customer for a custom invoice amount without creating a new product for every job
  • Running a pay-what-you-want promotion for a digital product or service

Step 1: Create the Base Product

Create a simple product (e.g. “Custom Payment”) with a regular price of $0.00, and note its product ID.

Step 2: Add the Price Input Field

PHP
/** Show a custom price input field on one specific $0 product */
add_action( 'woocommerce_before_add_to_cart_button', 'show_custom_price_input_field', 9 );

function show_custom_price_input_field() {
    global $product;
    if ( 51290 !== $product->get_id() ) return; // Replace with your product ID

    woocommerce_form_field( 'custom_price', array(
        'type'     => 'text',
        'required' => true,
        'label'    => 'Enter amount ' . get_woocommerce_currency_symbol(),
    ) );
}

add_filter( 'woocommerce_add_to_cart_validation', 'validate_custom_price_input', 9999, 3 );

function validate_custom_price_input( $passed, $product_id, $qty ) {
    if ( 51290 === $product_id && empty( $_POST['custom_price'] ) ) {
        wc_add_notice( 'Please enter an amount.', 'error' );
        $passed = false;
    }
    return $passed;
}

add_filter( 'woocommerce_add_cart_item_data', 'store_custom_price_in_cart_item', 9999, 2 );

function store_custom_price_in_cart_item( $cart_item, $product_id ) {
    if ( 51290 === $product_id && isset( $_POST['custom_price'] ) ) {
        $cart_item['custom_price'] = wc_format_decimal( sanitize_text_field( $_POST['custom_price'] ) );
    }
    return $cart_item;
}

add_action( 'woocommerce_before_calculate_totals', 'apply_custom_price_to_cart_item', 9999 );

function apply_custom_price_to_cart_item( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    foreach ( $cart->get_cart() as $cart_item ) {
        if ( isset( $cart_item['custom_price'] ) ) {
            $cart_item['data']->set_price( $cart_item['custom_price'] );
        }
    }
}
Let customers set their own product price in WooCommerce: the page before adding the code
Before
Let customers set their own product price in WooCommerce: the page after adding the code
After

Troubleshooting

Customers Enter Non-Numeric or Negative Values

Add stricter validation in validate_custom_price_input() — check that the value is numeric and greater than zero (or your defined minimum) before allowing it to pass.

The Price Resets Unexpectedly

The did_action() guard prevents this function from running more than twice per request, avoiding a common WooCommerce bug where totals recalculate multiple times and reset custom pricing — leave that check in place.

Frequently Asked Questions

Can I set a minimum or maximum amount?

Yes, add a numeric range check inside the validation function and show an error notice if the entered value falls outside your allowed bounds.

Does this work with subscriptions or recurring payments?

Not directly — recurring billing plugins usually need their own compatible way of handling variable amounts, since they calculate future charges differently from a one-time cart price override.

Will the custom price show correctly on the invoice and emails?

Yes, since the price is set on the cart item before the order is created, it flows through normally into order totals, emails, and invoices.

Conclusion

Letting customers set their own product price in WooCommerce takes a simple input field, validation, and a cart price override — no extra plugin required for straightforward donation or pay-what-you-want use cases.

See also How to Let a Customer Purchase a WooCommerce Product Only Once. Then read How to Let Customers Request a Quote in WooCommerce.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call