If you ever worked with clients you know that a feature request can sometimes be very challenging, not by default supported by WooCommerce, or almost impossible to implement with existing plugins. So when there is no out-of-the-box solution, we turn to custom WordPress/WooCommerce/PHP and functions.php editing.
The request that came to me was to change the WooCommerce price discount based on the quantity added to the cart. The prices for some of the products should be 10% lower if the customer wants to buy the whole package. Package size depends and it’s different for every product.
Register custom input in product admin
For this part of the task, you should use
woocommerce_product_options_inventory_product_data hook for displaying the input field and
woocommerce_process_product_meta hook to process the saved field.
The code is following:
This code will register new field in Product admin page inside Inventory tab:
Add a discount notice to the Product page
The next step is to let potential customers that they can get a discount if they buy a whole package of products. You can use any of the hooks on the product page (find a hook for your use-case on the next link), for my case I used
woocommerce_before_add_to_cart_button position just above the “Add to cart” button.
Your product (with package quantity set) should look like this:
Don’t forget to add custom CSS (will cover this in some other post) to style your notice.
Calculate WooCommerce price discount on checkout
In this step, we will implement a negative tax that will reduce checkout prices by 10% for products that fulfill the conditions. Hook
woocommerce_cart_calculate_fees will do the work and the code is following:
Note that I added method
exclude_cart_fees_taxes that fixes the tax system for my use case. Use it depending on your use case.
Conclusion on WooCommerce price discount
That’s it, pretty simple, straightforward process of adding discounts (negative taxes to be technically correct) for bulk purchases. Have in mind that this code can be customized to your project’s needs so feel free to play with it!
Find more posts about WooCommerce on the following link.