I have created a 'BOGOF' (buy one get one free) coupon, using the normal woocommerce coupon method.
The coupon gives the user 100% percentage discount on 1 other item in the cart.
Coupon settings
General:
Discount type: Percentage discount Coupon
amount: 100
Usage limits:
- Limit usage to X items: 1
When used:
- Coupon applies 100% to a random item in the cart (default behavior, I guess)
Desired:
- It needs to take 100% off the cheapest item in the cart.
With the following code I try to achieve my goal, unfortunately without the desired result
function filter_woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $instance ) {
$price_array = array();
foreach( $cart_item as $item ) {
echo $item->price;
if($item->price > 0){
array_push($price_array, $item->price);
}
}
$lowestPrice = min($price_array);
if( $lowestPrice < $discount ){
$discount = $lowestPrice;
}
return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );