coupon management in order and cart process

This commit is contained in:
Etienne Roudeix
2013-12-19 14:17:22 +01:00
parent c981102d3a
commit d7c1ecf09a
8 changed files with 124 additions and 28 deletions

View File

@@ -24,11 +24,13 @@ namespace Front\Controller;
use Propel\Runtime\Exception\PropelException;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Event\Cart\CartEvent;
use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Form\CartAdd;
use Thelia\Model\AddressQuery;
class CartController extends BaseFrontController
{
@@ -53,6 +55,8 @@ class CartController extends BaseFrontController
$this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent);
$this->afterModifyCart();
$this->redirectSuccess();
} catch (PropelException $e) {
@@ -83,6 +87,8 @@ class CartController extends BaseFrontController
try {
$this->dispatch(TheliaEvents::CART_UPDATEITEM, $cartEvent);
$this->afterModifyCart();
$this->redirectSuccess();
} catch (PropelException $e) {
$this->getParserContext()->setGeneralError($e->getMessage());
@@ -98,6 +104,8 @@ class CartController extends BaseFrontController
try {
$this->getDispatcher()->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent);
$this->afterModifyCart();
$this->redirectSuccess();
} catch (PropelException $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage()));
@@ -142,4 +150,25 @@ class CartController extends BaseFrontController
return $cartAdd;
}
protected function afterModifyCart()
{
/* recalculate postage amount */
$order = $this->getSession()->getOrder();
if(null !== $order) {
$deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
$deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress);
if(null !== $deliveryModule && null !== $deliveryAddress) {
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
$orderEvent = new OrderEvent($order);
$orderEvent->setPostage($postage);
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
}
}
}
}

View File

@@ -25,11 +25,12 @@ namespace Front\Controller;
use Propel\Runtime\Exception\PropelException;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Coupon\CouponConsumeEvent;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Form\CouponCode;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Log\Tlog;
use Thelia\Model\Order;
use Thelia\Model\AddressQuery;
/**
* Class CouponController
@@ -65,6 +66,25 @@ class CouponController extends BaseFrontController
// 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->chosenDeliveryAddress);
if(null !== $deliveryModule && null !== $deliveryAddress) {
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
$orderEvent = new OrderEvent($order);
$orderEvent->setPostage($postage);
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
}
}
$this->redirect($couponCodeForm->getSuccessUrl());
} catch (FormValidationException $e) {
$message = sprintf('Please check your coupon code: %s', $e->getMessage());
} catch (PropelException $e) {

View File

@@ -95,6 +95,7 @@ class OrderController extends BaseFrontController
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_ADDRESS, $orderEvent);
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
$this->redirectToRoute("order.invoice");