From 58840d44227f19d3ad59a5ec3af87616a0642eac Mon Sep 17 00:00:00 2001 From: Prestaplugins Date: Mon, 26 Jan 2026 17:13:35 +0100 Subject: [PATCH 1/3] Check active flag on viewed products only --- ps_viewedproduct.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ps_viewedproduct.php b/ps_viewedproduct.php index 92eaf40..c003d91 100644 --- a/ps_viewedproduct.php +++ b/ps_viewedproduct.php @@ -248,9 +248,9 @@ protected function getViewedProductIds() $viewedProductsIds = array_diff($viewedProductsIds, [$this->currentProductId]); } - $existingProducts = $this->getExistingProductsIds(); - $viewedProductsIds = array_filter($viewedProductsIds, function ($entry) use ($existingProducts) { - return in_array($entry, $existingProducts); + $activeProducts = $this->getActiveProductsIds($viewedProductsIds); + $viewedProductsIds = array_filter($viewedProductsIds, function ($entry) use ($activeProducts) { + return in_array($entry, $activeProducts); }); return array_slice($viewedProductsIds, 0, (int) (Configuration::get('PRODUCTS_VIEWED_NBR'))); @@ -321,18 +321,24 @@ protected function getViewedProducts() } /** - * @return array the list of active product ids + * @param array $productIds List of product IDs to check + * @return array the list of active product ids among those provided */ - private function getExistingProductsIds() + private function getActiveProductsIds(array $productIds = []) { - $existingProductsQuery = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS(' + if (empty($productIds)) { + return []; + } + + $activeProductsQuery = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS(' SELECT p.id_product FROM ' . _DB_PREFIX_ . 'product p - WHERE p.active = 1' + WHERE p.active = 1 + AND p.id_product IN (' . implode(',', array_map('intval', $productIds)) . ')' ); return array_map(function ($entry) { return $entry['id_product']; - }, $existingProductsQuery); + }, $activeProductsQuery); } } From 026f01628e8e6fdfed26676f05051d32f17d1b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Viguier?= <16720275+jf-viguier@users.noreply.github.com> Date: Mon, 26 Jan 2026 18:48:42 +0100 Subject: [PATCH 2/3] fix PHPDoc for cs fixer --- ps_viewedproduct.php | 1 + 1 file changed, 1 insertion(+) diff --git a/ps_viewedproduct.php b/ps_viewedproduct.php index c003d91..983c0f1 100644 --- a/ps_viewedproduct.php +++ b/ps_viewedproduct.php @@ -322,6 +322,7 @@ protected function getViewedProducts() /** * @param array $productIds List of product IDs to check + * * @return array the list of active product ids among those provided */ private function getActiveProductsIds(array $productIds = []) From 536c2677c40cefff03212a303076d84f6c8c55ac Mon Sep 17 00:00:00 2001 From: Prestaplugins Date: Mon, 26 Jan 2026 19:10:17 +0100 Subject: [PATCH 3/3] Update ps_viewedproduct.php The SQL query must use "product_shop" table, not "product" Fix Test Fiix PHPstan before PS 8.0.0 Keep the old name of the method --- ps_viewedproduct.php | 8 ++++---- tests/phpstan/phpstan-1.7.1.2.neon | 1 + tests/phpstan/phpstan-1.7.2.5.neon | 1 + tests/phpstan/phpstan-1.7.3.4.neon | 1 + tests/phpstan/phpstan-1.7.4.4.neon | 1 + tests/phpstan/phpstan-1.7.5.1.neon | 1 + tests/phpstan/phpstan-1.7.6.neon | 1 + tests/phpstan/phpstan-1.7.7.neon | 1 + tests/phpstan/phpstan-1.7.8.neon | 1 + 9 files changed, 12 insertions(+), 4 deletions(-) diff --git a/ps_viewedproduct.php b/ps_viewedproduct.php index 983c0f1..2d29b5b 100644 --- a/ps_viewedproduct.php +++ b/ps_viewedproduct.php @@ -248,7 +248,7 @@ protected function getViewedProductIds() $viewedProductsIds = array_diff($viewedProductsIds, [$this->currentProductId]); } - $activeProducts = $this->getActiveProductsIds($viewedProductsIds); + $activeProducts = $this->getExistingProductsIds($viewedProductsIds); $viewedProductsIds = array_filter($viewedProductsIds, function ($entry) use ($activeProducts) { return in_array($entry, $activeProducts); }); @@ -325,7 +325,7 @@ protected function getViewedProducts() * * @return array the list of active product ids among those provided */ - private function getActiveProductsIds(array $productIds = []) + private function getExistingProductsIds(array $productIds = []) { if (empty($productIds)) { return []; @@ -333,9 +333,9 @@ private function getActiveProductsIds(array $productIds = []) $activeProductsQuery = Db::getInstance((bool) _PS_USE_SQL_SLAVE_)->executeS(' SELECT p.id_product - FROM ' . _DB_PREFIX_ . 'product p + FROM ' . _DB_PREFIX_ . 'product_shop p WHERE p.active = 1 - AND p.id_product IN (' . implode(',', array_map('intval', $productIds)) . ')' + AND p.id_product IN (' . implode(',', array_map('intval', $productIds)) . ')' . Shop::addSqlRestriction(false, 'p') ); return array_map(function ($entry) { diff --git a/tests/phpstan/phpstan-1.7.1.2.neon b/tests/phpstan/phpstan-1.7.1.2.neon index 2e864e6..cfc1910 100644 --- a/tests/phpstan/phpstan-1.7.1.2.neon +++ b/tests/phpstan/phpstan-1.7.1.2.neon @@ -7,3 +7,4 @@ parameters: - '#Call to method assign\(\) on an unknown class Smarty_Data#' - '#Call to method present\(\) on an unknown class PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductListingPresenter.#' - '#Instantiated class PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductListingPresenter not found.#' + - '#Parameter \#1 \$share of static method ShopCore::addSqlRestriction\(\) expects int, false given.#' diff --git a/tests/phpstan/phpstan-1.7.2.5.neon b/tests/phpstan/phpstan-1.7.2.5.neon index 2e864e6..cfc1910 100644 --- a/tests/phpstan/phpstan-1.7.2.5.neon +++ b/tests/phpstan/phpstan-1.7.2.5.neon @@ -7,3 +7,4 @@ parameters: - '#Call to method assign\(\) on an unknown class Smarty_Data#' - '#Call to method present\(\) on an unknown class PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductListingPresenter.#' - '#Instantiated class PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductListingPresenter not found.#' + - '#Parameter \#1 \$share of static method ShopCore::addSqlRestriction\(\) expects int, false given.#' diff --git a/tests/phpstan/phpstan-1.7.3.4.neon b/tests/phpstan/phpstan-1.7.3.4.neon index 2e864e6..cfc1910 100644 --- a/tests/phpstan/phpstan-1.7.3.4.neon +++ b/tests/phpstan/phpstan-1.7.3.4.neon @@ -7,3 +7,4 @@ parameters: - '#Call to method assign\(\) on an unknown class Smarty_Data#' - '#Call to method present\(\) on an unknown class PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductListingPresenter.#' - '#Instantiated class PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductListingPresenter not found.#' + - '#Parameter \#1 \$share of static method ShopCore::addSqlRestriction\(\) expects int, false given.#' diff --git a/tests/phpstan/phpstan-1.7.4.4.neon b/tests/phpstan/phpstan-1.7.4.4.neon index 2f0d773..6a636c1 100644 --- a/tests/phpstan/phpstan-1.7.4.4.neon +++ b/tests/phpstan/phpstan-1.7.4.4.neon @@ -6,3 +6,4 @@ parameters: - '#Access to an undefined property Cookie::\$viewed.#' - '#Call to method present\(\) on an unknown class PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductListingPresenter.#' - '#Instantiated class PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductListingPresenter not found.#' + - '#Parameter \#1 \$share of static method ShopCore::addSqlRestriction\(\) expects int, false given.#' diff --git a/tests/phpstan/phpstan-1.7.5.1.neon b/tests/phpstan/phpstan-1.7.5.1.neon index f63cb8f..6a7f16b 100644 --- a/tests/phpstan/phpstan-1.7.5.1.neon +++ b/tests/phpstan/phpstan-1.7.5.1.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#Access to an undefined property Cookie::\$viewed.#' + - '#Parameter \#1 \$share of static method ShopCore::addSqlRestriction\(\) expects int, false given.#' diff --git a/tests/phpstan/phpstan-1.7.6.neon b/tests/phpstan/phpstan-1.7.6.neon index f63cb8f..6a7f16b 100644 --- a/tests/phpstan/phpstan-1.7.6.neon +++ b/tests/phpstan/phpstan-1.7.6.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#Access to an undefined property Cookie::\$viewed.#' + - '#Parameter \#1 \$share of static method ShopCore::addSqlRestriction\(\) expects int, false given.#' diff --git a/tests/phpstan/phpstan-1.7.7.neon b/tests/phpstan/phpstan-1.7.7.neon index f63cb8f..6a7f16b 100644 --- a/tests/phpstan/phpstan-1.7.7.neon +++ b/tests/phpstan/phpstan-1.7.7.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#Access to an undefined property Cookie::\$viewed.#' + - '#Parameter \#1 \$share of static method ShopCore::addSqlRestriction\(\) expects int, false given.#' diff --git a/tests/phpstan/phpstan-1.7.8.neon b/tests/phpstan/phpstan-1.7.8.neon index f63cb8f..6a7f16b 100644 --- a/tests/phpstan/phpstan-1.7.8.neon +++ b/tests/phpstan/phpstan-1.7.8.neon @@ -4,3 +4,4 @@ includes: parameters: ignoreErrors: - '#Access to an undefined property Cookie::\$viewed.#' + - '#Parameter \#1 \$share of static method ShopCore::addSqlRestriction\(\) expects int, false given.#'