Jump to content

How to control order of SubCategories


Recommended Posts

in this case everything depends on module that you use.

it's necessary to analyse the code of this addon first

check what method it uses in hookLeftColumn function

 

in default module you can find function

			$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
			SELECT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
			FROM `'._DB_PREFIX_.'category` c
			INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->context->language->id.Shop::addSqlRestrictionOnLang('cl').')
			INNER JOIN `'._DB_PREFIX_.'category_shop` cs ON (cs.`id_category` = c.`id_category` AND cs.`id_shop` = '.(int)$this->context->shop->id.')
			WHERE (c.`active` = 1 OR c.`id_category` = '.(int)Configuration::get('PS_HOME_CATEGORY').')
			AND c.`id_category` != '.(int)Configuration::get('PS_ROOT_CATEGORY').'
			'.((int)$maxdepth != 0 ? ' AND `level_depth` <= '.(int)$maxdepth : '').'
			'.$range.'
			AND c.id_category IN (
				SELECT id_category
				FROM `'._DB_PREFIX_.'category_group`
				WHERE `id_group` IN ('.pSQL(implode(', ', Customer::getGroupsStatic((int)$this->context->customer->id))).')
			)
			ORDER BY `level_depth` ASC, '.(Configuration::get('BLOCK_CATEG_SORT') ? 'cl.`name`' : 'cs.`position`').' '.(Configuration::get('BLOCK_CATEG_SORT_WAY') ? 'DESC' : 'ASC'));

it's neccessary to change ORDER BY clause

Link to comment
Share on other sites

Thanks Vekia.

This is my current code: I guess I do not see what change I need to make to make the display order of sub-categories the same order as both the back office and the navigation menu at the top. (BTW, the top navigation sub-categories matches the order of sub categories in the back office - only the display order of subcategories is shown in random order)

public function getSubCategories($id_lang, $active = true)
	{
	 	if (!Validate::isBool($active))
	 		die(Tools::displayError());

		$groups = FrontController::getCurrentCustomerGroups();
		$sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');

		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
			SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description
			FROM `'._DB_PREFIX_.'category` c
			'.Shop::addSqlAssociation('category', 'c').'
			LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
				ON (c.`id_category` = cl.`id_category`
				AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
			LEFT JOIN `'._DB_PREFIX_.'category_group` cg
				ON (cg.`id_category` = c.`id_category`)
			WHERE `id_parent` = '.(int)$this->id.'
				'.($active ? 'AND `active` = 1' : '').'
				AND cg.`id_group` '.$sql_groups.'
			GROUP BY c.`id_category`
			ORDER BY `level_depth` ASC, category_shop.`position` ASC
		');
Link to comment
Share on other sites

Sure - here is category.tpl

{include file="$tpl_dir./breadcrumb.tpl"}

{include file="$tpl_dir./errors.tpl"}

<div class="main row">

    <!-- right content start  -->

    <div class="col-main span9">

        <!-- cart baner  -->

         <div class="category-description std">

            <div class="row">

{$sccop.trendy_category_page_markup|html_entity_decode}

            </div>    

         </div>

        <!-- end the cart baner -->

        {if isset($category)}

            {if $category->id AND $category->active}

             <!--   <div class="resumecat category-product-count">

                    {include file="$tpl_dir./category-count.tpl"}

                </div> -->


                {if $scenes || $category->description || $category->id_image}

<div class="content_scene_cat">

                        {if $scenes}

                            <!-- Scenes -->

                            {include file="$tpl_dir./scenes.tpl" scenes=$scenes}

                        {else}

                            <!-- Category image -->

                            {if $category->id_image}

                                <div class="align_center">

                                    <img src="{$link->getCatImageLink($category->link_rewrite, $category->id_image, 'category_default')}" alt="{$category->name|escape:'htmlall':'UTF-8'}" title="{$category->name|escape:'htmlall':'UTF-8'}" id="categoryImage" width="{$categorySize.width}" height="{$categorySize.height}" />

                                </div>

                            {/if}

                        {/if}
{if $category->description}
<p></p>
<div class="cat_desc"><!-- swap p with div to allow tinymce -->
{if strlen($category->description) > 800}
<div id="category_description_short">{$category->description|truncate:800}</div><!-- swap p with div to allow tinymce -->
<div id="category_description_full" style="display:none">{$category->description}</div><!-- swap p with div to allow tinymce -->
<a href="#" onclick="$('#category_description_short').hide(); $('#category_description_full').show(); $(this).hide(); return false;" class="lnk_more">{l s='More'}</a>
{else}
<div>{$category->description}</div><!-- swap p with div to allow tinymce -->
{/if}
</div>
{/if}
 
                    </div>

                {/if}

                {if isset($subcategories)}

<!-- Subcategories -->
<div id="subcategories">
<h3>{l s='Subcategories'}</h3>
<ul class="inline_list">
	{foreach from=$subcategories item=subcategory}
		<li class="clearfix">
			<a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" title="{$subcategory.name|escape:'htmlall':'UTF-8'}" class="img">

                                        {if $subcategory.id_image}

                                            <img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')}" alt="{$subcategory.name|escape:'htmlall':'UTF-8'}" width="{$mediumSize.width}" height="{$mediumSize.height}" />

                                        {else}

                                            <img src="{$img_cat_dir}default-medium_default.jpg" alt="{$subcategory.name|escape:'htmlall':'UTF-8'}" width="{$mediumSize.width}" height="{$mediumSize.height}" />

                                        {/if}

                                    </a>
									<!--add h2 for SEO-->
                                    <h2><a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" class="cat_name">{$subcategory.name|escape:'htmlall':'UTF-8'}</a></h2>

                                   <!-- {if $subcategory.description}
                                        <p class="cat_desc">{$subcategory.description}</p>
                                    {/if}-->

{if $subcategory.short_description}
						<p class="cat_desc">{$subcategory.short_description}</p><!-- swap p with div to allow tinymce -->
					{elseif $subcategory.description}
						<p class="cat_desc">{$subcategory.description}</p>
					{/if}

                                </li>

                            {/foreach}

                        </ul>

                        <br class="clear"/>

                    </div>

                {/if}


                <div class="page-title category-title">

                    {if $products} 

                        <a href="{$link->getPageLink('products-comparison')}" title="" class="compaate_button">{l s='Compare'}</a>

                    {/if}

                    <h2> {*change from h1 *}

                        {strip}

                            {$category->name|escape:'htmlall':'UTF-8'}

                            {if isset($categoryNameComplement)}

                                {$categoryNameComplement|escape:'htmlall':'UTF-8'}

                            {/if}

                        {/strip}

                    </h2>

  
                </div>

                {if $products}


                    <div class="toolbar">

                        <div class="tb">

                            <div class="sorter">

                                <p class="view-mode">

                                    <strong title="Grid" class="grid grid-active"> </strong>

                                    <a href="#" title="List" class="list"> </a>

                                </p>


                                {include file="./product-sort.tpl"}

                            </div>

                            <div class="pager hidden-phone">

                                {include file="./nbr-product-page.tpl"}

                            </div>

                        </div>

                    </div>


                    {include file="./product-list.tpl" products=$products}


                    <div class="toolbar-bottom">

                        <div class="toolbar">

                            <div class="tb">

                                <div class="sorter">

                                    <p class="view-mode">

                                        <strong title="Grid" class="grid grid-active"> </strong>

                                        <a href="#" title="List" class="list"> </a>

                                    </p>



                                    {include file="./product-sort.tpl"} 

                                </div>

                                <div class="pager hidden-phone">

                                    {include file="./nbr-product-page.tpl"}

                                </div>

                            </div>

                        </div>

                    </div>

                    {include file="./pagination.tpl"}	

                {/if}

            {elseif $category->id}

                <p class="warning">{l s='This category is currently unavailable.'}</p>

            {/if}

        {/if}


        <!-- end the span9 -->

    </div>

    <!-- left content  end  -->


    <!-- start col-left sidebar  span3  -->

    <div class="col-left sidebar span3">

        {$HOOK_LEFT_COLUMN}

    </div>
    <!-- end the left sidebar  -->
    <!-- end the main row  -->

</div>
Link to comment
Share on other sites

you can try to change

category_shop.`position` ASC

to

c.`position` ASC

but in fact, category positions are stored in ps_category_shop table, this is why it's weird that it doesnt work for you.

prestashop stores information about position also in ps_category table, so this is why i suggested to use c.position

maybe this is the key to your problem

Link to comment
Share on other sites

you can try to change

category_shop.`position` ASC

to

c.`position` ASC

but in fact, category positions are stored in ps_category_shop table, this is why it's weird that it doesnt work for you.

prestashop stores information about position also in ps_category table, so this is why i suggested to use c.position

maybe this is the key to your problem

 

I changed all three instances in category.php to what you suggested but nothing changed (and nothing broke so this is good too)

Link to comment
Share on other sites

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