CartItem.class.php
3.81 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Created by PhpStorm.
* User: Администратор
* Date: 16.06.14
* Time: 11:56
*/
namespace Cpeople\Classes\Catalog;
use Bitrix\Main\DB\Exception;
class CartItem implements \ArrayAccess
{
protected $container;
protected $productClassName = '\Cpeople\Classes\Catalog\Product';
/**
* @var \Cpeople\Classes\Catalog\Cart
*/
protected $cart;
/**
* @var \Cpeople\Classes\Block\Object
*/
protected $product;
public function __construct($data = array(), \Cpeople\Classes\Catalog\Cart $cart)
{
$this->container = $data;
$this->cart = $cart;
}
public function offsetSet($offset, $value)
{
if (is_null($offset))
{
$this->container[] = $value;
}
else
{
$this->container[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->container[$offset]);
}
public function offsetUnset($offset)
{
unset($this->container[$offset]);
}
public function offsetGet($offset)
{
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
public function __get($key)
{
$key = strtoupper($key);
if (!isset($this->container[$key]))
{
throw new Exception('Object ' . get_class($this) . ' does not have property ' . $key);
}
return $this->container[$key];
}
public function getProduct($className = null)
{
if (!isset($this->product))
{
if (empty($className)) $className = $this->productClassName;
$this->product = \Cpeople\Classes\Block\Getter::instance()
->setClassName($className)
->getById($this['PRODUCT_ID']);
}
return $this->product;
}
public function getSum()
{
return $this->getDiscountPrice() * $this->getQuantity();
}
public function getSumWODiscount()
{
return $this->getOldPrice() * $this->getQuantity();
}
public function getPrice()
{
return $this['PRICE'];
}
public function getWeight()
{
return $this['WEIGHT'];
}
public function getBitrixDiscounts()
{
return \CCatalogDiscount::GetDiscountByProduct($this->getProduct()->id);
}
public function getDiscountPrice()
{
return $this->getProduct()->getDiscountPrice();
/*return \CCatalogProduct::CountPriceWithDiscount(
$this->getOldPrice(),
$this['CURRENCY'],
$this->getBitrixDiscounts()
);*/
}
public function getOldPrice()
{
return $this->getProduct()->getOldPrice();
}
public function getQuantity()
{
return $this['QUANTITY'];
}
public function remove()
{
\CSaleBasket::Delete($this['ID']);
}
public function setQuantity($quantity)
{
$this['QUANTITY'] = floatval($quantity);
$this->save();
}
public function save()
{
return \CSaleBasket::Update($this['ID'], array(
'PRICE' => $this['PRICE'],
'QUANTITY' => $this['QUANTITY'],
'CURRENCY' => $this['CURRENCY'],
));
$cart->setTainted(true);
}
public function hasDiscount()
{
return $this->getOldPrice() > $this->getDiscountPrice();
}
public function getDiscountValue()
{
// return $this->getBitrixDiscounts()[0]['VALUE'];
return $this->hasDiscount() ? 100 - round(100 * $this->getDiscountPrice() / $this->getOldPrice()) : false;
}
public function getName()
{
return $this['NAME'];
}
}