Jump to content
Chetanilead

Show all categories in a CMS Page?

Recommended Posts

Hello All,

 

I want to display all the categories added from the admin section on a front CMS page.

 

Can anyone help me ? Its urgent.

 

Thanks in advance.

Share this post


Link to post
Share on other sites

I believe the only way to do it is by editing the cms controller, and if the cms page id matches the one you need, you can use Category::getCategories($id_lang) to get all of them. Assign to the tpl, and display inside cms.tpl is that smarty variable has been assigned

Share this post


Link to post
Share on other sites

Hi nemo1,

 

Thanks for the quick reply :) . I am new to prestashop. Could you please clarify more?

The Changes which U have suggested my affect the core files of prestashop right?

If yes then is there any other way to implement it without modifying core scripts.

 

Thanks

Share this post


Link to post
Share on other sites

Well you can use override, it's a bit complex to explain in a forum topic, if you don't feel confident you may hire someone to do it for you from the paid jobs section :)

Share this post


Link to post
Share on other sites

If you can provide me some sample knowledge I am capable in doing that I have knowledge of other PHP frameworks and CMS. But I am new in prestashop

Share this post


Link to post
Share on other sites

getCategories() function is static function so in .tpl file you can use foreach loop on Category::getCategories()

 

 

something like:

{foreach from=Category::getCategories() item=category}
{$category.name}<br/>
{/foreach}

Share this post


Link to post
Share on other sites

Sure! 

 

check out this tut on overrides:

http://doc.prestashop.com/display/PS15/Overriding+default+behaviors#Overridingdefaultbehaviors-Overridingacontroller

 

You have to override the CMSController.php file, in override/controllers/front

 

In the initContent method, check if Tools::getValue('id_cms') equals the id of the cms page you want the list to appear. If it does use

 

Category::getCategories($id_lang) 

 

To get categories, then $this->context->smarty->assign('thevariablenameinsmarty',$categories_variable_name);

 

Finally, inside cms.tpl (template folder) if $thevariablenameinsmarty is assigned, display categories

Share this post


Link to post
Share on other sites

I have studies and created a sample code in my_module\override\controllers\front\CmsController.php

 

In which I have written following code

 

<?php

class MycmsController extends CmsControllerCore{
    public function init()
    {
        $id_cms = (int)Tools::getValue('id_cms');
                echo $id_cms;
                die;
    }

 

  public function initContent()
    {
        $id_cms = (int)Tools::getValue('id_cms');
                echo $id_cms;
                die;
    }
    
}

 

I have also deleted the class_index.php  file from cache folder but still not getting cms page id and it not event die(stops) the execution.

 

Help me please

Edited by Chetanilead (see edit history)

Share this post


Link to post
Share on other sites

it has to be called cmsController, without "my"

Share this post


Link to post
Share on other sites

Thanks for the reply but it still not working for me :(

 

<?php
class cmsController extends CmsControllerCore{
    public function init()
    {
        $id_cms = (int)Tools::getValue('id_cms');
                echo $id_cms;
                die;
    }

 

  public function initContent()
    {
        $id_cms = (int)Tools::getValue('id_cms');
                echo $id_cms;
                die;
    }
    
}

Share this post


Link to post
Share on other sites

Can you check if the override has been moved to main the override/controllers/front folder after installing the module?

Share this post


Link to post
Share on other sites

Try reinstalling the module. If it doesn't move yet, move it manually and clear the class index

Share this post


Link to post
Share on other sites

Hi Nemo thanks for your reply it worked for me but I forgot to thank you.

 

I have a new query in this How can I load a tpl or php file when my CMS page is called.

 

Thanks in advance

Share this post


Link to post
Share on other sites

Hi

 

I have used  $categories = Category::getCategories(); to get all categories it is working fine for me but I didn't get category image on the tpl file can you please let me know how can I get that image. I am also not getting $category.id_image parameter to get image

 

I got this array for each category :

 

[infos] => Array
(
[id_category] => 13
[id_parent] => 2
[id_shop_default] => 1
[level_depth] => 2
[nleft] => 3
[nright] => 4
[active] => 1
[date_add] => 2014-05-23 01:58:27
[date_upd] => 2014-05-28 02:30:24
[position] => 1
[is_root_category] => 0
[id_shop] => 1
[id_lang] => 1
[name] => Appearance Protection Packages
[description] =>
[link_rewrite] => appearance-protection-packages
[meta_title] =>
[meta_keywords] =>
[meta_description] =>
)

 

How can I get image for this category please help.

Share this post


Link to post
Share on other sites

Like this:

 

{$link->getCatImageLink($category->link_rewrite, $category->id_image, 'category')}

Share this post


Link to post
Share on other sites

 

getCategories() function is static function so in .tpl file you can use foreach loop on Category::getCategories()

 

 

something like:

{foreach from=Category::getCategories() item=category}
{$category.name}<br/>
{/foreach}

 

in this way the only thing to override is the .tpl and not the cmsController.php?

 

Share this post


Link to post
Share on other sites

Correct, but I would not use logic in templates like that honestly

  • Like 1

Share this post


Link to post
Share on other sites

hello ALL

i want disppaly all parent category in cms pge and i did all step before but i cant see an result 

can you help me ?

Share this post


Link to post
Share on other sites
On 7/14/2017 at 11:23 AM, mhdcasa said:

hello ALL

i want disppaly all parent category in cms pge and i did all step before but i cant see an result 

can you help me ?

Hello did you solve that? I know it's been a long time ago but, I'm trying to do the same now in prestashop 1.7.5.1.

@Chetanilead would it be possible to get the module you developed?

Thanks.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More