/** * Coupon consuming */ public function consumeAction() { $this->checkCartNotEmpty(); $message = false; $couponCodeForm = $this->createForm(FrontForm::COUPON_CONSUME); try { $form = $this->validateForm($couponCodeForm, 'post'); $couponCode = $form->get('coupon-code')->getData(); if (null === $couponCode || empty($couponCode)) { $message = true; throw new \Exception($this->getTranslator()->trans('Coupon code can\'t be empty', [], Front::MESSAGE_DOMAIN)); } $couponConsumeEvent = new CouponConsumeEvent($couponCode); // Dispatch Event to the Action $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent); /* recalculate postage amount */ $order = $this->getSession()->getOrder(); if (null !== $order) { $deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress()); if (null !== $deliveryModule && null !== $deliveryAddress) { $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container); $orderEvent = new OrderEvent($order); try { $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($deliveryAddress->getCountry())); $orderEvent->setPostage($postage->getAmount()); $orderEvent->setPostageTax($postage->getAmountTax()); $orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle()); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent); } catch (DeliveryException $ex) { // The postage has been chosen, but changes dues to coupon causes an exception. // Reset the postage data in the order $orderEvent->setDeliveryModule(0); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent); } } } return $this->generateSuccessRedirect($couponCodeForm); } catch (FormValidationException $e) { $message = $this->getTranslator()->trans('Please check your coupon code: %message', ["%message" => $e->getMessage()], Front::MESSAGE_DOMAIN); } catch (UnmatchableConditionException $e) { $message = $this->getTranslator()->trans('You should <a href="%sign">sign in</a> or <a href="%register">register</a> to use this coupon', ['%sign' => $this->retrieveUrlFromRouteId('customer.login.view'), '%register' => $this->retrieveUrlFromRouteId('customer.create.view')], Front::MESSAGE_DOMAIN); } catch (PropelException $e) { $this->getParserContext()->setGeneralError($e->getMessage()); } catch (\Exception $e) { $message = $this->getTranslator()->trans('Sorry, an error occurred: %message', ["%message" => $e->getMessage()], Front::MESSAGE_DOMAIN); } if ($message !== false) { Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())); $couponCodeForm->setErrorMessage($message); $this->getParserContext()->addForm($couponCodeForm)->setGeneralError($message); } return $this->generateErrorRedirect($couponCodeForm); }
/** * Coupon consuming */ public function consumeAction() { $this->checkAuth(); $this->checkCartNotEmpty(); $message = false; $couponCodeForm = new CouponCode($this->getRequest()); try { $form = $this->validateForm($couponCodeForm, 'post'); $couponCode = $form->get('coupon-code')->getData(); if (null === $couponCode || empty($couponCode)) { $message = true; throw new \Exception('Coupon code can\'t be empty'); } $couponConsumeEvent = new CouponConsumeEvent($couponCode); // Dispatch Event to the Action $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent); /* recalculate postage amount */ $order = $this->getSession()->getOrder(); if (null !== $order) { $deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress()); if (null !== $deliveryModule && null !== $deliveryAddress) { $moduleInstance = $deliveryModule->getModuleInstance($this->container); $orderEvent = new OrderEvent($order); try { $postage = $moduleInstance->getPostage($deliveryAddress->getCountry()); $orderEvent->setPostage($postage); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent); } catch (DeliveryException $ex) { // The postage has been chosen, but changes dues to coupon causes an exception. // Reset the postage data in the order $orderEvent->setDeliveryModule(0); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent); } } } return $this->generateSuccessRedirect($couponCodeForm); } catch (FormValidationException $e) { $message = sprintf('Please check your coupon code: %s', $e->getMessage()); } catch (PropelException $e) { $this->getParserContext()->setGeneralError($e->getMessage()); } catch (\Exception $e) { $message = sprintf('Sorry, an error occurred: %s', $e->getMessage()); } if ($message !== false) { Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())); $couponCodeForm->setErrorMessage($message); $this->getParserContext()->addForm($couponCodeForm)->setGeneralError($message); } }
protected function afterModifyCart() { /* recalculate postage amount */ $order = $this->getSession()->getOrder(); if (null !== $order) { $deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress()); if (null !== $deliveryModule && null !== $deliveryAddress) { $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container); $orderEvent = new OrderEvent($order); try { $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($deliveryAddress->getCountry())); $orderEvent->setPostage($postage->getAmount()); $orderEvent->setPostageTax($postage->getAmountTax()); $orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle()); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent); } catch (DeliveryException $ex) { // The postage has been chosen, but changes in the cart causes an exception. // Reset the postage data in the order $orderEvent->setDeliveryModule(0); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent); } } } }
public function setModuleDelivery(OrderEvent $event) { if ($event->getDeliveryModule() == TNTFrance::getModuleId()) { $request = $this->getRequest(); $id = $request->getSession()->getSessionCart()->getId(); $address = TNTFrance::getCartDeliveryAddress($this->getRequest()); $data = TNTFrance::getExtraOrderData($id); try { $this->saveExtraInformation($data, $event->getOrder(), $address); //We must recalcul postage when this informations are saved :( if (array_key_exists('tnt_serviceCode', $data)) { $cartEvent = new CartEvent($this->getRequest()->getSession()->getSessionCart($event->getDispatcher())); $event->getDispatcher()->dispatch(OrderAction::TNT_CALCUL_CART_WEIGHT, $cartEvent); $postage = TNTFrance::calculPriceForService($data['tnt_serviceCode'], $cartEvent->getCart()->getVirtualColumn('total_package'), $cartEvent->getCart()->getVirtualColumn('total_weight')); $event->getOrder()->setPostage($postage); $event->setPostage($postage); } } catch (\Exception $ex) { throw $ex; } TNTFrance::setExtraOrderData($id, $data); } }