public function orderStatusChange(OrderEvent $event, $eventName, EventDispatcherInterface $dispatcher)
    {
        // The order has been canceled or refunded ?
        if ($event->getOrder()->isCancelled() || $event->getOrder()->isRefunded()) {
            // Cancel usage of all coupons for this order
            $usedCoupons = OrderCouponQuery::create()
                ->filterByUsageCanceled(false)
                ->findByOrderId($event->getOrder()->getId());

            $customerId = $event->getOrder()->getCustomerId();

            /** @var OrderCoupon $usedCoupon */
            foreach ($usedCoupons as $usedCoupon) {
                if (null !== $couponModel = CouponQuery::create()->findOneByCode($usedCoupon->getCode())) {
                    // If the coupon still exists, restore one usage to the usage count.
                    $this->couponManager->incrementQuantity($couponModel, $customerId);
                }

                // Mark coupon usage as canceled in the OrderCoupon table
                $usedCoupon->setUsageCanceled(true)->save();
            }
        } else {
            // Mark canceled coupons for this order as used again
            $usedCoupons = OrderCouponQuery::create()
                ->filterByUsageCanceled(true)
                ->findByOrderId($event->getOrder()->getId());

            $customerId = $event->getOrder()->getCustomerId();

            /** @var OrderCoupon $usedCoupon */
            foreach ($usedCoupons as $usedCoupon) {
                if (null !== $couponModel = CouponQuery::create()->findOneByCode($usedCoupon->getCode())) {
                    // If the coupon still exists, mark the coupon as used
                    $this->couponManager->decrementQuantity($couponModel, $customerId);
                }

                // The coupon is no longer canceled
                $usedCoupon->setUsageCanceled(false)->save();
            }
        }
    }
public function orderStatusChange(OrderEvent $event, $eventName, EventDispatcherInterface $dispatcher)
    {
        // The order has been canceled or refunded ?
        if ($event->getOrder()->isCancelled() || $event->getOrder()->isRefunded()) {
            // Cancel usage of all coupons for this order
            $usedCoupons = OrderCouponQuery::create()
                ->filterByUsageCanceled(false)
                ->findByOrderId($event->getOrder()->getId());

            $customerId = $event->getOrder()->getCustomerId();

            /** @var OrderCoupon $usedCoupon */
            foreach ($usedCoupons as $usedCoupon) {
                if (null !== $couponModel = CouponQuery::create()->findOneByCode($usedCoupon->getCode())) {
                    // If the coupon still exists, restore one usage to the usage count.
                    $this->couponManager->incrementQuantity($couponModel, $customerId);
                }

                // Mark coupon usage as canceled in the OrderCoupon table
                $usedCoupon->setUsageCanceled(true)->save();
            }
        } else {
            // Mark canceled coupons for this order as used again
            $usedCoupons = OrderCouponQuery::create()
                ->filterByUsageCanceled(true)
                ->findByOrderId($event->getOrder()->getId());

            $customerId = $event->getOrder()->getCustomerId();

            /** @var OrderCoupon $usedCoupon */
            foreach ($usedCoupons as $usedCoupon) {
                if (null !== $couponModel = CouponQuery::create()->findOneByCode($usedCoupon->getCode())) {
                    // If the coupon still exists, mark the coupon as used
                    $this->couponManager->decrementQuantity($couponModel, $customerId);
                }

                // The coupon is no longer canceled
                $usedCoupon->setUsageCanceled(false)->save();
            }
        }
    }
public function orderStatusChange ( OrderEvent $ event , $ eventName , EventDispatcherInterface $ dispatcher ) { // The order has been canceled or refunded ? if ( $ event -> getOrder ( ) -> isCancelled ( ) || $ event -> getOrder ( ) -> isRefunded ( ) ) { // Cancel usage of all coupons for this order $ usedCoupons = OrderCouponQuery :: create ( ) -> filterByUsageCanceled ( false ) -> findByOrderId ( $ event -> getOrder ( ) -> getId ( ) ) ; $ customerId = $ event -> getOrder ( ) -> getCustomerId ( ) ; /** @var OrderCoupon $usedCoupon */ foreach ( $ usedCoupons as $ usedCoupon ) { if ( null !== $ couponModel = CouponQuery :: create ( ) -> findOneByCode ( $ usedCoupon -> getCode ( ) ) ) { // If the coupon still exists, restore one usage to the usage count. $ this -> couponManager -> incrementQuantity ( $ couponModel , $ customerId ) ; } // Mark coupon usage as canceled in the OrderCoupon table $ usedCoupon -> setUsageCanceled ( true ) -> save ( ) ; } } else { // Mark canceled coupons for this order as used again $ usedCoupons = OrderCouponQuery :: create ( ) -> filterByUsageCanceled ( true ) -> findByOrderId ( $ event -> getOrder ( ) -> getId ( ) ) ; $ customerId = $ event -> getOrder ( ) -> getCustomerId ( ) ; /** @var OrderCoupon $usedCoupon */ foreach ( $ usedCoupons as $ usedCoupon ) { if ( null !== $ couponModel = CouponQuery :: create ( ) -> findOneByCode ( $ usedCoupon -> getCode ( ) ) ) { // If the coupon still exists, mark the coupon as used $ this -> couponManager -> decrementQuantity ( $ couponModel , $ customerId ) ; } // The coupon is no longer canceled $ usedCoupon -> setUsageCanceled ( false ) -> save ( ) ; } } }
Cancels order coupons usage when order is canceled or refunded,
or use canceled coupons again if the order is no longer canceled or refunded

@param OrderEvent $event
@param string $eventName
@param EventDispatcherInterface $dispatcher
@throws \Exception
@throws \Propel\Runtime\Exception\PropelException
Cancels order coupons usage when order is canceled or refunded or use canceled coupons again if the order is no longer canceled or refunded