forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rule.php
412 lines (376 loc) · 13.4 KB
/
Rule.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_CatalogRule
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Enter description here ...
*
* @method Mage_CatalogRule_Model_Resource_Rule _getResource()
* @method Mage_CatalogRule_Model_Resource_Rule getResource()
* @method string getName()
* @method Mage_CatalogRule_Model_Rule setName(string $value)
* @method string getDescription()
* @method Mage_CatalogRule_Model_Rule setDescription(string $value)
* @method string getFromDate()
* @method Mage_CatalogRule_Model_Rule setFromDate(string $value)
* @method string getToDate()
* @method Mage_CatalogRule_Model_Rule setToDate(string $value)
* @method Mage_CatalogRule_Model_Rule setCustomerGroupIds(string $value)
* @method int getIsActive()
* @method Mage_CatalogRule_Model_Rule setIsActive(int $value)
* @method string getConditionsSerialized()
* @method Mage_CatalogRule_Model_Rule setConditionsSerialized(string $value)
* @method string getActionsSerialized()
* @method Mage_CatalogRule_Model_Rule setActionsSerialized(string $value)
* @method int getStopRulesProcessing()
* @method Mage_CatalogRule_Model_Rule setStopRulesProcessing(int $value)
* @method int getSortOrder()
* @method Mage_CatalogRule_Model_Rule setSortOrder(int $value)
* @method string getSimpleAction()
* @method Mage_CatalogRule_Model_Rule setSimpleAction(string $value)
* @method float getDiscountAmount()
* @method Mage_CatalogRule_Model_Rule setDiscountAmount(float $value)
* @method string getWebsiteIds()
* @method Mage_CatalogRule_Model_Rule setWebsiteIds(string $value)
*
* @category Mage
* @package Mage_CatalogRule
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_CatalogRule_Model_Rule extends Mage_Rule_Model_Rule
{
const XML_NODE_RELATED_CACHE = 'global/catalogrule/related_cache_types';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'catalogrule_rule';
/**
* Parameter name in event
*
* In observe method you can use $observer->getEvent()->getRule() in this case
*
* @var string
*/
protected $_eventObject = 'rule';
/**
* Matched product ids array
*
* @var array
*/
protected $_productIds;
/**
* Limitation for products collection
*
* @var int|array|null
*/
protected $_productsFilter = null;
protected $_now;
/**
* Cached data of prices calculated by price rules
*
* @var array
*/
protected static $_priceRulesData = array();
/**
* Init resource model and id field
*/
protected function _construct()
{
parent::_construct();
$this->_init('Mage_CatalogRule_Model_Resource_Rule');
$this->setIdFieldName('rule_id');
}
/**
* Filtering products that must be checked for matching with rule
*
* @param int|array $productIds
*/
public function setProductsFilter($productIds)
{
$this->_productsFilter = $productIds;
}
/**
* Returns products filter
*
* @return array|int|null
*/
public function getProductsFilter()
{
return $this->_productsFilter;
}
public function getConditionsInstance()
{
return Mage::getModel('Mage_CatalogRule_Model_Rule_Condition_Combine');
}
public function getActionsInstance()
{
return Mage::getModel('Mage_CatalogRule_Model_Rule_Action_Collection');
}
public function getNow()
{
if (!$this->_now) {
return now();
}
return $this->_now;
}
public function setNow($now)
{
$this->_now = $now;
}
public function toString($format='')
{
$str = Mage::helper('Mage_CatalogRule_Helper_Data')->__("Name: %s", $this->getName()) ."\n"
. Mage::helper('Mage_CatalogRule_Helper_Data')->__("Start at: %s", $this->getStartAt()) ."\n"
. Mage::helper('Mage_CatalogRule_Helper_Data')->__("Expire at: %s", $this->getExpireAt()) ."\n"
. Mage::helper('Mage_CatalogRule_Helper_Data')->__("Customer Registered: %s", $this->getCustomerRegistered()) ."\n"
. Mage::helper('Mage_CatalogRule_Helper_Data')->__("Customer is a New Buyer: %s", $this->getCustomerNewBuyer()) ."\n"
. Mage::helper('Mage_CatalogRule_Helper_Data')->__("Description: %s", $this->getDescription()) ."\n\n"
. $this->getConditions()->toStringRecursive() ."\n\n"
. $this->getActions()->toStringRecursive() ."\n\n";
return $str;
}
/**
* Returns rule as an array for admin interface
*
* Output example:
* array(
* 'name'=>'Example rule',
* 'conditions'=>{condition_combine::toArray}
* 'actions'=>{action_collection::toArray}
* )
*
* @return array
*/
public function toArray(array $arrAttributes = array())
{
$out = parent::toArray($arrAttributes);
$out['customer_registered'] = $this->getCustomerRegistered();
$out['customer_new_buyer'] = $this->getCustomerNewBuyer();
return $out;
}
/**
* Invalidate related cache types
*
* @return Mage_CatalogRule_Model_Rule
*/
protected function _invalidateCache()
{
$types = Mage::getConfig()->getNode(self::XML_NODE_RELATED_CACHE);
if ($types) {
$types = $types->asArray();
Mage::app()->getCacheInstance()->invalidateType(array_keys($types));
}
return $this;
}
/**
* Process rule related data after rule save
*
* @return Mage_CatalogRule_Model_Rule
*/
protected function _afterSave()
{
$this->_getResource()->updateRuleProductData($this);
parent::_afterSave();
}
/**
* Get array of product ids which are matched by rule
*
* @return array
*/
public function getMatchingProductIds()
{
if (is_null($this->_productIds)) {
$this->_productIds = array();
$this->setCollectedAttributes(array());
$websiteIds = $this->getWebsiteIds();
if (!is_array($websiteIds)) {
$websiteIds = explode(',', $websiteIds);
}
if ($websiteIds) {
$productCollection = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Collection')
->addWebsiteFilter($websiteIds);
if ($this->_productsFilter) {
$productCollection->addIdFilter($this->_productsFilter);
}
$this->getConditions()->collectValidatedAttributes($productCollection);
Mage::getSingleton('Mage_Core_Model_Resource_Iterator')->walk(
$productCollection->getSelect(),
array(array($this, 'callbackValidateProduct')),
array(
'attributes' => $this->getCollectedAttributes(),
'product' => Mage::getModel('Mage_Catalog_Model_Product'),
)
);
}
}
return $this->_productIds;
}
/**
* Callback function for product matching
*
* @param $args
* @return void
*/
public function callbackValidateProduct($args)
{
$product = clone $args['product'];
$product->setData($args['row']);
if ($this->getConditions()->validate($product)) {
$this->_productIds[] = $product->getId();
}
}
/**
* Apply rule to product
*
* @param int|Mage_Catalog_Model_Product $product
* @param array $websiteIds
* @return void
*/
public function applyToProduct($product, $websiteIds=null)
{
if (is_numeric($product)) {
$product = Mage::getModel('Mage_Catalog_Model_Product')->load($product);
}
if (is_null($websiteIds)) {
$websiteIds = explode(',', $this->getWebsiteIds());
}
$this->getResource()->applyToProduct($this, $product, $websiteIds);
}
/**
* Get array of assigned customer group ids
*
* @return array
*/
public function getCustomerGroupIds()
{
$ids = $this->getData('customer_group_ids');
if (($ids && !$this->getCustomerGroupChecked()) || is_string($ids)) {
if (is_string($ids)) {
$ids = explode(',', $ids);
}
$groupIds = Mage::getModel('Mage_Customer_Model_Group')->getCollection()->getAllIds();
$ids = array_intersect($ids, $groupIds);
$this->setData('customer_group_ids', $ids);
$this->setCustomerGroupChecked(true);
}
return $ids;
}
/**
* Apply all price rules, invalidate related cache and refresh price index
*
* @return Mage_CatalogRule_Model_Rule
*/
public function applyAll()
{
$this->getResourceCollection()->walk(array($this->_getResource(), 'updateRuleProductData'));
$this->_getResource()->applyAllRulesForDateRange();
$this->_invalidateCache();
$indexProcess = Mage::getSingleton('Mage_Index_Model_Indexer')->getProcessByCode('catalog_product_price');
if ($indexProcess) {
$indexProcess->reindexAll();
}
}
/**
* Apply all price rules to product
*
* @param int|Mage_Catalog_Model_Product $product
* @return Mage_CatalogRule_Model_Rule
*/
public function applyAllRulesToProduct($product)
{
$this->_getResource()->applyAllRulesForDateRange(NULL, NULL, $product);
$this->_invalidateCache();
if ($product instanceof Mage_Catalog_Model_Product) {
$productId = $product->getId();
} else {
$productId = $product;
}
if ($productId) {
Mage::getSingleton('Mage_Index_Model_Indexer')->processEntityAction(
new Varien_Object(array('id' => $productId)),
Mage_Catalog_Model_Product::ENTITY,
Mage_Catalog_Model_Product_Indexer_Price::EVENT_TYPE_REINDEX_PRICE
);
}
}
/**
* Calculate price using catalog price rule of product
*
* @param Mage_Catalog_Model_Product $product
* @param float $price
* @return float|null
*/
public function calcProductPriceRule(Mage_Catalog_Model_Product $product, $price)
{
$priceRules = null;
$productId = $product->getId();
$storeId = $product->getStoreId();
$websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
if ($product->hasCustomerGroupId()) {
$customerGroupId = $product->getCustomerGroupId();
} else {
$customerGroupId = Mage::getSingleton('Mage_Customer_Model_Session')->getCustomerGroupId();
}
$dateTs = Mage::app()->getLocale()->storeTimeStamp($storeId);
$cacheKey = date('Y-m-d', $dateTs) . "|$websiteId|$customerGroupId|$productId|$price";
if (!array_key_exists($cacheKey, self::$_priceRulesData)) {
$rulesData = $this->_getResource()->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
if ($rulesData) {
foreach ($rulesData as $ruleData) {
if ($product->getParentId()) {
if ($ruleData['sub_is_enable']) {
$priceRules = Mage::helper('Mage_CatalogRule_Helper_Data')->calcPriceRule(
$ruleData['sub_simple_action'],
$ruleData['sub_discount_amount'],
$priceRules ? $priceRules : $price
);
} else {
$priceRules = $price;
}
if ($ruleData['stop_rules_processing']) {
break;
}
} else {
$priceRules = Mage::helper('Mage_CatalogRule_Helper_Data')->calcPriceRule(
$ruleData['simple_action'],
$ruleData['discount_amount'],
$priceRules ? $priceRules :$price
);
if ($ruleData['stop_rules_processing']) {
break;
}
}
}
return self::$_priceRulesData[$cacheKey] = $priceRules;
} else {
self::$_priceRulesData[$cacheKey] = null;
}
} else {
return self::$_priceRulesData[$cacheKey];
}
return null;
}
}