Created
May 26, 2023 04:42
-
-
Save AshlinRejo/6d95675a4dfd8fce6f912da72bb1335a to your computer and use it in GitHub Desktop.
Discount rules v2: Handle set discount on having low price for few item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* To handle the low price product in set discount. | |
* Works from Discount rules v2.6.1 | |
*/ | |
add_filter('advanced_woo_discount_rules_matched_set_discount_range', function ($selected_bundle_range, $product, $rule, $cart_items, $is_cart){ | |
if(class_exists('\Wdr\App\Helpers\Woocommerce')){ | |
if($is_cart){ | |
if(!empty($selected_bundle_range->type) && $selected_bundle_range->type == 'fixed_set_price'){ | |
if(!empty($cart_items)){ | |
$discounted_price = $selected_bundle_range->value; | |
$availableQty = $selected_bundle_range->from; | |
$splitted_discount = $discounted_price/$availableQty; | |
$adjust_discount = false; | |
$item_with_low_price = array(); | |
$subtotal = $qty_to_apply = 0; | |
$discount_to_adjust = 0; | |
foreach ($cart_items as $cart_item){ | |
if($rule->isFilterPassed($cart_item['data'])){ | |
$product_price = $cart_item['data']->get_price(); | |
$qty = isset($cart_item['quantity']) ? $cart_item['quantity'] : 0; | |
if($availableQty >= $qty){ | |
$qty_to_apply = $qty; | |
$subtotal += $product_price*$qty; | |
$availableQty -= $qty_to_apply; | |
} else { | |
$qty_to_apply = $qty-$availableQty; | |
$subtotal += $product_price*($qty_to_apply); | |
$availableQty -= $qty_to_apply; | |
} | |
$cart_key = $cart_item['key']; | |
$info = array(); | |
$info['adjustable_qty'] = $qty_to_apply; | |
if($splitted_discount > $product_price){ | |
$adjust_discount = true; | |
$info['fixed_price'] = $product_price; | |
$discount_to_adjust += $info['negative_price'] = ($splitted_discount-$product_price)*$qty_to_apply; | |
$info['adjustable_price'] = 0; | |
} else { | |
$info['fixed_price'] = $splitted_discount; | |
$info['negative_price'] = 0; | |
$info['adjustable_price'] = $product_price - $splitted_discount; | |
} | |
$item_with_low_price[$cart_key] = $info; | |
if($availableQty <= 0){ | |
break; | |
} | |
} | |
} | |
if($adjust_discount === true && $subtotal >= $discounted_price){ | |
if($discount_to_adjust > 0){ | |
foreach ($item_with_low_price as $key => $item){ | |
$adjustable_price = $item['adjustable_price']*$qty_to_apply; | |
if($adjustable_price >= $discount_to_adjust){ | |
$item_with_low_price[$key]['fixed_price'] += $discount_to_adjust/$item['adjustable_qty']; | |
$discount_to_adjust = 0; | |
} else if($adjustable_price > 0){ | |
$discount_to_adjust -= $adjustable_price; | |
$item_with_low_price[$key]['fixed_price'] += $item['adjustable_price']; | |
} | |
} | |
} | |
$product_id = \Wdr\App\Helpers\Woocommerce::getProductId($product); | |
if(!empty($cart_items)){ | |
foreach ($cart_items as $cart_item){ | |
$key = $cart_item['key']; | |
$cart_item_product_id = \Wdr\App\Helpers\Woocommerce::getProductIdFromCartItem($cart_item); | |
if($cart_item_product_id == $product_id){ | |
if(isset($item_with_low_price[$key])){ | |
$selected_bundle_range->value = $item_with_low_price[$key]['fixed_price'] * $selected_bundle_range->from; | |
} | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
return $selected_bundle_range; | |
}, 10, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment