/
Merchandiser.php
245 lines (224 loc) · 8.72 KB
/
Merchandiser.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
<?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.
*
* @category OnTap
* @package OnTap_Merchandiser
* @copyright Copyright (c) 2014 On Tap Networks Ltd. (http://www.ontapgroup.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class OnTap_Merchandiser_Model_Merchandiser
{
/**
* _data
*
* (default value: array())
*
* @var array
*/
protected $_data = array();
/**
* moveInStockToTheTop
*
* @param mixed $params
* @return void
*/
public function moveInStockToTheTop($params)
{
$catId = $params['catId'];
$merchandiserResourceModel = Mage::getResourceModel('merchandiser/merchandiser');
$outStockProducts = $merchandiserResourceModel->getOutofStockProducts($catId);
$maxPosition = $merchandiserResourceModel->getMaxInstockPositionFromCategory($catId);
if (count($outStockProducts)) {
foreach ($outStockProducts as $outStockProduct) {
$outStockProductId = $outStockProduct['product_id'];
$merchandiserResourceModel->updateProductPosition($catId, $outStockProductId, ++$maxPosition);
}
}
}
/**
* moveSaleAtTop
*
* @param mixed $params
* @return void
*/
public function moveSaleAtTop($params)
{
$catId = $params['catId'];
$merchandiserResourceModel = Mage::getResourceModel('merchandiser/merchandiser');
$readResult = $merchandiserResourceModel->getSaleCategoryProducts($catId, "DESC");
$position = 1;
foreach ($readResult as $row) {
$merchandiserResourceModel->updateProductPosition($catId, $row['product_id'], $position);
$position++;
}
}
/**
* moveSaleAtBottom
*
* @param mixed $params
* @return void
*/
public function moveSaleAtBottom($params)
{
$catId = $params['catId'];
$merchandiserResourceModel = Mage::getResourceModel('merchandiser/merchandiser');
$readResult = $merchandiserResourceModel->getSaleCategoryProducts($catId, "ASC");
$position = 1;
foreach ($readResult as $row) {
$merchandiserResourceModel->updateProductPosition($catId, $row['product_id'], $position);
$position++;
}
}
/**
* affectCategoryBySmartRule
*
* @param mixed $categoryId
* @return void
*/
public function affectCategoryBySmartRule($categoryId)
{
$merchandiserResourceModel = Mage::getResourceModel('merchandiser/merchandiser');
$insertData = array();
$allocatedProducts = array();
$iCounter = 1;
$categoryValues = $merchandiserResourceModel->getCategoryValues($categoryId);
$categoryProductsResult = $merchandiserResourceModel->getCategoryProduct($categoryId);
$positionsArray = array();
foreach ($categoryProductsResult as $categoryProductPostions) {
$positionsArray[$categoryProductPostions['product_id']] = $categoryProductPostions['position'];
}
asort($positionsArray);
$productPositions = $positionsArray;
$productPositions = array_keys($productPositions);
$merchandiserResourceModel->clearCategoryProducts($categoryId);
$categoryProducts = array_map(
array($this, 'categoryProductsMap'),
$categoryProductsResult
);
$heroProducts = $categoryValues['heroproducts'];
$productObject = Mage::getModel('catalog/product');
foreach (explode(",", $heroProducts) as $heroSKU) {
if ($heroSKU != '' && $productId = $productObject->getIdBySku(trim($heroSKU))) {
if ($productId > 0) {
if (!in_array($productId, $allocatedProducts)) {
$allocatedProducts[] = $productId;
unset($positionsArray[$productId]);
$insertData[] = array(
'category_id' => $categoryId,
'product_id' => $productId,
'position' => $iCounter
);
$iCounter++;
}
}
}
}
$addTo = Mage::helper('merchandiser')->newProductsHandler(); // 1= TOP , 2 = BOTTOM
$addTo = $addTo < 1 ? 1 : $addTo;
$categoryProducts = array_diff($categoryProducts, $allocatedProducts);
$ruledProductIds = Mage::helper('merchandiser')->smartFilter($categoryId, $categoryValues['smart_attributes']);
$ruledProductCount = $iCounter;
if (sizeof($ruledProductIds) > 0) {
$normalProductCount = sizeof($positionsArray) > 0 ? max($positionsArray) : 0;
$differenceFactor = $iCounter - $normalProductCount;
if ($differenceFactor <= 0) {
$differenceFactor = 1;
}
if ($addTo == 2 && $categoryValues['ruled_only'] == 0) {
$ruledProductCount = $differenceFactor + $normalProductCount;
}
foreach ($ruledProductIds as $productId) {
if (!in_array($productId, $allocatedProducts)) {
$allocatedProducts[] = $productId;
if ($addTo == 2) {
unset($positionsArray[$productId]);
}
$insertData[] = array(
'category_id' => $categoryId,
'product_id' => $productId,
'position' => $ruledProductCount
);
$ruledProductCount++;
}
}
}
if ($addTo == 1) {
$iCounter = $ruledProductCount;
}
if ($categoryValues['ruled_only'] == 0) {
if (sizeof($categoryProducts) > 0) {
$incrementFactor = $iCounter - min($positionsArray);
if ($incrementFactor < 0) {
$incrementFactor = 0;
}
foreach ($categoryProducts as $productId ) {
if (!in_array($productId, $allocatedProducts)) {
$allocatedProducts[] = $productId;
$currentPosition = ($positionsArray[$productId] > 0) ? $positionsArray[$productId] : 0;
$currentPosition += $incrementFactor;
$insertData[] = array(
'category_id' => $categoryId,
'product_id' => $productId,
'position' => $currentPosition
);
}
}
}
}
if (sizeof($insertData)>0) {
$merchandiserResourceModel->insertMultipleProductsToCategory($insertData);
}
}
/**
* categoryProductsMap
*
* @param mixed $value
* @return string
*/
public function categoryProductsMap($value)
{
if (is_array($value) && isset($value['product_id'])) {
return $value['product_id'];
}
}
/**
* getCategoryValues
*
* @param mixed $categoryId
* @param mixed $field (default: null)
* @return string
*/
public function getCategoryValues($categoryId, $field = null)
{
return Mage::getResourceModel('merchandiser/merchandiser')->getCategoryValues($categoryId, $field);
}
/**
* clearEntityCache
*
* @param Mage_Core_Model_Abstract $entity
* @param array $ids
* @return void
*/
public function clearEntityCache(Mage_Core_Model_Abstract $entity, array $ids)
{
$cacheTags = array();
foreach ($ids as $entityId) {
$entity->setId($entityId);
$cacheTags = array_merge($cacheTags, $entity->getCacheIdTags());
}
if (!empty($cacheTags)) {
Enterprise_PageCache_Model_Cache::getCacheInstance()->clean($cacheTags);
}
}
}