WooCommerce: Bulk Dynamic Pricing (Without Plugins) – 2024

Implementing bulk dynamic pricing in WooCommerce can significantly enhance the shopping experience by encouraging larger purchases through volume discounts. While many choose plugins for this functionality, a custom-built solution without plugins offers flexibility and greater control. This guide explains how to implement WooCommerce: Bulk Dynamic Pricing (Without Plugins), including how to add custom fields for dynamic settings and display these on product pages.

Adding Custom Fields for Bulk Pricing

First, we’ll add custom fields in the WooCommerce product dashboard where store managers can define bulk pricing rules.

/*
 * Snippet: WooCommerce: Bulk Dynamic Pricing (Without Plugins) – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?1102
* Tested with WooCommerce 8.8.3
* "Add custom field to product tab"
*/ function wcsuccess_add_bulk_pricing_fields() { woocommerce_wp_text_input( array( 'id' => 'bulk_pricing', 'label' => 'Bulk Pricing (Qty:Discount)', 'placeholder' => 'e.g. 100:5,500:10', 'desc_tip' => 'true', 'description' => 'Enter pairs of quantities and discounts separated by colons and commas.', 'type' => 'text', )); } add_action('woocommerce_product_options_general_product_data', 'wcsuccess_add_bulk_pricing_fields'); function wcsuccess_save_bulk_pricing_fields($post_id) { $woocommerce_bulk_pricing = $_POST['bulk_pricing']; if (!empty($woocommerce_bulk_pricing)) update_post_meta($post_id, 'bulk_pricing', esc_attr($woocommerce_bulk_pricing)); } add_action('woocommerce_process_product_meta', 'wcsuccess_save_bulk_pricing_fields');

Modifying Cart Pricing Based on Custom Fields

Next, we adjust the cart pricing dynamically based on the defined bulk pricing rules.

/*
 * Snippet: WooCommerce: Bulk Dynamic Pricing (Without Plugins) – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?1102
* Tested with WooCommerce 8.8.3
* "Modify pricing based on custom fields"
*/ add_action('woocommerce_before_calculate_totals', 'wcsuccess_quantity_based_pricing', 9999); function wcsuccess_quantity_based_pricing($cart) { if (is_admin() && !defined('DOING_AJAX')) return; if (did_action('woocommerce_before_calculate_totals') >= 2) return; foreach ($cart->get_cart() as $cart_item_key => $cart_item) { $bulk_pricing = get_post_meta($cart_item['product_id'], 'bulk_pricing', true); if (!$bulk_pricing) continue; $pricing_rules = explode(',', $bulk_pricing); foreach ($pricing_rules as $rule) { list($threshold, $discount) = explode(':', $rule); if ($cart_item['quantity'] >= $threshold) { $price = round($cart_item['data']->get_price() * (1 - ($discount / 100)), 2); $cart_item['data']->set_price($price); } } } }

Displaying Bulk Pricing on Product Pages

To help customers understand the available bulk discounts, we’ll display them on the product page in a clear table format.

/*
 * Snippet: WooCommerce: Bulk Dynamic Pricing (Without Plugins) – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?1102
* Tested with WooCommerce 8.8.3
* "Display pricing on product page"
*/ function wcsuccess_display_bulk_pricing_table() { global $product; $bulk_pricing = get_post_meta($product->get_id(), 'bulk_pricing', true); if (!$bulk_pricing) return; $pricing_rules = explode(',', $bulk_pricing); echo '<table class="bulk-pricing"><thead><tr><th>Quantity</th><th>Discount (%)</th></tr></thead><tbody>'; foreach ($pricing_rules as $rule) { list($threshold, $discount) = explode(':', $rule); echo '<tr><td>' . esc_html($threshold) . '+</td><td>' . esc_html($discount) . '%</td></tr>'; } echo '</tbody></table>'; } add_action('woocommerce_single_product_summary', 'wcsuccess_display_bulk_pricing_table', 20);

Conclusion

Implementing WooCommerce: Bulk Dynamic Pricing (Without Plugins) not only provides a customizable approach to pricing but also enhances transparency, allowing customers to understand how much they can save by purchasing in bulk. This method fosters better customer engagement and potentially boosts sales by incentivizing larger orders.

0 0 votes
Article Rating

Stay In Touch

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

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top
×