/** * Apply coupon to the cart * * @return void */ protected function doActionAdd() { $code = strval(\XLite\Core\Request::getInstance()->code); /** @var \XLite\Module\CDev\Coupons\Model\Coupon $coupon */ $coupon = \XLite\Core\Database::getRepo('XLite\\Module\\CDev\\Coupons\\Model\\Coupon')->findOneByCode($code); $codes = $coupon ? $coupon->getErrorCodes($this->getCart()) : array(); $error = null; if (!$coupon || $codes) { $this->valid = false; if ($coupon && in_array(\XLite\Module\CDev\Coupons\Model\Coupon::ERROR_SINGLE_USE, $codes)) { $error = static::t('This coupon cannot be combined with other coupons'); } elseif ($coupon && in_array(\XLite\Module\CDev\Coupons\Model\Coupon::ERROR_SINGLE_USE2, $codes)) { $error = static::t('Sorry, this coupon cannot be combined with the coupon already applied. Revome the previously applied coupon and try again.'); } elseif ($coupon && in_array(\XLite\Module\CDev\Coupons\Model\Coupon::ERROR_TOTAL, $codes)) { $currency = $this->getCart()->getCurrency(); if (0 < $coupon->getTotalRangeBegin() && 0 < $coupon->getTotalRangeEnd()) { $error = static::t('To use the coupon, your order subtotal must be between X and Y', array('min' => $currency->formatValue($coupon->getTotalRangeBegin()), 'max' => $currency->formatValue($coupon->getTotalRangeEnd()))); } elseif (0 < $coupon->getTotalRangeBegin()) { $error = static::t('To use the coupon, your order subtotal must be at least X', array('min' => $currency->formatValue($coupon->getTotalRangeBegin()))); } else { $error = static::t('To use the coupon, your order subtotal must not exceed Y', array('max' => $currency->formatValue($coupon->getTotalRangeEnd()))); } } else { $error = static::t('There is no such a coupon, please check the spelling: X', array('code' => $code)); } } else { $found = false; foreach ($this->getCart()->getUsedCoupons() as $usedCoupon) { if ($usedCoupon->getCoupon() && $usedCoupon->getCoupon()->getId() == $coupon->getId()) { $found = true; break; } } if ($found) { // Duplicate $this->valid = false; $error = static::t('You have already used the coupon'); } else { // Create $usedCoupon = new \XLite\Module\CDev\Coupons\Model\UsedCoupon(); $usedCoupon->setOrder($this->getCart()); $this->getCart()->addUsedCoupons($usedCoupon); $usedCoupon->setCoupon($coupon); $coupon->addUsedCoupons($usedCoupon); \XLite\Core\Database::getEM()->persist($usedCoupon); $this->updateCart(); \XLite\Core\Database::getEM()->flush(); \XLite\Core\TopMessage::addInfo('The coupon has been applied to your order'); } } if ($error) { \XLite\Core\Event::invalidElement('code', $error); } $this->setPureAction(); }
/** * 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()); } } } } }
/** * Add coupon * * @param \XLite\Module\CDev\Coupons\Model\Coupon $coupon Coupon * * @throws \Doctrine\ORM\ORMInvalidArgumentException * * @return void */ public function addCoupon(\XLite\Module\CDev\Coupons\Model\Coupon $coupon) { $usedCoupon = new \XLite\Module\CDev\Coupons\Model\UsedCoupon(); $usedCoupon->setOrder($this); $this->addUsedCoupons($usedCoupon); $usedCoupon->setCoupon($coupon); $coupon->addUsedCoupons($usedCoupon); \XLite\Core\Database::getEM()->persist($usedCoupon); }