diff --git a/ps_viewedproduct.php b/ps_viewedproduct.php index 92eaf40..2d29b5b 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->getExistingProductsIds($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,25 @@ 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 getExistingProductsIds(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' + FROM ' . _DB_PREFIX_ . 'product_shop p + WHERE p.active = 1 + AND p.id_product IN (' . implode(',', array_map('intval', $productIds)) . ')' . Shop::addSqlRestriction(false, 'p') ); return array_map(function ($entry) { return $entry['id_product']; - }, $existingProductsQuery); + }, $activeProductsQuery); } } 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.#'