Creating a dynamic pricing system in WooCommerce without relying on plugins can give you finer control over pricing mechanisms and reduce dependency on third-party software. This guide will show you how to implement custom dynamic pricing rules directly in your WooCommerce store using code. We’ll cover several scenarios including price changes based on cookies, user login status, and previous purchase behavior.
Understanding Dynamic Pricing
Dynamic pricing involves adjusting the prices of products in real-time based on various criteria. For eCommerce, these criteria can include customer behavior, market demand, inventory levels, and special promotions. Implementing this directly through code involves using hooks and filters provided by WooCommerce and WordPress.
Implementing Dynamic Pricing Without a Plugin
Creating a dynamic pricing system without a plugin requires a deep understanding of WooCommerce hooks and careful planning to ensure that the changes do not adversely affect the performance and scalability of your store.
Scenario 1: Price Based on User Login Status
Offering different prices based on whether a customer is logged in can encourage users to create accounts, potentially leading to increased loyalty and sales.
/*
* Snippet: How to Create a Dynamic Pricing System in WooCommerce without Using a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1158
* Tested with WooCommerce 10.7.0
* "Apply different pricing for logged in and logged out users"
*/
function wcsuccess_logged_in_user_pricing($price, $product) {
if (is_user_logged_in()) {
// Apply a 10% discount for logged-in users
return $price * 0.90;
}
return $price;
}
add_filter('woocommerce_product_get_price', 'wcsuccess_logged_in_user_pricing', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'wcsuccess_logged_in_user_pricing', 10, 2);
Scenario 2: Price Adjustment Based on Previous Purchase Behavior
Adjusting prices based on how much a customer has spent or what they have bought in the past can be a powerful tool for increasing customer retention.
/*
* Snippet: How to Create a Dynamic Pricing System in WooCommerce without Using a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1158
* Tested with WooCommerce 10.7.0
* "Adjust prices based on customer's purchase history"
*/
function wcsuccess_adjust_price_based_on_history($price, $product) {
$user_id = get_current_user_id();
$total_spent = wc_get_customer_total_spent($user_id);
if ($total_spent > 1000) {
// Apply a 5% discount for customers who have spent over $1000
return $price * 0.95;
}
return $price;
}
add_filter('woocommerce_product_get_price', 'wcsuccess_adjust_price_based_on_history', 20, 2);
add_filter('woocommerce_product_variation_get_price', 'wcsuccess_adjust_price_based_on_history', 20, 2);
Scenario 3: Dynamic Pricing Based on Cookies
Changing prices based on data stored in cookies can allow you to personalize prices based on user behavior or characteristics identified in previous visits.
Setting the “SpecialPromo” Cookie
To implement dynamic pricing based on a cookie, first, you need to set this cookie under specific conditions, such as during a promotional campaign. Here’s how you can set the “SpecialPromo” cookie when a user visits a specific URL or a promotional page.
Example PHP Code to Set a Cookie:
/*
* Snippet: Set "SpecialPromo" Cookie
* Description: Sets a cookie when a user visits a promotional page or through a specific URL parameter.
*/
function wcsuccess_set_special_promo_cookie() {
if (isset($_GET['promo']) && $_GET['promo'] == 'special2024') {
setcookie('SpecialPromo', 'Engaged', time() + 86400, '/'); // Cookie expires in 1 day
wp_redirect(remove_query_arg('promo'));
exit;
}
}
add_action('init', 'wcsuccess_set_special_promo_cookie');
This script checks for a URL parameter (promo=special2024). If this parameter is present, it sets a cookie named SpecialPromo with the value Engaged. The cookie is set to expire in 24 hours and is available across the entire site.
/*
* Snippet: How to Create a Dynamic Pricing System in WooCommerce without Using a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1158
* Tested with WooCommerce 10.7.0
* "Implement price changes based on cookies"
*/
function wcsuccess_price_based_on_cookies($price, $product) {
if (isset($_COOKIE['SpecialPromo']) && $_COOKIE['SpecialPromo'] == "Engaged") {
// Apply a 15% discount if a special promo cookie is set
return $price * 0.85;
}
return $price;
}
add_filter('woocommerce_product_get_price', 'wcsuccess_price_based_on_cookies', 30, 2);
add_filter('woocommerce_product_variation_get_price', 'wcsuccess_price_based_on_cookies', 30, 2);
Dynamic Pricing for Return Visitors
Now, let’s create a scenario where the price changes for a returning visitor who has not completed a checkout. This example assumes you track such visitors with a cookie indicating they’ve added items to their cart but left the site without completing the purchase.
Example PHP Code to Modify Prices for Return Visitors:
/*
* Snippet: Adjust Prices for Return Visitors
* Description: Provides a discount to visitors who returned to the site but haven't completed the checkout.
*/
function wcsuccess_adjust_price_for_return_visitors($price, $product) {
if (isset($_COOKIE['ReturnVisitor']) && $_COOKIE['ReturnVisitor'] == "NotPurchased") {
// Apply a 10% discount to encourage purchase completion
return $price * 0.90;
}
return $price;
}
add_filter('woocommerce_product_get_price', 'wcsuccess_adjust_price_for_return_visitors', 20, 2);
add_filter('woocommerce_product_variation_get_price', 'wcsuccess_adjust_price_for_return_visitors', 20, 2);
/*
* Set "ReturnVisitor" Cookie on Cart Addition
*/
function wcsuccess_set_return_visitor_cookie() {
if (!is_admin() && WC()->cart->get_cart_contents_count() > 0) {
setcookie('ReturnVisitor', 'NotPurchased', time() + 3600, '/'); // Cookie expires in 1 hour
}
}
add_action('wp', 'wcsuccess_set_return_visitor_cookie');
In this code:
- The first function applies a 10% discount if a
ReturnVisitorcookie is present and set toNotPurchased. This encourages users to complete their purchases by offering them a slight discount. - The second function sets the
ReturnVisitorcookie whenever a product is added to the cart and the user is not an admin. This cookie also expires in 1 hour, assuming that if the visitor doesn’t purchase within this time, they might need extra motivation to return and complete the purchase.
Testing Your Dynamic Pricing System
- Unit Tests: Write PHP unit tests to check that your pricing functions return the correct values under different conditions.
- Browser Tests: Use browser testing tools to simulate user interactions and check that prices update correctly based on the scenarios set.
- Performance Tests: Ensure that the custom code does not significantly impact site performance, especially during high traffic.
Conclusion
Implementing a dynamic pricing system in WooCommerce without a plugin provides flexibility and reduces potential bloat from third-party plugins. This approach requires careful planning and testing but can result in a highly customized and efficient pricing system that directly addresses your business needs.
Further Considerations
Maintain clear documentation of all customizations for future reference and potential troubleshooting. Regularly update your code to adhere to WooCommerce and WordPress updates to ensure compatibility and security.
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


For whatever reason, using the filters:
add_filter( ‘woocommerce_product_get_price’, ‘custom_discount_price’, 10, 2 );
add_filter( ‘woocommerce_product_variation_get_price’, ‘custom_discount_price’, 10, 2 );
Does a great job of setting the prices with a custom discount on product pages but when goes to cart it applies the discount a second time.
Hi Jim, apologies for the late reply. Are you using all code block and applying specials for when a user is logged in along with a URL parameter deal? Under those circumstances it will apply to discounts