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

@@ -28,6 +28,7 @@ use Thelia\Condition\ConditionFactory;
use Thelia\Condition\Implementation\ConditionInterface;
use Thelia\Core\Event\Coupon\CouponConsumeEvent;
use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Coupon\CouponFactory;
@@ -122,13 +123,6 @@ class Coupon extends BaseAction implements EventSubscriberInterface
$request->getSession()->setConsumedCoupons($consumedCoupons);
$totalDiscount = $couponManager->getDiscount();
// @todo insert false product in cart with the name of the coupon and the discount as negative price
// Decrement coupon quantity
// @todo move this part in after order event
$couponQuery = CouponQuery::create();
$couponModel = $couponQuery->findOneByCode($coupon->getCode());
$couponManager->decrementQuantity($couponModel);
$request
->getSession()
@@ -206,6 +200,47 @@ class Coupon extends BaseAction implements EventSubscriberInterface
$event->setCouponModel($coupon);
}
/**
* @param \Thelia\Core\Event\Order\OrderEvent $event
*/
public function testFreePostage(OrderEvent $event)
{
/** @var CouponManager $couponManager */
$couponManager = $this->container->get('thelia.coupon.manager');
if($couponManager->isCouponRemovingPostage()) {
$order = $event->getOrder();
$order->setPostage(0);
$event->setOrder($order);
$event->stopPropagation();
}
}
/**
* @param \Thelia\Core\Event\Order\OrderEvent $event
*/
public function decreaseCouponQuantity(OrderEvent $event)
{
$request = $this->container->get('request');
/** @var CouponManager $couponManager */
$couponManager = $this->container->get('thelia.coupon.manager');
$consumedCoupons = $request->getSession()->getConsumedCoupons();
if (is_array($consumedCoupons)) {
foreach($consumedCoupons as $couponCode) {
// Decrement coupon quantity
$couponQuery = CouponQuery::create();
$couponModel = $couponQuery->findOneByCode($couponCode);
$couponManager->decrementQuantity($couponModel);
}
}
}
/**
* Returns an array of event names this subscriber listens to.
*
@@ -232,7 +267,9 @@ class Coupon extends BaseAction implements EventSubscriberInterface
TheliaEvents::COUPON_CREATE => array("create", 128),
TheliaEvents::COUPON_UPDATE => array("update", 128),
TheliaEvents::COUPON_CONSUME => array("consume", 128),
TheliaEvents::COUPON_CONDITION_UPDATE => array("updateCondition", 128)
TheliaEvents::COUPON_CONDITION_UPDATE => array("updateCondition", 128),
TheliaEvents::ORDER_SET_POSTAGE => array("testFreePostage", 256),
TheliaEvents::ORDER_BEFORE_PAYMENT => array("decreaseCouponQuantity", 128),
);
}
}

View File

@@ -72,7 +72,18 @@ class Order extends BaseAction implements EventSubscriberInterface
{
$order = $event->getOrder();
$order->setDeliveryModuleId($event->getDeliveryModule());
$order->setPostage($event->getPostage());
$event->setOrder($order);
}
/**
* @param \Thelia\Core\Event\Order\OrderEvent $event
*/
public function setPostage(OrderEvent $event)
{
$order = $event->getOrder();
$order->setPostage($event->getPostage());
$event->setOrder($order);
@@ -178,6 +189,8 @@ class Order extends BaseAction implements EventSubscriberInterface
OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_NOT_PAID)->getId()
);
/* refresh discount @todo */
/* memorize discount */
$placedOrder->setDiscount(
$cart->getDiscount()
@@ -267,10 +280,7 @@ class Order extends BaseAction implements EventSubscriberInterface
}
}
/* discount @todo */
/* refresh discount */
/* memorize coupons */
/* memorize coupons @todo */
$con->commit();
@@ -420,6 +430,7 @@ class Order extends BaseAction implements EventSubscriberInterface
return array(
TheliaEvents::ORDER_SET_DELIVERY_ADDRESS => array("setDeliveryAddress", 128),
TheliaEvents::ORDER_SET_DELIVERY_MODULE => array("setDeliveryModule", 128),
TheliaEvents::ORDER_SET_POSTAGE => array("setPostage", 128),
TheliaEvents::ORDER_SET_INVOICE_ADDRESS => array("setInvoiceAddress", 128),
TheliaEvents::ORDER_SET_PAYMENT_MODULE => array("setPaymentModule", 128),
TheliaEvents::ORDER_PAY => array("create", 128),

View File

@@ -370,6 +370,7 @@ final class TheliaEvents
*/
const ORDER_SET_DELIVERY_ADDRESS = "action.order.setDeliveryAddress";
const ORDER_SET_DELIVERY_MODULE = "action.order.setDeliveryModule";
const ORDER_SET_POSTAGE = "action.order.setPostage";
const ORDER_SET_INVOICE_ADDRESS = "action.order.setInvoiceAddress";
const ORDER_SET_PAYMENT_MODULE = "action.order.setPaymentModule";
const ORDER_PAY = "action.order.pay";

View File

@@ -82,15 +82,8 @@ class CouponManager
if (count($this->coupons) > 0) {
$couponsKept = $this->sortCoupons($this->coupons);
$isRemovingPostage = $this->isCouponRemovingPostage($couponsKept);
$discount = $this->getEffect($couponsKept);
if ($isRemovingPostage) {
$postage = $this->facade->getCheckoutPostagePrice();
$discount += $postage;
}
// Just In Case test
$checkoutTotalPrice = $this->facade->getCartTotalPrice();
if ($discount >= $checkoutTotalPrice) {
@@ -103,23 +96,24 @@ class CouponManager
/**
* Check if there is a Coupon removing Postage
*
* @param array $couponsKept Array of CouponInterface sorted
*
* @return bool
*/
protected function isCouponRemovingPostage(array $couponsKept)
public function isCouponRemovingPostage()
{
$isRemovingPostage = false;
if (count($this->coupons) == 0) {
return false;
}
$couponsKept = $this->sortCoupons($this->coupons);
/** @var CouponInterface $coupon */
foreach ($couponsKept as $coupon) {
if ($coupon->isRemovingPostage()) {
$isRemovingPostage = true;
return true;
}
}
return $isRemovingPostage;
return false;
}
/**