WordPress / 8 MIN READ
Woo Tier Pricing
Creating WooCommerce Product Buy Buttons
From the original Fervor library. Examples may use older package versions.
Tutorial: Setting Up Quantity-Based Pricing Tiers in WooCommerce
WooCommerce doesn’t have a built-in feature for quantity-based pricing tiers out of the box, but with the help of plugins or some custom code, we can set up a dynamic pricing system. Here’s how to create three different price tiers that automatically adjust based on the quantity of products in the cart:
📋 What You’ll Need
- WooCommerce installed on your WordPress site.
- A plugin like WooCommerce Dynamic Pricing (recommended for ease) or some basic knowledge of adding custom PHP code (if you’re comfortable with a hands-on approach).
🛠️ Method 1: Using a Plugin (Recommended for Non-Coders)
Step 1: Install the “WooCommerce Dynamic Pricing” Plugin
- Go to your WordPress dashboard.
- Navigate to Plugins > Add New.
- Search for “WooCommerce Dynamic Pricing” (or a similar plugin, such as Discount Rules for WooCommerce).
- Install and activate the plugin.
Step 2: Set Up Pricing Rules
- Go to WooCommerce > Dynamic Pricing (or the equivalent settings section for your plugin).
- Add a new pricing rule for the product(s) you want to apply the tiers to.
Step 3: Define the Pricing Tiers
- For 1–5 pcs: Set the normal price (e.g., $10 per piece).
- For 6–10 pcs: Set the discounted price (e.g., $9 per piece).
- For >10 pcs: Set the larger discounted price (e.g., $8 per piece).
Here’s an example setup for rules:
- Rule Type: Quantity-based discounts.
- Conditions:
- Quantity between 1 and 5 → Price = $10
- Quantity between 6 and 10 → Price = $9
- Quantity greater than 10 → Price = $8
Step 4: Test the Pricing
- Go to your product page.
- Add different quantities to the cart and verify that the price changes dynamically based on your tiers.
🛠️ Method 2: Adding Custom Code (For Coders)
If you’re comfortable adding custom PHP code to your site, here’s how to do it:
Step 1: Add a Custom Function
- Go to your WordPress dashboard.
- Navigate to Appearance > Theme Editor or use a plugin like Code Snippets (recommended to avoid directly editing theme files).
- Add the following code snippet to your
functions.phpfile:
add_action('woocommerce_before_calculate_totals', 'custom_dynamic_pricing', 10, 1);
function custom_dynamic_pricing($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item) {
$quantity = $cart_item['quantity'];
$product = $cart_item['data'];
// Set price tiers
if ($quantity >= 1 && $quantity <= 5) {
$product->set_price(10); // Price for 1-5 pcs
} elseif ($quantity >= 6 && $quantity <= 10) {
$product->set_price(9); // Price for 6-10 pcs
} elseif ($quantity > 10) {
$product->set_price(8); // Price for >10 pcs
}
}
}
Step 2: Save and Test
- Save the changes.
- Add a product to the cart and adjust the quantity to see the pricing change.
⚙️ Important Notes
- Plugin Method: This is the easiest, most user-friendly way, especially if you’re not a developer.
- Custom Code Method: This provides more control but requires you to manage updates and potential conflicts.
- Bulk Discounts & Coupons: Plugins often let you combine bulk pricing with other discount features for extra flexibility.
🎉 You’re All Set!
Now you’ve got a WooCommerce store that dynamically adjusts product prices based on quantity tiers. Whether you used a plugin or custom code, your customers will enjoy automatic discounts, and you’ll enjoy seamless pricing management.
Bonus MY BUILD OUT
Here’s how you can modify the prices based on these attributes for specific quantity tiers using a custom PHP solution.
🛠️ Using Product Attributes for Pricing Tiers
We’ll use the product attributes (CURR Domestic Base Price Qty A, B, and C) to dynamically adjust prices in the cart based on quantity tiers.
Step 1: Add Attributes to Your Products
- Make sure the attributes (
CURR Domestic Base Price Qty A,B, andC) are properly imported and assigned to your products. - Each attribute should represent a price for a specific quantity tier:
- Qty A: Price for 1–5 pcs.
- Qty B: Price for 6–10 pcs.
- Qty C: Price for >10 pcs.
Step 2: Add Custom Code for Dynamic Pricing
We’ll use the attributes in the cart calculation. Add this code to your functions.php file or a custom code snippet plugin:
add_action('woocommerce_before_calculate_totals', 'dynamic_pricing_with_attributes', 10, 1);
function dynamic_pricing_with_attributes($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item) {
$product = $cart_item['data']; // Get product data
$quantity = $cart_item['quantity']; // Get cart quantity
// Fetch product attributes
$price_qty_a = get_post_meta($product->get_id(), 'CURR Domestic Base Price Qty A', true);
$price_qty_b = get_post_meta($product->get_id(), 'CURR Domestic Base Price Qty B', true);
$price_qty_c = get_post_meta($product->get_id(), 'CURR Domestic Base Price Qty C', true);
// Set pricing logic
if ($quantity >= 1 && $quantity <= 5 && $price_qty_a) {
$product->set_price($price_qty_a);
} elseif ($quantity >= 6 && $quantity <= 10 && $price_qty_b) {
$product->set_price($price_qty_b);
} elseif ($quantity > 10 && $price_qty_c) {
$product->set_price($price_qty_c);
}
}
}
Step 3: Test the Pricing Logic
- Ensure the product attributes (
CURR Domestic Base Price Qty A,B,C) are set with valid prices in the product’s Custom Fields. - Add a product to the cart and adjust the quantity:
- 1–5 pcs: Should use
Qty Aprice. - 6–10 pcs: Should use
Qty Bprice. - >10 pcs: Should use
Qty Cprice.
- 1–5 pcs: Should use
📋 Explanation of the Code
get_post_meta: Retrieves the custom field values for the product, corresponding to your attributes.$product->set_price(): Dynamically updates the price in the cart based on the quantity tier.woocommerce_before_calculate_totals: Ensures that the cart totals are recalculated with the updated prices.
Step 4: Debugging & Optimization
- If prices aren’t updating, ensure that the attributes are correctly imported and saved as custom fields.
- Use
error_log()to debug by printing the values of$price_qty_a,$price_qty_b, and$price_qty_c.
🎉 Done!
Your WooCommerce store now uses product attributes to dynamically set prices based on quantity tiers. This solution keeps your pricing flexible and tied to attributes for easier updates and management.
Bonus More Solutions
Instead of showing a list of tiered pricing, we can dynamically calculate and display how much the customer is saving by purchasing a higher quantity. The savings will be shown in red for better visibility.
Here’s how to implement it:
🛠️ Code to Display Savings in the Cart Page
Add the following code to your functions.php file or a custom code snippet plugin. This will calculate the savings based on the current quantity and display the amount saved in red under the item price.
add_action('woocommerce_cart_item_price', 'show_savings_for_higher_quantities', 10, 3);
function show_savings_for_higher_quantities($price_html, $cart_item, $cart_item_key) {
$product = $cart_item['data'];
$quantity = $cart_item['quantity'];
// Fetch product attributes for pricing tiers
$price_qty_a = get_post_meta($product->get_id(), 'CURR Domestic Base Price Qty A', true); // Base price (1-5 pcs)
$price_qty_b = get_post_meta($product->get_id(), 'CURR Domestic Base Price Qty B', true); // Discounted price (6-10 pcs)
$price_qty_c = get_post_meta($product->get_id(), 'CURR Domestic Base Price Qty C', true); // Discounted price (>10 pcs)
// Determine the current price tier and calculate potential savings
$savings_message = '';
if ($quantity >= 6 && $quantity <= 10) {
$savings_per_item = $price_qty_a - $price_qty_b; // Savings per item for tier B
$total_savings = $savings_per_item * $quantity;
$savings_message = '<p style="color: red; font-weight: bold;">You saved ' . wc_price($total_savings) . '!</p>';
} elseif ($quantity > 10) {
$savings_per_item = $price_qty_a - $price_qty_c; // Savings per item for tier C
$total_savings = $savings_per_item * $quantity;
$savings_message = '<p style="color: red; font-weight: bold;">You saved ' . wc_price($total_savings) . '!</p>';
}
// Return the original price HTML with the savings message appended
return $price_html . $savings_message;
}
🎯 How It Works:
-
Savings Calculation:
- For quantities between 6–10, the savings are calculated as
(Price Qty A - Price Qty B) × Quantity. - For quantities greater than 10, the savings are
(Price Qty A - Price Qty C) × Quantity.
- For quantities between 6–10, the savings are calculated as
-
Savings Display:
- If there are no savings (e.g., for quantities 1–5), nothing additional is shown.
- For 6 or more items, the total savings are displayed in bold red text under the product price.
🧪 Testing the Savings Display
- Add products to the cart and adjust quantities:
- For 1–5 pcs: No savings message should appear.
- For 6–10 pcs: The savings message should display based on the difference between
Qty AandQty B. - For >10 pcs: The savings message should display based on the difference between
Qty AandQty C.
💡 Optional: Improve Savings Presentation
To make the savings display stand out even more, you can add a custom icon or some CSS styling. For example:
Custom Styling
Add this to your theme’s stylesheet or the Additional CSS section in the WordPress customizer:
.woocommerce-cart .cart_item p {
margin-top: 5px;
}
.woocommerce-cart .cart_item p[style*="color: red;"] {
font-size: 1em;
text-align: left;
background-color: #ffe6e6;
padding: 5px 10px;
border-radius: 5px;
}
This will:
- Add a subtle background highlight for the savings message.
- Make it visually distinct without overwhelming the cart page.
Now, your customers will see exactly how much they’ve saved in the cart, incentivizing larger purchases without cluttering the page with unnecessary details. 😊