/** * Only token per cart setting * * @param OnlineShop_Framework_ICart $cart * * @throws Exception */ protected function checkOnlyToken(OnlineShop_Framework_ICart $cart) { $cartCodes = $cart->getVoucherTokenCodes(); $cartVoucherCount = sizeof($cartCodes); if ($cartVoucherCount && method_exists($this->configuration, 'getOnlyTokenPerCart')) { if ($this->configuration->getOnlyTokenPerCart()) { throw new OnlineShop_Framework_Exception_VoucherServiceException("OnlyTokenPerCart: This token is only allowed as only token in this cart.", 6); } $cartToken = OnlineShop_Framework_VoucherService_Token::getByCode($cartCodes[0]); $cartTokenSettings = Object_OnlineShopVoucherSeries::getById($cartToken->getVoucherSeriesId())->getTokenSettings()->getItems()[0]; if ($cartTokenSettings->getOnlyTokenPerCart()) { throw new OnlineShop_Framework_Exception_VoucherServiceException("OnlyTokenPerCart: There is a token of type onlyToken in your this cart already.", 7); } } }
/** * @param string $code * @param OnlineShop_Framework_ICart $cart * @param OnlineShop_Framework_AbstractOrder $order * * @return bool|Object_OnlineShopVoucherToken */ public function applyToken($code, OnlineShop_Framework_ICart $cart, OnlineShop_Framework_AbstractOrder $order) { if ($token = OnlineShop_Framework_VoucherService_Token::getByCode($code)) { if ($token->check($this->configuration->getUsages(), true)) { if ($token->apply()) { $orderToken = Object_OnlineShopVoucherToken::getByToken($code, 1); if (!$orderToken instanceof Object_OnlineShopVoucherToken) { $orderToken = new Object_OnlineShopVoucherToken(); $orderToken->setTokenId($token->getId()); $orderToken->setToken($token->getToken()); $series = Object_OnlineShopVoucherSeries::getById($token->getVoucherSeriesId()); $orderToken->setVoucherSeries($series); $orderToken->setParent($series); // TODO set correct parent for applied tokens $orderToken->setKey(\Pimcore\File::getValidFilename($token->getToken())); $orderToken->setPublished(1); $orderToken->save(); } return $orderToken; } } } return false; }