Jump to content

Set last added product image as subcategory image


Recommended Posts

Hi guys, 

I'm trying to set last added product image as subcategory image, as we are importing a big list of products everyday and subcategories could change from day to day and in order to avoid subcategory images maintenance was thinking on this solution. 

Time ago this code used to work on PS 1.6,  but when i try to implement it in 1.7 doesn't work. 


class CategoryController extends CategoryControllerCore
	 * Assign sub categories templates vars AND get product image from subcategory
	protected function assignSubcategories()
		if ($subCategories = $this->category->getSubCategories($this->context->language->id))
				foreach($subCategories as &$subCategory ) {
					$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
					  select p.id_product, i.id_image, pl.link_rewrite
					from '._DB_PREFIX_.'category c1, '._DB_PREFIX_.'category c2
					inner join  '._DB_PREFIX_.'product p on (p.id_category_default = c2.id_category)
					inner join  '._DB_PREFIX_.'image i on (i.id_product = p.id_product and i.cover = 1)
					inner join  '._DB_PREFIX_.'product_lang pl on (p.id_product = pl.id_product)
					where c2.nleft >= c1.nleft and c2.nleft <= c1.nright
					and c1.id_category = '.(int)$subCategory['id_category']. '
					order by p.date_add desc limit 0,1');
				  if(isset($result[0] ) ) {
					$subCategory['productimage'] = $result[0]['id_product'] . '-'. $result[0]['id_image'];
					$subCategory['imglinkrewrite'] = $result[0]['link_rewrite'];
				'subcategories' => $subCategories,
				'subcategories_nb_total' => count($subCategories),
				'subcategories_nb_half' => ceil(count($subCategories) / 2)

I'm creating this in /public_html/override/controllers/front/CategoryController.php


and then in my template file:

{block name='product_list_subcategories'}
    {if isset($subcategories)}
        <!-- Subcategories -->
        <div class="product-list-subcategories {if $iqitTheme.cat_hide_mobile} hidden-sm-down{/if}">
            <div class="row">
                {foreach from=$subcategories item=subcategory}
                    <div class="col-{$iqitTheme.cat_sub_thumbs_p} col-md-{$iqitTheme.cat_sub_thumbs_t} col-lg-{$iqitTheme.cat_sub_thumbs_d}">
                        <div class="subcategory-image">
                            <a href="{$subcategory.url}">
							  <img src="{$link->getImageLink($subcategory.imglinkrewrite, $subcategory.productimage, 'medium_default')}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" />
                        <a class="subcategory-name" href="{$subcategory.url}">{$subcategory.name}</a>


But i'm getting an error, 

(1/1) ContextErrorException

Notice: Undefined index: imglinkrewrite

in a2d558d45173455799931201a8c8b5fec31613fb_2.file.category-subcategories.tpl.php line 62


I just want to know if i'm in the right way to do this?  Also does anyone know if there is a module that already do this ?


Kind Regards,

Link to comment
Share on other sites

  • 2 weeks later...

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...