Back to PhocacartCartCalculation class

Method calculateCartCoupons

public
calculateCartCoupons
(mixed &$fullItems, mixed &$fullItemsGroup, mixed &$total, mixed &$coupon)

Method calculateCartCoupons - Source code

// ============
// CART COUPON
// ============
/*
 * $coupon ... coupon ID and Title set in checkout
 * $couponO ... coupon object
 * $couponDb ... all the information from coupon
 * $validCoupon ... is or isn't valid TRUE/FALSE
 */
public function calculateCartCoupons(&$fullItems, &$fullItemsGroup, &$total, &$coupon)
{
    $couponO = new PhocacartCoupon();
    $couponO->setType($this->type);
    $couponO->setCoupon($coupon['id']);
    $couponDb = $couponO->getCoupon();
    foreach ($fullItems as $k => $v) {
        $validCoupon = $couponO->checkCoupon(0, $v['id'], $v['catid'], $total['quantity'], $total['netto'], $total['subtotalnetto']);
        if ($validCoupon) {
            $validCouponId = $couponDb['id'];
            $validCouponAmount = $couponDb['discount'];
            if ($couponDb['free_shipping'] == 1) {
                $total['free_shipping'] = $couponDb['free_shipping'];
            }
            if ($couponDb['free_payment'] == 1) {
                $total['free_payment'] = $couponDb['free_payment'];
            }
            if (isset($couponDb['discount']) && isset($couponDb['calculation_type'])) {
                $fullItems[$k]['couponcart'] = 1;
                $fullItems[$k]['couponcarttitle'] = $couponDb['title'];
                $fullItems[$k]['couponcartid'] = $couponDb['id'];
                if ($couponDb['calculation_type'] == 0) {
                    // FIXED AMOUNT
                    // We need to divide fixed couponDb amount to products which meet the couponDb ID rule
                    // There can be two products in the cart and each can meet other couponDb rules
                    if (isset($total['couponcartfixedamount'][$validCouponId]['quantity'])) {
                        $total['couponcartfixedamount'][$validCouponId]['quantity'] += $v['quantity'];
                    } else {
                        $total['couponcartfixedamount'][$validCouponId]['quantity'] = $v['quantity'];
                    }
                    if (isset($total['couponcartfixedamount'][$validCouponId]['netto'])) {
                        $total['couponcartfixedamount'][$validCouponId]['netto'] += $v['netto'] * $v['quantity'];
                    } else {
                        $total['couponcartfixedamount'][$validCouponId]['netto'] = $v['netto'] * $v['quantity'];
                    }
                    $total['couponcartfixedamount'][$validCouponId]['discount'] = $validCouponAmount;
                    $fullItems[$k]['couponcartfixedid'] = $validCouponId;
                } else {
                    // PERCENTAGE
                    PhocacartCalculation::calculateDiscountPercentage($couponDb['discount'], $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
                    PhocacartCalculation::correctItemsIfNull($fullItems[$k]);
                    PhocacartCalculation::correctTotalIfNull($total, $v['taxkey']);
                    $price = new PhocacartPrice();
                    $total['couponcarttxtsuffix'] = ' (' . $price->cleanPrice($couponDb['discount']) . ' %)';
                }
            }
        }
        // !!! VALID COUPON
        // In case the coupon is valid at least for one product or one category it is then valid
        // and will be divided into valid products/categories
        // As global we mark it as valid - so change the valid coupon variable only in case it is valid
        if ($validCoupon == 1) {
            $coupon['valid'] = $validCoupon;
        }
        $fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
        /*
         *
         * Must be done in recalculateCartCoupons for fixed coupons
         */
        if (isset($couponDb['calculation_type']) && $couponDb['calculation_type'] != 0) {
            if (isset($fullItems[$k]['nettodiscount']) && !$this->posbruttocalculation) {
                $fullItems[$k]['finaldiscount'] = $fullItems[$k]['nettodiscount'] * $v['quantity'];
            } else {
                if (isset($fullItems[$k]['bruttodiscount'])) {
                    $fullItems[$k]['finaldiscount'] = $fullItems[$k]['bruttodiscount'] * $v['quantity'];
                }
            }
        }
        if ($this->correctsubtotal) {
            $this->correctSubTotal($fullItems[$k], $total);
        }
    }
}