Skip to content

Instantly share code, notes, and snippets.

@AshlinRejo
Last active April 14, 2023 10:10
Show Gist options
  • Save AshlinRejo/67d3de0f13ec13bf2973d75c002bb015 to your computer and use it in GitHub Desktop.
Save AshlinRejo/67d3de0f13ec13bf2973d75c002bb015 to your computer and use it in GitHub Desktop.
Discount rule v2: Check for any possible discount
if(!function_exists('isPossibleToHaveDiscountThroughWDR')) {
function isPossibleToHaveDiscountThroughWDR() {
$status = false;
if (class_exists('\Wdr\App\Controllers\DiscountCalculator')) {
$rules = \Wdr\App\Controllers\DiscountCalculator::$rules;
$cart = \Wdr\App\Helpers\Woocommerce::getCart();
foreach ($rules as $rule) {
if ($rule->isEnabled()) {
if ($rule->hasConditions()) {
$conditions_passed = $rule->isCartConditionsPassed($cart);
if ($conditions_passed) {
$status = true;
break;
}
} else {
$status = true;
break;
}
}
}
}
return $status;
}
}
// Example for how to check possibility to have a discount or not
add_action('woocommerce_before_cart', function (){
$has_discount = isPossibleToHaveDiscountThroughWDR();
if($has_discount){
echo "Has discount";
} else {
echo "No discount available";
}
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment