We have a deal coming up for all of November at my work. Buy one, get one half off. There’s no way to do this in vanilla WooCommerce. I looked at Dynamic Pricing and it didn’t seem to offer this ability and if it did it was crazy complicated. I looked at some other plugins out there and found one that may work but it was pro version only and I didn’t want to pay to find out.
I figured this is pretty straight forward need so why not write up a function real fast. Here’s my train of thought followed by a code example. This is only a proof of concept and I still need to add – price limits (items must be over a certain price for this to work), category limits and the ability to apply this more than once to an order. But as of now it will take the second highest priced item and give it a 50% discount. If you have two of the same item as the highest price it will discount the amount to make it the same as taking 50% off just one of the items.
First we need to hook into woocommerce_before_calculate_totals. This way we can change the price on the cart and on the checkout.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
Then I set variables for highest price, quantity of that item, a counter so we know if the shopper has more than 1 item in our discount range (for future use with pricing bottoms) and next highest price item.
$highest_price_id = 0;
$highest_price_quantity = 0;
$cnt = 0;
$highest_price = 0;
$next_highest_price_id = 0;
$next_highest_price = 0;
I need to optimize this, there should be no need for looping the cart twice but in a hurry to prove the concept works this morning and get something ready for our promo I did it this way. I loop through the cart and gather all my data: highest priced item, quantity and so on. Then we loop the contents again and reduce price where needed.
foreach ( $cart_object->cart_contents as $key => $value ) {
if( $value['data']->price > $highest_price ) {
$highest_price = $value['data']->price;
$highest_price_id = $value['product_id'];
$highest_price_quantity = $value['quantity'];
$cnt++;
}
if( $value['data']->price < $highest_price && $value['data']->price > $next_highest_price ) {
$next_highest_price = $value['data']->price;
$next_highest_price_id = $value['product_id'];
$cnt++;
}
}
The final loop we first check if:
- Highest priced item is more than 1 quantity and we are on it in the loop
- There’s no other item that’s considered the second highest price. If there is, we want to discount that and not one of the highest priced items.
- If any of that fails then we check if this is the next highest priced item and run our discount here
foreach ( $cart_object->cart_contents as $key => $value ) {
if ( $highest_price_quantity > 1 && $value['product_id'] === $highest_price_id && $next_highest_price === 0 ) {
$value['data']->price = $value['data']->price - ( $value['data']->price / $value['quantity'] ) * .5;
} elseif ( $value['product_id'] === $next_highest_price_id && $cnt > 1 ) {
$value['data']->price = $value['data']->price - ( $value['data']->price / $value['quantity'] ) * .5;
}
}
The discount is based on quantity. If someone has two of the same $99 item we don’t want to make them both $48. We want to discount just the first item but I’m not sure if that’s even doable. We could generate a coupon or some other more complicated options but I went with the simple approach. Let’s take the quantity, divide the price by that (so we have two $99 items, 99/2 is $48) then take 50% of that and subtract it from the price. Each item will have 25% subtracted basically and add up to 50% total discount on 2 items.
I’ve been testing this and it seems to be working. I JUST put this together though so needs a lot more testing to make sure it’s working correctly.
https://gist.github.com/ChrisFlannagan/0d2010c49f582d5d89fa6f9740bf5703