Check if cart meets basic requirements for a coupon.

@param CartInterface   $cart   Cart
@param CouponInterface $coupon Coupon

@throws CouponIncompatibleException Coupon incompatible
330476
public function validateCoupon(
        CartInterface $cart,
        CouponInterface $coupon
    ) {
        if ($cart->getTotalItemNumber() === 0) {
            throw new CouponIncompatibleException();
        }

        $this
            ->couponManager
            ->checkCoupon($coupon);
    }
112440
public function notifyCouponUsage(CouponInterface $coupon)
    {
        $couponUsedEvent = new CouponOnUsedEvent($coupon);

        $this->eventDispatcher->dispatch(
            ElcodiCouponEvents::COUPON_USED,
            $couponUsedEvent
        );

        return $this;
    }
787559
public function process()
	{
		$view = $this->getView();
		$context = $this->_getContext();

		switch( $view->param( 'b-action' ) )
		{
			case 'coupon-delete':

				if( ( $coupon = $view->param( 'b-coupon' ) ) != '' )
				{
					$this->_clearCached();
					$cntl = Controller_Frontend_Factory::createController( $context, 'basket' );
					$cntl->deleteCoupon( $coupon );
				}

				break;

			default:

				if( ( $coupon = $view->param( 'b-coupon' ) ) != '' )
				{
					$this->_clearCached();
					$cntl = Controller_Frontend_Factory::createController( $context, 'basket' );

					/** client/html/basket/standard/coupon/allowed
					 * Number of coupon codes a customer is allowed to enter
					 *
					 * This configuration option enables shop owners to limit the number of coupon
					 * codes that can be added by a customer to his current basket. By default, only
					 * one coupon code is allowed per order.
					 *
					 * Coupon codes are valid until a payed order is placed by the customer. The
					 * "count" of the codes is decreased afterwards. If codes are not personalized
					 * the codes can be reused in the next order until their "count" reaches zero.
					 *
					 * @param integer Positive number of coupon codes including zero
					 * @since 2014.05
					 * @category User
					 * @category Developer
					 */
					$allowed = $context->getConfig()->get( 'client/html/basket/standard/coupon/allowed', 1 );

					if( $allowed <= count( $cntl->get()->getCoupons() ) ) {
						throw new Client_Html_Exception( sprintf( 'Number of coupon codes exceeds the limit' ) );
					}

					$cntl->addCoupon( $coupon );
				}

				break;
		}

		parent::process();
	}
592759
public function refreshCouponAmount(CartInterface $cart)
    {
        $couponAmount = Money::create(
            0,
            $this
                ->currencyWrapper
                ->get()
        );

        $coupons = $this
            ->cartCouponManager
            ->getCoupons($cart);

        foreach ($coupons as $coupon) {
            $currentCouponAmount = $this
                ->cartCouponApplicatorCollector
                ->getCouponAbsoluteValue(
                    $cart,
                    $coupon
                );
            $coupon->setAbsolutePrice($currentCouponAmount);
            $couponAmount = $couponAmount->add($currentCouponAmount);
        }

        $cart->setCouponAmount($couponAmount);
    }
1275990
public function dispatchCartCouponOnRemoveEvent(
        CartCouponInterface $cartCoupon
    ) {
        $cart = $cartCoupon->getCart();
        $coupon = $cartCoupon->getCoupon();

        $event = new CartCouponOnRemoveEvent($cart, $coupon);
        $event->setCartCoupon($cartCoupon);
        $this->eventDispatcher->dispatch(
            ElcodiCartCouponEvents::CART_COUPON_ONREMOVE,
            $event
        );
    }
1079524
private function getNonAppliedAutomaticCoupons(CartInterface $cart)
    {
        $automaticCoupons = $this
            ->couponRepository
            ->findBy([
                'enforcement' => ElcodiCouponTypes::ENFORCEMENT_AUTOMATIC,
            ]);

        $loadedAutomaticCoupons = $this
            ->cartCouponManager
            ->getCoupons($cart);

        return array_udiff(
            $automaticCoupons,
            $loadedAutomaticCoupons,
            function (CouponInterface $a, CouponInterface $b) {
                return $a->getId() != $b->getId();
            }
        );
    }
303764
public function decrementQuantity(Coupon $coupon, $customerId = null)
    {
        if ($coupon->isUsageUnlimited()) {
            return true;
        } else {
            try {
                $usageLeft = $coupon->getUsagesLeft($customerId);

                if ($usageLeft > 0) {
                    // If the coupon usage is per user, add an entry to coupon customer usage count table
                    if ($coupon->getPerCustomerUsageCount()) {
                        if (null == $customerId) {
                            throw new \LogicException("Customer should not be null at this time.");
                        }

                        $ccc = CouponCustomerCountQuery::create()
                            ->filterByCouponId($coupon->getId())
                            ->filterByCustomerId($customerId)
                            ->findOne()
                        ;

                        if ($ccc === null) {
                            $ccc = new CouponCustomerCount();

                            $ccc
                                ->setCustomerId($customerId)
                                ->setCouponId($coupon->getId())
                                ->setCount(0);
                        }

                        $newCount = 1 + $ccc->getCount();

                        $ccc
                            ->setCount($newCount)
                            ->save()
                        ;

                        return $usageLeft - $newCount;
                    } else {
                        $coupon->setMaxUsage(--$usageLeft);

                        $coupon->save();

                        return $usageLeft;
                    }
                }
            } catch (\Exception $ex) {
                // Just log the problem.
                Tlog::getInstance()->addError(sprintf("Failed to decrement coupon %s: %s", $coupon->getCode(), $ex->getMessage()));
            }
        }

        return false;
    }
700008
public function isCouponRemovingPostage(Order $order)
    {
        $coupons = $this->facade->getCurrentCoupons();

        if (\count($coupons) == 0) {
            return false;
        }

        $couponsKept = $this->sortCoupons($coupons);

        /** @var CouponInterface $coupon */
        foreach ($couponsKept as $coupon) {
            if ($coupon->isRemovingPostage()) {
                // Check if delivery country is on the list of countries for which delivery is free
                // If the list is empty, the shipping is free for all countries.
                $couponCountries = $coupon->getFreeShippingForCountries();

                if (! $couponCountries->isEmpty()) {
                    if (null === $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress())) {
                        continue;
                    }

                    $countryValid = false;

                    $deliveryCountryId = $deliveryAddress->getCountryId();

                    /** @var CouponCountry $couponCountry */
                    foreach ($couponCountries as $couponCountry) {
                        if ($deliveryCountryId == $couponCountry->getCountryId()) {
                            $countryValid = true;
                            break;
                        }
                    }

                    if (! $countryValid) {
                        continue;
                    }
                }

                // Check if shipping method is on the list of methods for which delivery is free
                // If the list is empty, the shipping is free for all methods.
                $couponModules = $coupon->getFreeShippingForModules();

                if (! $couponModules->isEmpty()) {
                    $moduleValid = false;

                    $shippingModuleId = $order->getDeliveryModuleId();

                    /** @var CouponModule $couponModule */
                    foreach ($couponModules as $couponModule) {
                        if ($shippingModuleId == $couponModule->getModuleId()) {
                            $moduleValid = true;
                            break;
                        }
                    }

                    if (! $moduleValid) {
                        continue;
                    }
                }

                // All conditions are met, the shipping is free !
                return true;
            }
        }

        return false;
    }
551782
public function buildCouponFromModel(Coupon $model)
    {
        $isCumulative = ($model->getIsCumulative() == 1 ? true : false);
        $isRemovingPostage = ($model->getIsRemovingPostage() == 1 ? true : false);

        if (!$this->container->has($model->getType())) {
            return false;
        }

        /** @var CouponInterface $couponManager*/
        $couponManager = $this->container->get($model->getType());
        $couponManager->set(
            $this->facade,
            $model->getCode(),
            $model->getTitle(),
            $model->getShortDescription(),
            $model->getDescription(),
            $model->getEffects(),
            $isCumulative,
            $isRemovingPostage,
            $model->getIsAvailableOnSpecialOffers(),
            $model->getIsEnabled(),
            $model->getMaxUsage(),
            $model->getExpirationDate(),
            $model->getFreeShippingForCountries(),
            $model->getFreeShippingForModules(),
            $model->getPerCustomerUsageCount()
        );

        /** @var ConditionFactory $conditionFactory */
        $conditionFactory = $this->container->get('thelia.condition.factory');
        $conditions = $conditionFactory->unserializeConditionCollection(
            $model->getSerializedConditions()
        );

        $couponManager->setConditions($conditions);

        return clone $couponManager;
    }
1296246
public function validateCartIntegrity(CartInterface $cart)
    {
        /**
         * Check every CartLine.
         *
         * @var CartLineInterface $cartLine
         */
        foreach ($cart->getCartLines() as $cartLine) {
            $this->validateCartLine($cartLine);
        }
    }
1348551
public function setCoupon()
    {
        foreach ($this->cart->getExternalCoupons() as $coupon) {
            $this->setCouponToQuote($coupon);
        }

        return $this->quote;
    }
352136
def add(self, quote_id, coupon_code, store_view=None):
        
        return bool(
            self.call('cart_coupon.add', [quote_id, coupon_code, store_view])
        )
1110887
public function getCouponAbsoluteValue(
        CartInterface $cart,
        CouponInterface $coupon
    ) {
        $currency = $this
            ->currencyWrapper
            ->get();

        $amount = $coupon->getPrice();

        return $this
            ->currencyConverter
            ->convertMoney(
                $amount,
                $currency
            );
    }
921699
public function addCoupon( MShop_Order_Item_Base_Interface $base )
	{
		if( $this->_getObject()->isAvailable( $base ) === false ) {
			return;
		}

		$config = $this->_getItem()->getConfig();

		if( !isset( $config['freeshipping.productcode'] ) )
		{
			throw new MShop_Coupon_Exception( sprintf(
				'Invalid configuration for coupon provider "%1$s", needs "%2$s"',
				$this->_getItem()->getProvider(), 'freeshipping.productcode'
			) );
		}

		$price = clone ( $base->getService('delivery')->getPrice() );
		$price->setRebate( $price->getCosts() );
		$price->setCosts( -$price->getCosts() );

		$orderProduct = $this->_createProduct( $config['freeshipping.productcode'], 1 );
		$orderProduct->setPrice( $price );

		$base->addCoupon( $this->_getCode(), array( $orderProduct ) );
	}
402358
public function set(CartInterface $cart)
    {
        if (!$this->saveInSession) {
            return $this;
        }

        $this
            ->session
            ->set(
                $this->sessionFieldName,
                $cart->getId()
            );

        return $this;
    }
327232
public function checkMinAmount($minAmount, $throwErrors = true)
    {
        $laraCart = \App::make(LaraCart::SERVICE);

        if ($laraCart->subTotal(false, false, false) >= $minAmount) {
            return true;
        } else {
            if ($throwErrors) {
                throw new CouponException('You must have at least a total of '.$laraCart->formatMoney($minAmount));
            } else {
                return false;
            }
        }
    }
1349759
public function validateNegativeAmount(CartInterface $cart)
    {
        $amount = $cart->getAmount();

        if ($amount->getAmount() <= 0) {
            $cart
                ->setAmount(Money::create(
                    0,
                    $amount->getCurrency()
                ));
        }
    }