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;
    }
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;
    }
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 ; }
Decrement this coupon quantity

To call when a coupon is consumed

@param \Thelia\Model\Coupon $coupon     Coupon consumed
@param int|null             $customerId the ID of the ordering customer

@return int Usage left after decremental
Decrement this coupon quantity