Allowing customers to define the product price in your WooCommerce store can be a powerful way to engage with your audience. Whether you’re running a donation-based store, offering a pay-what-you-want model, or testing a new pricing strategy, this guide will show you how to achieve this without relying on plugins. All you need is some simple custom code added to your theme’s functions.php file.
Using the custom code method ensures you have full control over how this feature works and integrates seamlessly with your theme. If you’re unfamiliar with editing your theme’s files, consider using a free WordPress child theme generator to create a child theme, preserving your customisations during theme updates.
Step 1: Adding a Custom Price Field to the Product Page
To allow customers to define the product price, you first need to add a custom field on the product page where they can enter their desired amount. This is achieved by adding the following code to your theme’s functions.php file:
/**
* Snippet: How to Allow Customers to Define the Product Price in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1423
* Tested with WooCommerce 10.7.0
* "Add custom price input field on the product page"
*/
function wc_success_custom_price_field() {
echo '<div class="custom-price">';
echo '<label for="custom_price">'.__('Enter Your Price', 'woocommerce').'</label>';
echo '<input type="number" name="custom_price" id="custom_price" value="" step="0.01" min="0" />';
echo '</div>';
}
add_action('woocommerce_before_add_to_cart_button', 'wc_success_custom_price_field');
This code snippet creates a new input field on the product page that allows customers to input their own price.
Step 2: Processing the Custom Price on Add to Cart
After adding the custom price field, the next step is to ensure that the price entered by the customer is processed correctly when they add the product to the cart. Add the following code to handle this:
/**
* Snippet: How to Allow Customers to Define the Product Price in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1423
* Tested with WooCommerce 10.7.0
* "Capture and validate the custom price entered by the customer"
*/
function wc_success_add_custom_price_to_cart($cart_item_data, $product_id) {
if (isset($_POST['custom_price'])) {
$custom_price = (float) sanitize_text_field($_POST['custom_price']);
if ($custom_price > 0) {
$cart_item_data['custom_price'] = $custom_price;
}
}
return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'wc_success_add_custom_price_to_cart', 10, 2);
This function captures the custom price when the product is added to the cart and saves it within the cart item data.
Step 3: Displaying the Custom Price in the Cart
Once the custom price is in the cart, we need to display it correctly. The following code ensures the custom price appears in the cart instead of the default product price:
/**
* Snippet: How to Allow Customers to Define the Product Price in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1423
* Tested with WooCommerce 10.7.0
* "Override the default product price in the cart with the custom price"
*/
function wc_success_custom_price_in_cart($cart_object) {
foreach ($cart_object->get_cart() as $cart_item) {
if (isset($cart_item['custom_price'])) {
$cart_item['data']->set_price($cart_item['custom_price']);
}
}
}
add_action('woocommerce_before_calculate_totals', 'wc_success_custom_price_in_cart');
This function updates the product price in the cart based on the custom price the customer provided.
Step 4: Reflecting the Custom Price on Checkout and Order Details
Finally, we need to ensure that the custom price is reflected on the checkout page, order details, and in the email notifications. The following code achieves this:
/**
* Snippet: How to Allow Customers to Define the Product Price in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1423
* Tested with WooCommerce 10.7.0
* "Ensure the custom price is displayed on checkout and order details"
*/
function wc_success_custom_price_display($price, $cart_item, $cart_item_key) {
if (isset($cart_item['custom_price'])) {
$price = wc_price($cart_item['custom_price']);
}
return $price;
}
add_filter('woocommerce_cart_item_price', 'wc_success_custom_price_display', 10, 3);
add_filter('woocommerce_order_item_get_formatted_meta_data', 'wc_success_custom_price_display', 10, 3);
This code snippet makes sure the custom price is shown throughout the customer’s journey, from adding the product to their cart, checking out, and receiving order confirmations.
Conclusion
By following these steps, you can easily allow customers to define the product price on your WooCommerce store without relying on additional plugins. The custom code provided is simple to implement and ensures your store retains flexibility while offering this unique pricing option.
If you are new to editing your theme’s functions.php file or wish to keep your changes safe during theme updates, consider using our free WordPress child theme generator. It allows you to create a child theme quickly and easily, ensuring your customisations remain intact.
Feel free to explore our other free tools like the wp-config file generator, .htaccess file generator, robots.txt generator, and the bad bot blocker generator to further enhance your WordPress and WooCommerce site.
Allowing customers to define the product price can be a game-changer for your WooCommerce store, fostering engagement and driving conversions. Happy coding!
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

