public function applyRule(ISC_QUOTE $quote)
	{
		if (!customerIsSignedIn()) {
			return null;
		}

		$customerId = getClass('ISC_CUSTOMER')->getCustomerId();
		$query = "
			SELECT COUNT(*)
			FROM [|PREFIX|]orders
			WHERE ordcustid='".$customerId."' AND ordstatus > 0 AND deleted = 0
			LIMIT 1
		";
		$orderCount = $GLOBALS['ISC_CLASS_DB']->fetchOne($query);

		// Discount does not apply
		if($orderCount <= $this->orders) {
			return false;
		}

		$items = $quote->getItems();
		$totalDiscount = 0;
		// The discount needs to come off each item, so that tax is also affected.
		foreach($items as $item) {
			$discountAmount = $item->getDiscountedBaseTotal() * ($this->amount / 100);
			$item->addDiscount($this->getDbId(), $discountAmount);
			$totalDiscount += $discountAmount;
		}

		$quote->addDiscount($this->getDbId(), $totalDiscount);
		$this->banners[] = sprintf(getLang($this->getName().'DiscountMessage'), $this->amount);
		return true;
	}
	public function applyRule(ISC_QUOTE $quote)
	{
		if($quote->getBaseSubTotal() < $this->amount) {
			return false;
		}

		$runningTotal = $this->amount_off;
		$items = $quote->getItems();
		foreach ($items as $item) {
			$discountedBase = $item->getDiscountedBaseTotal();
			if($discountedBase - $runningTotal < 0) {
				$item->addDiscount($this->getDbId(), $discountedBase);
				$runningTotal -= $discountedBase;
			}
			else {
				$item->addDiscount($this->getDbId(), $runningTotal);
				$runningTotal -= $runningTotal;
			}

			if($runningTotal <= 0) {
				break;
			}
		}

		$quote->addDiscount($this->getDbId(), $this->amount_off);

		$amountOff = currencyConvertFormatPrice($this->amount_off);
		$amount = currencyConvertFormatPrice($this->amount);
		$this->banners[] = sprintf(getLang($this->getName().'DiscountMessage'), $amountOff, $amount);
		return true;
	}
	public function applyRule(ISC_QUOTE $quote)
	{
		$found = false;
		$items = $quote->getItems();
		$ruleCats = explode(',', $this->catids);

		// The discount needs to come off each item, so that tax is also affected.
		$totalDiscount = 0;
		foreach($items as $item) {
			$apply = false;
			if($item instanceof ISC_QUOTE_ITEM_GIFTCERTIFICATE) {
				continue;
			}

			$categoryIds = $item->getCategoryIds();
			foreach($ruleCats as $categoryId) {
				if(!in_array($categoryId, $categoryIds) && $categoryId != 0) {
					continue;
				}

				$apply = true;
				$found[] = $categoryId;
			}

			if(!$apply) {
				continue;
			}

			$discountAmount = $item->getBaseTotal() * ($this->amount / 100);
			$discountAmount = round($discountAmount, getConfig('DecimalPlaces'));
			if($item->getBaseTotal() - $discountAmount < 0) {
				$discountAmount = 0;
			}

			$item->addDiscount($this->getDbId(), $discountAmount);
			$totalDiscount += $discountAmount;
		}

		if (!empty($found)) {

			$quote->addDiscount($this->getDbId(), $totalDiscount);

			$catname = '';
			$catids = implode(',', $found);

			$query = "
				SELECT catname
				FROM [|PREFIX|]categories
				WHERE categoryid IN ($catids)
			";
			$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
			while($var = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
				$catname[] = $var['catname'];
			}
			if (isset($catname{1})) {
				$this->banners[] = sprintf(GetLang($this->getName().'DiscountMessagePlural'), $this->amount, implode(' and ',$catname));
			} else {
				$this->banners[] = sprintf(GetLang($this->getName().'DiscountMessage'), $this->amount, implode(' and ',$catname));

			}
			return true;
		}

		return false;
	}
	public function applyRule(ISC_QUOTE $quote)
	{
		$customerId = getClass('ISC_CUSTOMER')->getCustomerId();
		$query = "
			SELECT COUNT(*)
			FROM [|PREFIX|]orders
			WHERE ordcustid='".$customerId."' AND ordstatus > 0 AND deleted = 0
			LIMIT 1
		";
		$orderCount = $GLOBALS['ISC_CLASS_DB']->fetchOne($query);

		// Discount does not apply
		if($orderCount <= $this->orders) {
			return false;
		}

		$runningTotal = $this->amount;
		$appliedAmount = 0;
		$items = $quote->getItems();
		foreach ($items as $item) {
			$discountedBase = $item->getDiscountedBaseTotal();
			if($discountedBase - $runningTotal < 0) {
				$item->addDiscount($this->getDbId(), $discountedBase);
				$appliedAmount += $discountedBase;
				$runningTotal -= $discountedBase;
			}
			else {
				$item->addDiscount($this->getDbId(), $runningTotal);
				$appliedAmount += $runningTotal;
				$runningTotal -= $runningTotal;
			}

			if($runningTotal <= 0) {
				break;
			}
		}

		$quote->addDiscount($this->getDbId(), $appliedAmount);

		$amount = currencyConvertFormatPrice($appliedAmount);
		$this->banners[] = sprintf(getLang($this->getName().'DiscountMessage'), $amount);
		return true;
	}