Skip to content

Commit e375cf6

Browse files
authored
Merge pull request #38 from Hlavtox/assemble-in-bulk
Assemble products in bulk to save performance
2 parents 92f28d5 + 3a3fc53 commit e375cf6

2 files changed

Lines changed: 55 additions & 42 deletions

File tree

.github/workflows/php.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
uses: actions/checkout@v2.0.0
4242

4343
- name: Cache dependencies
44-
uses: actions/cache@v2
44+
uses: actions/cache@v4
4545
with:
4646
path: vendor
4747
key: php-${{ hashFiles('composer.lock') }}
@@ -70,14 +70,14 @@ jobs:
7070

7171
# Add vendor folder in cache to make next builds faster
7272
- name: Cache vendor folder
73-
uses: actions/cache@v1
73+
uses: actions/cache@v4
7474
with:
7575
path: vendor
7676
key: php-${{ hashFiles('composer.lock') }}
7777

7878
# Add composer local folder in cache to make next builds faster
7979
- name: Cache composer folder
80-
uses: actions/cache@v1
80+
uses: actions/cache@v4
8181
with:
8282
path: ~/.composer/cache
8383
key: php-composer-cache

ps_viewedproduct.php

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -260,51 +260,64 @@ protected function getViewedProducts()
260260
{
261261
$productIds = $this->getViewedProductIds();
262262

263-
if (!empty($productIds)) {
264-
$assembler = new ProductAssembler($this->context);
265-
266-
$presenterFactory = new ProductPresenterFactory($this->context);
267-
$presentationSettings = $presenterFactory->getPresentationSettings();
268-
if (version_compare(_PS_VERSION_, '1.7.5', '>=')) {
269-
$presenter = new \PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductListingPresenter(
270-
new ImageRetriever(
271-
$this->context->link
272-
),
273-
$this->context->link,
274-
new PriceFormatter(),
275-
new ProductColorsRetriever(),
276-
$this->context->getTranslator()
277-
);
278-
} else {
279-
$presenter = new \PrestaShop\PrestaShop\Core\Product\ProductListingPresenter(
280-
new ImageRetriever(
281-
$this->context->link
282-
),
283-
$this->context->link,
284-
new PriceFormatter(),
285-
new ProductColorsRetriever(),
286-
$this->context->getTranslator()
287-
);
288-
}
263+
if (empty($productIds)) {
264+
return false;
265+
}
266+
267+
$assembler = new ProductAssembler($this->context);
268+
269+
$presenterFactory = new ProductPresenterFactory($this->context);
270+
$presentationSettings = $presenterFactory->getPresentationSettings();
271+
if (version_compare(_PS_VERSION_, '1.7.5', '>=')) {
272+
$presenter = new \PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductListingPresenter(
273+
new ImageRetriever(
274+
$this->context->link
275+
),
276+
$this->context->link,
277+
new PriceFormatter(),
278+
new ProductColorsRetriever(),
279+
$this->context->getTranslator()
280+
);
281+
} else {
282+
$presenter = new \PrestaShop\PrestaShop\Core\Product\ProductListingPresenter(
283+
new ImageRetriever(
284+
$this->context->link
285+
),
286+
$this->context->link,
287+
new PriceFormatter(),
288+
new ProductColorsRetriever(),
289+
$this->context->getTranslator()
290+
);
291+
}
292+
293+
// Now, we can present the products for the template.
294+
$products_for_template = [];
289295

290-
$products_for_template = [];
291-
292-
if (is_array($productIds)) {
293-
foreach ($productIds as $productId) {
294-
if ($this->currentProductId !== $productId) {
295-
$products_for_template[] = $presenter->present(
296-
$presentationSettings,
297-
$assembler->assembleProduct(['id_product' => $productId]),
298-
$this->context->language
299-
);
300-
}
296+
if (is_array($productIds)) {
297+
// Prepare a standardized array with products
298+
$rawProducts = [];
299+
foreach ($productIds as $productId) {
300+
if ($this->currentProductId == $productId) {
301+
continue;
301302
}
303+
$rawProducts[] = ['id_product' => $productId];
302304
}
303305

304-
return $products_for_template;
306+
// Assemble & present in bulk or separately, depending on core version
307+
$assembleInBulk = method_exists($assembler, 'assembleProducts');
308+
if ($assembleInBulk) {
309+
$rawProducts = $assembler->assembleProducts($rawProducts);
310+
}
311+
foreach ($rawProducts as $rawProduct) {
312+
$products_for_template[] = $presenter->present(
313+
$presentationSettings,
314+
($assembleInBulk ? $rawProduct : $assembler->assembleProduct($rawProduct)),
315+
$this->context->language
316+
);
317+
}
305318
}
306319

307-
return false;
320+
return $products_for_template;
308321
}
309322

310323
/**

0 commit comments

Comments
 (0)