35 lines
812 B
PHP
35 lines
812 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerCoupon\Model\Coupon;
|
|
|
|
use Magento\Quote\Model\Quote;
|
|
use Magento\SalesRule\Model\Rule;
|
|
|
|
class EligibilityService
|
|
{
|
|
public function isApplicableToQuote(Rule $rule, Quote $quote): bool
|
|
{
|
|
if (!$quote->getItemsCount()) {
|
|
return false;
|
|
}
|
|
|
|
$address = $quote->getIsVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
|
|
if (!$rule->getConditions()->validate($address)) {
|
|
return false;
|
|
}
|
|
|
|
if (!$rule->getActions()->getConditions()) {
|
|
return true;
|
|
}
|
|
|
|
foreach ($quote->getAllVisibleItems() as $item) {
|
|
if ($rule->getActions()->validate($item)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|