/** * Get the singleUse error * * @param \XLite\Model\Order $order Order model * * @return array */ protected function getSingleUseErrors(\XLite\Model\Order $order) { $result = array(); $usedCoupons = $order->getUsedCoupons(); if ($this->getSingleUse() && 0 < $usedCoupons->count()) { $found = false; foreach ($usedCoupons as $uc) { if ($uc->getCoupon() && $this->getCode() != $uc->getCoupon()->getCode()) { // Other coupon was found in the order $found = true; break; } } if ($found) { // Coupon can not be added to order together with other coupons $result[] = self::ERROR_SINGLE_USE; } } elseif (0 < $usedCoupons->count()) { $found = false; foreach ($usedCoupons as $uc) { if ($uc->getCoupon() && $uc->getCoupon()->getSingleUse()) { $found = true; break; } } if ($found) { // Coupon can not be added as order contains singleUse-coupon $result[] = self::ERROR_SINGLE_USE2; } } return $result; }
/** * Get used coupon by id * * @param \XLite\Model\Order $cart Cart * @param integer $id Used coupon id * * @return \XLite\Module\CDev\Coupons\Model\UsedCoupon */ protected function getUsedCoupon($cart, $id) { return array_reduce($cart->getUsedCoupons()->toArray(), function ($carry, $item) use($id) { return $carry ?: ($item->getId() === $id ? $item : null); }, null); }
/** * Process coupons * * @param \XLite\Model\Order $order Order * * @return void */ protected function processCoupons(\XLite\Model\Order $order) { $request = \XLite\Core\Request::getInstance(); // Remove coupon foreach ($order->getUsedCoupons() as $coupon) { $hash = md5($coupon->getCode()); if ($coupon->getCode() && !empty($request->removeCoupons[$hash])) { // Register order change static::setOrderChanges('Removed coupons:' . $coupon->getId(), $coupon->getCode()); // Remove used coupon from order $order->getUsedCoupons()->removeElement($coupon); \XLite\Core\Database::getEM()->remove($coupon); } } // Add coupon if (!empty($request->newCoupon) && is_array($request->newCoupon)) { foreach ($request->newCoupon as $code) { if ($code) { $coupon = \XLite\Core\Database::getRepo('XLite\\Module\\CDev\\Coupons\\Model\\Coupon')->findOneByCode($code); if ($coupon) { $usedCoupon = new \XLite\Module\CDev\Coupons\Model\UsedCoupon(); $usedCoupon->setOrder($order); $order->addUsedCoupons($usedCoupon); $usedCoupon->setCoupon($coupon); $coupon->addUsedCoupons($usedCoupon); \XLite\Core\Database::getEM()->persist($usedCoupon); // Register order change static::setOrderChanges('Added coupons:' . $coupon->getId(), $coupon->getCode()); } } } } }
/** * Check coupon usages * * @param \XLite\Model\Order $order Order * * @throws \XLite\Module\CDev\Coupons\Core\CompatibilityException * * @return void */ protected function checkConflictsWithCoupons(\XLite\Model\Order $order) { if (!$order->containsCoupon($this)) { if ($this->getSingleUse() && $order->getUsedCoupons()->count()) { $this->throwCompatibilityException(static::ERROR_SINGLE_USE, 'This coupon cannot be combined with other coupons'); } if (!$this->getSingleUse() && $order->hasSingleUseCoupon()) { $this->throwCompatibilityException(static::ERROR_SINGLE_USE2, 'Sorry, this coupon cannot be combined with the coupon already applied. Revome the previously applied coupon and try again.'); } } }
/** * {@inheritDoc} */ public function getUsedCoupons() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getUsedCoupons', array()); return parent::getUsedCoupons(); }