From 78c24d0d24b3baa44265d91caba81beaf0b3d7c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Andrieu?= Date: Mon, 19 Nov 2018 11:15:58 +0100 Subject: [PATCH 1/2] On product deletion, the display should work as expected --- ps_viewedproduct.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/ps_viewedproduct.php b/ps_viewedproduct.php index 78607fa..eeac621 100644 --- a/ps_viewedproduct.php +++ b/ps_viewedproduct.php @@ -27,7 +27,7 @@ use PrestaShop\PrestaShop\Core\Module\WidgetInterface; use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever; use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter; -use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter; +use PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductListingPresenter; use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever; if (!defined('_PS_VERSION_')) { @@ -244,12 +244,17 @@ protected function addViewedProduct($idProduct) protected function getViewedProductIds() { - $arr = array_reverse(explode(',', $this->context->cookie->viewed)); - if (null !== $this->currentProductId && in_array($this->currentProductId, $arr)) { - $arr = array_diff($arr, array($this->currentProductId)); + $viewedProductsIds = array_reverse(explode(',', $this->context->cookie->viewed)); + if (null !== $this->currentProductId && in_array($this->currentProductId, $viewedProductsIds)) { + $viewedProductsIds = array_diff($viewedProductsIds, array($this->currentProductId)); } - return array_slice($arr, 0, (int) (Configuration::get('PRODUCTS_VIEWED_NBR'))); + $existingProducts = $this->getExistingProductsIds(); + $viewedProductsIds = array_filter($viewedProductsIds, function ($entry) use ($existingProducts) { + return in_array($entry, $existingProducts); + }); + + return array_slice($viewedProductsIds, 0, (int) (Configuration::get('PRODUCTS_VIEWED_NBR'))); } protected function getViewedProducts() @@ -275,7 +280,7 @@ protected function getViewedProducts() if (is_array($productIds)) { foreach ($productIds as $productId) { - if ($this->currentProductId != $productId) { + if ($this->currentProductId !== $productId) { $products_for_template[] = $presenter->present( $presentationSettings, $assembler->assembleProduct(array('id_product' => $productId)), @@ -290,4 +295,16 @@ protected function getViewedProducts() return false; } + + private function getExistingProductsIds() + { + $existingProductsQuery = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT p.id_product + FROM ' . _DB_PREFIX_ . 'product p' + ); + + return array_map(function ($entry) { + return $entry['id_product']; + }, $existingProductsQuery); + } } From 17f9c9bba44087ae9d123997e746e58658f7f921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Andrieu?= Date: Tue, 20 Nov 2018 18:04:21 +0100 Subject: [PATCH 2/2] Address comments --- ps_viewedproduct.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ps_viewedproduct.php b/ps_viewedproduct.php index eeac621..9d05c4d 100644 --- a/ps_viewedproduct.php +++ b/ps_viewedproduct.php @@ -296,11 +296,15 @@ protected function getViewedProducts() return false; } + /** + * @return array the list of active product ids. + */ private function getExistingProductsIds() { $existingProductsQuery = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT p.id_product - FROM ' . _DB_PREFIX_ . 'product p' + FROM ' . _DB_PREFIX_ . 'product p + WHERE p.active = 1' ); return array_map(function ($entry) {