/** * Checks whether a coupon can be applied or not. * * @param CouponInterface $coupon Coupon to work with * @param float $price Price * * @return boolean Coupon can be applied * * @throws CouponBelowMinimumPurchaseException * @throws CouponAppliedException * @throws CouponNotActiveException */ protected function checkCoupon(CouponInterface $coupon, $price) { /** * check if coupon is enabled and not deleted */ if (!$coupon->isEnabled()) { throw new CouponNotActiveException(); } $now = new DateTime(); /** * check if coupon is active */ if ($coupon->getValidFrom() > $now || $coupon->getValidTo() < $now) { throw new CouponNotActiveException(); } /** * check if coupon still can be applied */ if ($coupon->getCount() - $coupon->getUsed() < 1) { throw new CouponAppliedException(); } /** * you cannot add this coupon, too cheap */ if ($coupon->getMinimumPurchase()->getAmount() > $price) { throw new CouponBelowMinimumPurchaseException(); } return true; }
/** * Check if a coupon is currently active. * * @param CouponInterface $coupon Coupon to check activeness * @param DateTime $now * * @return bool */ private function isActive(CouponInterface $coupon, \DateTime $now = null) { if (!$coupon->isEnabled()) { return false; } $now = $now ?: $this->dateTimeFactory->create(); if ($coupon->getValidFrom() > $now) { return false; } $validTo = $coupon->getValidTo(); if ($validTo && $now > $validTo) { return false; } return true; }