Skip to content

Instantly share code, notes, and snippets.

@Casper-O
Created March 19, 2018 08:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Casper-O/e7e46f3b708aa03faa2c57fa429f357a to your computer and use it in GitHub Desktop.
Save Casper-O/e7e46f3b708aa03faa2c57fa429f357a to your computer and use it in GitHub Desktop.
Prestashop 1.6.X - Fix attribute color list doesn't show on product list if turning on smarty cache
<?php
class FrontController extends FrontControllerCore
{
public function addColorsToProductList(&$products)
{
if (!is_array($products) || !count($products) || !file_exists(_PS_THEME_DIR_.'product-list-colors.tpl')) {
return;
}
$products_need_cache = array();
foreach ($products as &$product) {
if (!$this->isCached(_PS_THEME_DIR_.'product-list-colors.tpl', $this->getColorsListCacheId($product['id_product']))) {
$products_need_cache[] = (int)$product['id_product'];
}else{
$product['color_list'] = $this->context->smarty->createTemplate(_PS_THEME_DIR_.'product-list-colors.tpl', $this->getColorsListCacheId($product['id_product']))->fetch();
}
}
unset($product);
$colors = false;
if (count($products_need_cache)) {
$colors = Product::getAttributesColorList($products_need_cache);
Tools::enableCache();
foreach ($products as &$product) {
if (in_array($product['id_product'], $products_need_cache)){
$tpl = $this->context->smarty->createTemplate(_PS_THEME_DIR_.'product-list-colors.tpl', $this->getColorsListCacheId($product['id_product']));
if (isset($colors[$product['id_product']])) {
$tpl->assign(array(
'id_product' => $product['id_product'],
'colors_list' => $colors[$product['id_product']],
'link' => Context::getContext()->link,
'img_col_dir' => _THEME_COL_DIR_,
'col_img_dir' => _PS_COL_IMG_DIR_
));
$product['color_list'] = $tpl->fetch(_PS_THEME_DIR_.'product-list-colors.tpl', $this->getColorsListCacheId($product['id_product']));
} else {
$product['color_list'] = '';
}
}
}
Tools::restoreCacheSettings();
}
}
}
@Casper-O
Copy link
Author

Casper-O commented Mar 19, 2018

how yo use:

  1. Download the gist and make sure the filename is FrontController.php and place it in overrides/classes/controller/
  2. Delete class_index.php in /cache/
  3. Clear store cache

Original Author: iTiaNex
Source: PrestaShop/PrestaShop#7876 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment