Jump to content
thirty bees forum
  • 0

Colors are not displayed on the categories page


Question

Posted

There is a bug with displaying colors on the categories page when SmartyCache is enabled.
It is not fixed in TB and PS (1.6.x).
Here is an example of how to fix it.

    public function addColorsToProductList(&$products)
    {
        if (!is_array($products) || !count($products) || !file_exists(_PS_THEME_DIR_.'product-list-colors.tpl')) {
            return;
        }

        $productsNeedCache = [];
        foreach ($products as &$product) {
            if (!$this->isCached(_PS_THEME_DIR_.'product-list-colors.tpl', $this->getColorsListCacheId($product['id_product']))) {
                $productsNeedCache[] = (int) $product['id_product'];
            } else {
                $product['color_list'] = $this->context->smarty->fetch(_PS_THEME_DIR_.'product-list-colors.tpl', $this->getColorsListCacheId($product['id_product']));
            }
        }

        unset($product);

        $colors = false;
        if (count($productsNeedCache)) {
            $colors = Product::getAttributesColorList($productsNeedCache);
        } else {
            return;
        }

        Tools::enableCache();
        foreach ($products as &$product) {
            if (isset($colors[$product['id_product']])) {
                $tpl = $this->context->smarty->createTemplate(_PS_THEME_DIR_.'product-list-colors.tpl', $this->getColorsListCacheId($product['id_product']));
                $tpl->assign(
                    [
                        'id_product'  => $product['id_product'],
                        'colors_list' => $colors[$product['id_product']],
                        'link'        => $this->context->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']));
            } 
            if (in_array($product['id_product'], $productsNeedCache) && !isset($colors[$product['id_product']])) {
                $product['color_list'] = '';
            }
        }
        Tools::restoreCacheSettings();
    }

 

  • Like 1

3 answers to this question

Recommended Posts

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...