WooCommerce Dynamic Pricing Based on Cart Contents – 2024

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.


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 and woocommerce_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.
See also  How to Add an Image Below Add to Cart on Woocommerce Product Page

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.


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.

See also  Limit Purchase Quantity in WooCommerce - 2024

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.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x
Scroll to Top