Back to PhocacartCartCalculation class

Method calculateCartDiscounts

public
calculateCartDiscounts
(mixed &$fullItems, mixed &$fullItemsGroup, mixed &$total, mixed &$cartDiscount)

Method calculateCartDiscounts - Source code

// =============
// CART DISCOUNT
// =============
public function calculateCartDiscounts(&$fullItems, &$fullItemsGroup, &$total, &$cartDiscount)
{
    // If there are more cart discounts e.g. separated by different rules
    // remove the suffix because it will be not valid
    $discountSuffixItems = array();
    foreach ($fullItems as $k => $v) {
        $discount = PhocacartDiscountCart::getCartDiscount($v['id'], $v['catid'], $total['quantity'], $total['netto'], $total['subtotalnetto']);
        // First check if there is even some discount
        if ($discount) {
            $discountId = $discount['id'];
            $discountAmount = $discount['discount'];
            if ($discount['free_shipping'] == 1) {
                $total['free_shipping'] = $discount['free_shipping'];
            }
            if ($discount['free_payment'] == 1) {
                $total['free_payment'] = $discount['free_payment'];
            }
            if (isset($discount['discount']) && isset($discount['calculation_type'])) {
                $fullItems[$k]['discountcart'] = 1;
                $fullItems[$k]['discountcarttitle'] = $discount['title'];
                $fullItems[$k]['discountcartid'] = $discount['id'];
                if ($discount['calculation_type'] == 0) {
                    // FIXED AMOUNT
                    // We need to divide fixed discount amount to products which meet the discount ID rule
                    // There can be two products in the cart and each can meet other discount rules
                    if (isset($total['discountcartfixedamount'][$discountId]['quantity'])) {
                        $total['discountcartfixedamount'][$discountId]['quantity'] += $v['quantity'];
                    } else {
                        $total['discountcartfixedamount'][$discountId]['quantity'] = $v['quantity'];
                    }
                    if (isset($total['discountcartfixedamount'][$discountId]['netto'])) {
                        $total['discountcartfixedamount'][$discountId]['netto'] += $v['netto'] * $v['quantity'];
                    } else {
                        $total['discountcartfixedamount'][$discountId]['netto'] = $v['netto'] * $v['quantity'];
                    }
                    $total['discountcartfixedamount'][$discountId]['discount'] = $discountAmount;
                    $fullItems[$k]['discountcartfixedid'] = $discountId;
                } else {
                    // PERCENTAGE
                    PhocacartCalculation::calculateDiscountPercentage($discount['discount'], $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
                    $discountSuffixItems[$discount['discount']] = $discount['discount'];
                    if (count($discountSuffixItems) > 1) {
                        // There are different types of discounts, remove the suffix Cart discount (10%) become Cart discount. Because if there is
                        // e.g. 5% and 10% then the 10% in () will be misleading
                        $total['discountcarttxtsuffix'] = '';
                    } else {
                        $price = new PhocacartPrice();
                        $total['discountcarttxtsuffix'] = ' (' . $price->cleanPrice($discount['discount']) . ' %)';
                    }
                }
                $cartDiscount['id'] = $discount['id'];
                $cartDiscount['title'] = $discount['title'];
            }
        }
        $fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
        /*
                     * Must be done in recalculateCartDiscounts()
                     *
                    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);
        }
    }
}