Product.class.php
3.05 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
<?php
/**
* User: graymur
* Date: 01.10.13
* Time: 16:38
*/
namespace Cpeople\Classes\Catalog;
class Product extends \Cpeople\Classes\Block\Object
{
private $price = null;
function getOffers()
{
if (!class_exists('\CCatalogSKU'))
{
\CModule::IncludeModule('catalog');
}
$retval = array();
$arInfo = \CCatalogSKU::GetInfoByProductIBlock($this->iblock_id);
if (is_array($arInfo))
{
$retval = \Cpeople\Classes\Block\Getter::instance()->setFilter(array(
'IBLOCK_ID' => $arInfo['IBLOCK_ID'],
'PROPERTY_' . $arInfo['SKU_PROPERTY_ID'] => $this->id
))
->setClassName('\Cpeople\Classes\Catalog\Offer')
->get();
}
return empty_array($retval) ? false : $retval;
}
function getBasketId()
{
return $this->id;
}
function getOfferBasketId()
{
$offers = $this->getOffers();
return $offers ? $offers[0]->id : $this->id;
}
public function getOldPrice()
{
$priceObj = $this->getPriceObj();
return $priceObj['PRICE']['PRICE'];
}
public function getCurrency()
{
$priceObj = $this->getPriceObj();
return $priceObj['PRICE']['CURRENCY'];
}
public function getPrice()
{
$priceObj = $this->getPriceObj();
return $priceObj['DISCOUNT_PRICE'];
}
public function getDiscountPrice()
{
return $this->getPrice();
}
public function hasDiscount()
{
$priceObj = $this->getPriceObj();
return $priceObj['PRICE']['PRICE'] > $priceObj['DISCOUNT_PRICE'];
}
public function getDiscountPercent()
{
return $this->hasDiscount() ? 100 - round(100 * $this->getPrice() / $this->getOldPrice()) : false;
}
public function isForSale()
{
$price = $this->getPriceObj();
return (bool) ($price['DISCOUNT_PRICE'] && $this->getQuantity());
}
public function getQuantity()
{
if($this->CATALOG_QUANTITY === null)
{
$this->fillCatalogData();
}
return $this->CATALOG_QUANTITY;
}
protected function getPriceObj()
{
if($this->price === null)
{
$this->price = \CCatalogProduct::GetOptimalPrice($this->ID);
// echo '<pre>'.print_r($this->price,true).'</pre>';
}
return $this->price;
}
protected function fillCatalogData()
{
static $result = array();
if(!isset($result[$this->ID]))
{
$ar = \Cpeople\Classes\Block\Getter::instance()
->setSelectFields(array('ID', 'CATALOG_QUANTITY'))
->getArrayById($this->ID);
foreach($ar as $key=>$val)
{
if($this->data[$key] === null) $this->data[$key] = $val;
}
$result[$this->ID] = true;
}
}
}