/
coupon.php
executable file
·114 lines (97 loc) · 4.29 KB
/
coupon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/*------------------------------------------------------------------------------
$Id$
AbanteCart, Ideal OpenSource Ecommerce Solution
http://www.AbanteCart.com
Copyright © 2011-2020 Belavier Commerce LLC
This source file is subject to Open Software License (OSL 3.0)
License details is bundled with this package in the file LICENSE.txt.
It is also available at this URL:
<http://www.opensource.org/licenses/OSL-3.0>
UPGRADE NOTE:
Do not edit or add to this file if you wish to upgrade AbanteCart to newer
versions in the future. If you wish to customize AbanteCart for your
needs please refer to http://www.AbanteCart.com for more information.
------------------------------------------------------------------------------*/
if (!defined('DIR_CORE')) {
header('Location: static_pages/');
}
class ModelTotalCoupon extends Model
{
public function getTotal(&$total_data, &$total, &$taxes, &$cust_data)
{
if (isset($cust_data['coupon']) && $this->config->get('coupon_status')) {
$promotion = new APromotion();
// see filtering parameters inside this method ! */
$coupon = $promotion->getCouponData($cust_data['coupon']);
if (!$coupon) {
return;
}
$discount_total = 0;
$subtotal = $this->cart->getSubTotal(true);
if (!$coupon['product']) {
$coupon_total = $subtotal;
} else {
$coupon_total = 0;
foreach ($this->cart->getProducts() as $product) {
if (in_array($product['product_id'], $coupon['product'])) {
$coupon_total += $product['total'];
}
}
}
if ($coupon['type'] == 'F') {
$coupon['discount'] = min($coupon['discount'], $coupon_total);
}
foreach ($this->cart->getProducts() as $product) {
$discount = 0;
if (!$coupon['product']) {
$status = true;
} else {
if (in_array($product['product_id'], $coupon['product'])) {
$status = true;
} else {
$status = false;
}
}
if ($status) {
if ($coupon['type'] == 'F') {
$discount = $coupon['discount'] * ($product['total'] / $coupon_total);
} elseif ($coupon['type'] == 'P') {
$discount = $product['total'] / 100 * $coupon['discount'];
}
if ($product['tax_class_id']) {
$taxes[$product['tax_class_id']]['total'] -= $discount;
$taxes[$product['tax_class_id']]['tax'] -= $this->tax->calcTotalTaxAmount(
$product['total'], $product['tax_class_id']
)
- $this->tax->calcTotalTaxAmount(
$product['total'] - $discount,
$product['tax_class_id']
);
}
}
$discount_total += $discount;
}
$ship_data = $cust_data['shipping_method'];
if ($coupon['shipping'] && isset($ship_data)) {
if (isset($ship_data['tax_class_id']) && $ship_data['tax_class_id']) {
$taxes[$ship_data['tax_class_id']]['total'] -= $ship_data['cost'];
$taxes[$ship_data['tax_class_id']]['tax'] -= $this->tax->calcTotalTaxAmount(
$ship_data['cost'],
$ship_data['tax_class_id']
);
}
$discount_total += (float)$ship_data['cost'];
}
$total_data[] = [
'id' => 'coupon',
'title' => $coupon['name'].':',
'text' => '-'.$this->currency->format($discount_total),
'value' => -$discount_total,
'sort_order' => $this->config->get('coupon_sort_order'),
'total_type' => $this->config->get('coupon_total_type'),
];
$total -= $discount_total;
}
}
}