forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductAssembler.php
223 lines (203 loc) · 7.33 KB
/
ProductAssembler.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
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
/**
* This class is responsible for enriching product data by all required fields
* in a performant way, before it goes into ProductLazyArray or ProductListingLazyArray.
*
* If you want to enrich a whole list of products, use assembleProducts method to get the data in one query.
*
* Currently, the data is passing through Product::getProductProperties also, but this step should be removed
* and all data from getProductProperties loaded on demand in the lazy arrays.
*/
class ProductAssemblerCore
{
private $context;
private $searchContext;
/**
* ProductAssemblerCore constructor.
*
* @param Context $context
*/
public function __construct(Context $context)
{
$this->context = $context;
$this->searchContext = new ProductSearchContext($context);
}
/**
* Add missing product fields.
*
* @param array $rawProduct
*
* @return array
*
* @throws PrestaShopDatabaseException
*/
private function addMissingProductFields(array $rawProduct): array
{
// If there is no ID product provided, return the original data
if (empty($rawProduct['id_product'])) {
return $rawProduct;
}
$sql = $this->getSqlQueryProductFields([(int) $rawProduct['id_product']]);
$rows = Db::getInstance()->executeS($sql);
if (empty($rows)) {
return $rawProduct;
}
return array_merge($rows[0], $rawProduct);
}
/**
* Add missing product fields to multiple products.
*
* @param array $rawProducts
*
* @return array
*
* @throws PrestaShopDatabaseException
*/
private function addMissingProductFieldsForMultipleProducts(array $rawProducts): array
{
// Get product IDs we want to retrieve from database
$productIds = array_column($rawProducts, 'id_product');
// If there were no product IDs provided or somebody passed an empty array,
// return the original data
if (empty($productIds)) {
return $rawProducts;
}
// Retrieve data and reassign them to new array by their key
$productData = [];
$sql = $this->getSqlQueryProductFields($productIds);
$rows = Db::getInstance()->executeS($sql);
foreach ($rows as $row) {
$productData[(int) $row['id_product']] = $row;
}
// Use this data to enrich the products and return it
foreach ($rawProducts as &$rawProduct) {
if (isset($productData[$rawProduct['id_product']])) {
$rawProduct = array_merge($productData[$rawProduct['id_product']], $rawProduct);
}
}
return $rawProducts;
}
/**
* Return the SQL query to get all product fields.
*
* @param array $productIds
*
* @return string
*/
private function getSqlQueryProductFields(array $productIds): string
{
// Get basic configuration
$idShop = $this->searchContext->getIdShop();
$idShopGroup = $this->searchContext->getIdShopGroup();
$isStockSharingBetweenShopGroupEnabled = $this->searchContext->isStockSharingBetweenShopGroupEnabled();
$idLang = $this->searchContext->getIdLang();
$prefix = _DB_PREFIX_;
$nbDaysNewProduct = (int) Configuration::get('PS_NB_DAYS_NEW_PRODUCT');
if (!Validate::isUnsignedInt($nbDaysNewProduct)) {
$nbDaysNewProduct = 20;
}
$now = date('Y-m-d') . ' 00:00:00';
$sql = "SELECT
p.*,
ps.*,
pl.*,
sa.out_of_stock,
IFNULL(sa.quantity, 0) as quantity,
(DATEDIFF(
p.`date_add`,
DATE_SUB(
'$now',
INTERVAL $nbDaysNewProduct DAY
)
) > 0) as new
FROM {$prefix}product p
LEFT JOIN {$prefix}product_lang pl
ON pl.id_product = p.id_product
AND pl.id_shop = $idShop
AND pl.id_lang = $idLang
LEFT JOIN {$prefix}stock_available sa ";
if ($isStockSharingBetweenShopGroupEnabled) {
$sql .= " ON sa.id_product = p.id_product
AND sa.id_shop = 0
AND sa.id_product_attribute = 0
AND sa.id_shop_group = $idShopGroup ";
} else {
$sql .= " ON sa.id_product = p.id_product
AND sa.id_product_attribute = 0
AND sa.id_shop = $idShop ";
}
$sql .= "LEFT JOIN {$prefix}product_shop ps
ON ps.id_product = p.id_product
AND ps.id_shop = $idShop
WHERE p.id_product IN (" . implode(',', $productIds) . ')';
return $sql;
}
/**
* Get basic product data for single product.
* The only required property is id_product.
* If some data were already provided in $rawProduct, it won't be overwritten.
*
* @param array $rawProduct
*
* @return mixed
*
* @throws PrestaShopDatabaseException
*/
public function assembleProduct(array $rawProduct)
{
$enrichedProduct = $this->addMissingProductFields($rawProduct);
return Product::getProductProperties(
$this->searchContext->getIdLang(),
$enrichedProduct,
$this->context
);
}
/**
* Get basic product data for multiple products.
* The only required property for each product is id_product.
* If some data were already provided in $rawProducts, it won't be overwritten.
*
* @param array $rawProducts Array with multiple products
*
* @return mixed
*
* @throws PrestaShopDatabaseException
*/
public function assembleProducts(array $rawProducts)
{
$enrichedProducts = $this->addMissingProductFieldsForMultipleProducts($rawProducts);
foreach ($enrichedProducts as &$product) {
$product = Product::getProductProperties(
$this->searchContext->getIdLang(),
$product,
$this->context
);
}
return $enrichedProducts;
}
}