Reputation: 31
I am currently setting up a WooCommerce shop. Basically this is what I am trying to achieve
"If you place an order for a certain quantity of a product for example 40 pieces from product "A" and then you add another 90 pieces of product "B" then you have ordered a total of 130 pieces (quantity in cart is equal to 130)
So now my prices are based on how many pieces you ordered in total not per piece.
So if you order any quantities between 80 & 150 then all you have to pay is a flat price of $50 (so if you order 91 or 101 or 150 your final cost will be $50).
So this is just an example as I would like to setup other pricing brackets.
Any idea how can that be achieved, I tried multiple plugins recommended by WooCommerce but non of them do what I am looking for.
Your help is greatly appreciated...
Upvotes: 0
Views: 4566
Reputation: 3899
At it's simplest, this code will do what you're asking:
function custom_price_function( $total, $cart ) {
if( $cart->cart_contents_count <= 150 && $cart->cart_contents_count >= 80 )
$total = 50;
return $total;
}
add_filter( 'woocommerce_calculated_total', 'custom_price_function', 10, 2 );
You add it to your theme functions.php
file or custom plugin code. It hooks into a filter in the WooCommerce file class-wc-cart.php
in the carts class calculate_totals()
function. This filter is at line 1361 at time of writing.
If you take a look at the function you'll see other properties that may be useful for calculating the total price the way you want it. For example taxes are contained in the $this->tax_total
variable accessed in the custom_price_function()
with $cart->tax_total
.
Upvotes: 2