Dynamic pricing in an online store is a powerful tool to incentivise customers to buy more products by offering discounts that are triggered by the cart’s contents. In this tutorial, you’ll learn how to implement WooCommerce dynamic pricing based on cart contents, which can include varying discount levels based on the quantity or type of products added to the cart.
Prerequisites
Before starting, ensure you have administrative access to your WordPress site and are comfortable with editing PHP files. Implement these changes in a child theme to avoid losing them during theme updates.
Step 1: Define Your Pricing Rules
First, decide on the pricing rules you want to apply. For example, you might offer a 10% discount if a customer buys more than three items, or specific product combinations might trigger a discount. In this tutorial, we’ll focus on a bulk purchase discount.
Step 2: Add Custom Code to Your Theme’s functions.php File
To implement WooCommerce dynamic pricing based on cart contents, add the following code to your theme’s functions.php
file:
/*
* Snippet: WooCommerce Dynamic Pricing Based on Cart Contents – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?979
* Tested with WooCommerce 8.8.3
*/
add_action('woocommerce_before_cart', 'wcsuccess_apply_dynamic_pricing');
add_action('woocommerce_before_checkout_form', 'wcsuccess_apply_dynamic_pricing');
function wcsuccess_apply_dynamic_pricing() {
// Ensure WC Cart is loaded
if (is_admin() || !WC()->cart) {
return;
}
$discount_threshold = 3; // The threshold quantity for a discount
$discount_percentage = 10; // The discount percentage
$cart_items_count = WC()->cart->get_cart_contents_count();
if ($cart_items_count >= $discount_threshold) {
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$original_price = $product->get_regular_price();
$discounted_price = $original_price - ($original_price * ($discount_percentage / 100));
$product->set_price($discounted_price);
}
}
}
Explanation of the Code
- Hooks: We use
woocommerce_before_cart
andwoocommerce_before_checkout_form
to apply the dynamic pricing when the cart or checkout page is loaded. - Discount Logic: The function checks the total number of items in the cart. If this number meets or exceeds the discount threshold, a discount is applied to each item in the cart based on the defined percentage.
- Price Adjustment: The price of each product in the cart is adjusted to reflect the discount.
Step 3: Test the Dynamic Pricing
After adding the code:
- Clear your WooCommerce cache and your browser cache.
- Add products to your cart and increase the quantity. Observe the prices as you add more items.
- Ensure the prices adjust correctly when the threshold is met.
Customising Further
- Product-Specific Discounts: Adapt the code to apply discounts only to specific product categories or IDs.
- Tiered Discounts: Implement multiple thresholds with varying discounts.
- Combination Discounts: Offer discounts when specific combinations of products are in the cart.
Conclusion
Implementing WooCommerce dynamic pricing based on cart contents is an effective way to encourage larger purchases and enhance customer satisfaction by offering immediate rewards for buying more. This tutorial on WooCommerce dynamic pricing has equipped you with the tools to adapt pricing strategies dynamically based on user behavior.
Always test any changes on a staging site before applying them to your live site to ensure they work correctly without impacting the user experience or transactional integrity.
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.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch