diff --git a/classes/Product.php b/classes/Product.php index a5eb361ec..e1668a59c 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -3045,6 +3045,35 @@ class ProductCore extends ObjectModel return true; } + public static function getAttributesColorList(Array $products, $have_stock = true) + { + if (!count($products)) + return array(); + + if (!$res = Db::getInstance()->executeS(' + SELECT pa.id_product, a.color, a.id_attribute, pac.id_product_attribute + FROM '._DB_PREFIX_.'product_attribute pa + '.Shop::addSqlAssociation('product_attribute', 'pa').' + JOIN '._DB_PREFIX_.'product_attribute_combination pac ON (pac.id_product_attribute = product_attribute_shop.id_product_attribute) + JOIN '._DB_PREFIX_.'attribute a ON (a.id_attribute = pac.id_attribute) + JOIN '._DB_PREFIX_.'attribute_group ag ON (ag.id_attribute_group = ag.id_attribute_group) + WHERE pa.id_product IN ('.implode(array_map('intval', $products), ',').') AND ag.is_color_group = 1 + GROUP BY pa.id_product, color + ')) + return false; + + $colors = array(); + foreach ($res as $row) + { + if (Tools::isEmpty($row['color'])) + continue; + + $colors[(int)$row['id_product']][] = array('id_product_attribute' => (int)$row['id_product_attribute'], 'color' => $row['color']); + } + + return $colors; + } + /** * Get all available attribute groups * diff --git a/classes/controller/Controller.php b/classes/controller/Controller.php index 27ebaf040..cf18b2076 100644 --- a/classes/controller/Controller.php +++ b/classes/controller/Controller.php @@ -356,4 +356,9 @@ abstract class ControllerCore $this->context->cookie->write(); $this->context->smarty->display($content); } + + protected function isCached($template, $cacheId = null, $compileId = null) + { + return $this->context->smarty->isCached($template, $cacheId, $compileId); + } } diff --git a/controllers/front/CategoryController.php b/controllers/front/CategoryController.php index d027fafcf..9957da6e9 100644 --- a/controllers/front/CategoryController.php +++ b/controllers/front/CategoryController.php @@ -199,10 +199,38 @@ class CategoryControllerCore extends FrontController // Pagination must be call after "getProducts" $this->pagination($this->nbProducts); + $products_need_cache = array(); foreach ($this->cat_products as &$product) { if ($product['id_product_attribute'] && isset($product['product_attribute_minimal_quantity'])) $product['minimal_quantity'] = $product['product_attribute_minimal_quantity']; + if (!$this->isCached(_PS_THEME_DIR_.'product-list-colors.tpl', 'productlist_colors|'.$product['id_product'])) + $products_need_cache[] = (int)$product['id_product']; + } + unset($product); + + $colors = array(); + if (count($products_need_cache)) + $colors = Product::getAttributesColorList($products_need_cache); + + if (count($colors)) + { + Tools::enableCache(); + + foreach ($this->cat_products as &$product) + if (isset($colors[$product['id_product']]) && count($colors[$product['id_product']])) + { + $data = $this->context->smarty->createData(); + $data->assign(array( + 'id_product' => $product['id_product'], + 'colors_list' => $colors[$product['id_product']] + )); + $product['color_list'] = $this->context->smarty->createTemplate(_PS_THEME_DIR_.'product-list-colors.tpl', $data)->fetch(_PS_THEME_DIR_.'product-list-colors.tpl', 'productlist_colors|'.(int)$product['id_product']); + } + else + $product['color_list'] = ''; + + Tools::restoreCacheSettings(); } $this->context->smarty->assign('nb_products', $this->nbProducts); diff --git a/themes/default/css/product_list.css b/themes/default/css/product_list.css index d1d52bce1..d0f8d83bf 100644 --- a/themes/default/css/product_list.css +++ b/themes/default/css/product_list.css @@ -146,4 +146,22 @@ ul#product_list { background:url(../img/arrow_right_1.png) no-repeat 100% 4px transparent } #product_list li .lnk_view:hover {text-decoration:underline} - + + #color_to_pick_list { + list-style-type:none; + width:125px; + float:left; + } + + #color_to_pick_list li { + float:left; + clear:none; + padding:0; + margin-bottom:1px; + } + + #color_to_pick_list li a { + display:block; + height:20px; + width:20px; + } diff --git a/themes/default/product-list.tpl b/themes/default/product-list.tpl index 1d5a38a59..08c6152e3 100644 --- a/themes/default/product-list.tpl +++ b/themes/default/product-list.tpl @@ -43,6 +43,7 @@
{$product.description_short|strip_tags:'UTF-8'|truncate:360:'...'}
+ {$product.color_list}