Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On product deletion, the display should work as expected #10

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 27 additions & 6 deletions ps_viewedproduct.php
Expand Up @@ -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_')) {
Expand Down Expand Up @@ -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()
Expand All @@ -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)),
Expand All @@ -290,4 +295,20 @@ protected function getViewedProducts()

return false;
}

/**
* @return array the list of active product ids.
*/
private function getExistingProductsIds()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc block? :trollface:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, as no other functions have PHPDoc block xD

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be a boy scout!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will make the pull request unreadable!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do it bro!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the PHPDoc can be added in an 2nd PR ? So we have PR 1 that modifies code behavior, no refactoring ; and PR 2 that only adds harmless doc

{
$existingProductsQuery = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT p.id_product
FROM ' . _DB_PREFIX_ . 'product p
WHERE p.active = 1'
);

return array_map(function ($entry) {
return $entry['id_product'];
}, $existingProductsQuery);
}
}