Back to PhocacartCartCalculation class

Method calculateProductDiscounts

public
calculateProductDiscounts
(mixed &$fullItems, mixed &$fullItemsGroup, mixed &$total)

Method calculateProductDiscounts - Source code

// ================
// PRODUCT DISCOUNT
// ================
public function calculateProductDiscounts(&$fullItems, &$fullItemsGroup, &$total)
{
    foreach ($fullItems as $k => $v) {
        // Get quantity of a group. Group is sum of all product variations
        // - explained in PhocacartDiscountProduct::getProductDiscount
        $groupId = $v['id'];
        $v['quantitygroup'] = $v['quantity'];
        // define
        if (isset($fullItemsGroup[$groupId]['quantity'])) {
            $v['quantitygroup'] = $fullItemsGroup[$groupId]['quantity'];
        }
        $discount = PhocacartDiscountProduct::getProductDiscount($v['id'], $v['quantitygroup'], $v['quantity']);
        if (isset($discount['discount']) && isset($discount['calculation_type'])) {
            $fullItems[$k]['discountproduct'] = 1;
            $fullItems[$k]['discountproducttitle'] = $discount['title'];
            if ($discount['calculation_type'] == 0) {
                // FIXED AMOUNT
                if (isset($v['netto']) && $v['netto'] > 0) {
                    //$r = $discount['discount'] / $v['quantity'] * 100 / $v['netto'];// Ratio to use it for brutto and tax
                    // PRODUCT DISCOUNT - DON'T DIVIDE IT INTO QUANTITY
                    //if you set 500 fixed amount as discount - it applies to each quantity
                    $r = $discount['discount'] * 100 / $v['netto'];
                } else {
                    $r = 0;
                }
                PhocacartCalculation::calculateDiscountFixedAmount($r, $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
            } else {
                // PERCENTAGE
                PhocacartCalculation::calculateDiscountPercentage($discount['discount'], $v['quantity'], $fullItems[$k], $total, $v['taxkey']);
            }
            PhocacartCalculation::correctItemsIfNull($fullItems[$k]);
            PhocacartCalculation::correctTotalIfNull($total, $v['taxkey']);
        }
        $fullItems[$k]['final'] = $fullItems[$k]['netto'] && !$this->posbruttocalculation ? $fullItems[$k]['netto'] * $v['quantity'] : $fullItems[$k]['brutto'] * $v['quantity'];
        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);
        }
    }
}