Back to PhocacartDiscountCart class

Method getCartDiscount

public static
getCartDiscount
(mixed $id = 0, mixed $catid = 0, mixed $quantity = 0, mixed $amount = 0, mixed $subtotalAmount = 0)

Method getCartDiscount - Source code

/*
 * id ... id of current checked product
 * quantity ... total quantity of all products
 * amount	... total amount based on all products (affected by sales, discounts, etc.)
 * subtotalAmount ... total amount based on all products (not affected by sales, discounts, etc.)
 */
public static function getCartDiscount($id = 0, $catid = 0, $quantity = 0, $amount = 0, $subtotalAmount = 0)
{
    //$app									= JFactory::getApplication();
    $paramsC = PhocacartUtils::getComponentParameters();
    $discount_priority = $paramsC->get('discount_priority', 1);
    $discount_subtotal_amount = $paramsC->get('discount_subtotal_amount', 1);
    // Cart discount applies to all cart, so we don't need to load it for each product
    // 1 mean id of cart, not id of product
    $discounts = self::getCartDiscountsById(1, 1);
    if (!empty($discounts)) {
        $bestKey = 0;
        // get the discount key which best meet the rules
        $maxDiscount = 0;
        foreach ($discounts as $k => $v) {
            // 1. ACCESS, PUBLISH CHECK, GROUP CHECK
            // Checked in SQL
            // 2. VALID DATE FROM TO CHECK
            if (isset($v['valid_from']) && isset($v['valid_to'])) {
                $valid = PhocacartDate::getActiveDate($v['valid_from'], $v['valid_to']);
                if ($valid != 1) {
                    unset($discounts[$k]);
                    continue;
                }
            } else {
                unset($discounts[$k]);
                continue;
            }
            // 3. VALID TOTAL AMOUNT
            if (isset($v['total_amount'])) {
                $currentAmount = $amount;
                if ($discount_subtotal_amount == 2) {
                    $currentAmount = $subtotalAmount;
                }
                if ($v['total_amount'] == 0) {
                    // OK we don't check the total amount as zero means, no total amount limit
                } else {
                    if ($v['total_amount'] > 0 && $currentAmount < $v['total_amount']) {
                        unset($discounts[$k]);
                        continue;
                    }
                }
            } else {
                unset($discounts[$k]);
                continue;
            }
            // 4. VALID QUANTITY
            if (isset($v['quantity_from'])) {
                if ((int) $v['quantity_from'] == 0) {
                    // OK we don't check the quantity as zero means, no quantity limit
                } else {
                    if ((int) $v['quantity_from'] > 0 && (int) $quantity < (int) $v['quantity_from']) {
                        unset($discounts[$k]);
                        continue;
                    }
                }
            } else {
                unset($discounts[$k]);
                continue;
            }
            // 5. VALID PRODUCT
            if (!empty($v['product'])) {
                $ids = explode(',', $v['product']);
                if (empty($ids)) {
                    // OK we don't check the quantity as zero means, no product limit
                } else {
                    if ($v['product_filter'] == 0) {
                        // All except the selected
                        if (in_array($id, $ids)) {
                            unset($discounts[$k]);
                            continue;
                        }
                    } else {
                        // All selected
                        if (!in_array($id, $ids)) {
                            unset($discounts[$k]);
                            continue;
                        }
                    }
                }
            }
            // 6. VALID CATEGORY
            if (!empty($v['category'])) {
                $catids = explode(',', $v['category']);
                if (empty($catids)) {
                    // OK we don't check the quantity as zero means, no category limit
                } else {
                    if ($v['category_filter'] == 0) {
                        // All except the selected
                        if (in_array($catid, $catids)) {
                            unset($discounts[$k]);
                            continue;
                        }
                    } else {
                        // All selected
                        if (!in_array($catid, $catids)) {
                            unset($discounts[$k]);
                            continue;
                        }
                    }
                }
            }
            //$ids	= explode(',', $this->coupon['product']);
            /*$catids	= explode(',', $this->coupon['category']);
            
            				// Products
            				if(!empty($ids)) {
            					if (in_array($id, $ids)) {
            						return true;
            					}
            				}
            
            				// Categories
            				if(!empty($catids)) {
            					if (in_array($catid, $catids)) {
            						return true;
            					}
            				}
            
            				// No condition regarding ids or catids was set, coupon is valid for every item
            				if (empty($ids) && empty($catids)) {
            					return true;
            				}
            
            				if (isset($ids[0]) && $ids[0] == '' && isset($catids[0]) && $catids[0] == '') {
            					return true;
            				}*/
            // 4. SELECT THE HIGHEST QUANTITY
            // When more product discounts fullfill the rules, select only one
            // Select the one with heighest quantity, e.g.:
            // minimum quantity = 10 -> discount 5%
            // minimum quantity = 20 -> discount 10%
            // minimum quantity = 30 -> discount 20%
            // If customer buys 50 items, we need to select 20% so both 5% and 10% should be unset
            // But if we have quantity_from == 0, this rule does not have quantity rule, it is first used.
            //4.1 if more discountes meet rule select the one with maxDiscount
            //4.2.if quantity is 0 for all select the largest discount (BUT be aware because of possible conflict)
            if ($discount_priority == 2) {
                if ((int) $v['quantity_from'] == 0) {
                    $maxDiscount = (int) $v['quantity_from'];
                    $bestKey = $k;
                } else {
                    if (isset($v['quantity_from']) && (int) $v['quantity_from'] > $maxDiscount) {
                        $maxDiscount = (int) $v['quantity_from'];
                        $bestKey = $k;
                    }
                }
            } else {
                if ((int) $v['discount'] == 0) {
                    $maxDiscount = (int) $v['discount'];
                    $bestKey = $k;
                } else {
                    if (isset($v['discount']) && (int) $v['discount'] > $maxDiscount) {
                        $maxDiscount = (int) $v['discount'];
                        $bestKey = $k;
                    }
                }
            }
        }
        // POSSIBLE CONFLICT discount vs. quantity - solved by parameter
        // POSSIBLE CONFLICT percentage vs. fixed amount
        if (isset($discounts[$bestKey])) {
            return $discounts[$bestKey];
        } else {
            return false;
        }
    } else {
        return false;
    }
}