Search code examples
phpprestashopsmarty

Prestashop: Get parent category informations from the sub-category page


I build a site on prestashop with a child theme based on the classic theme. For my needs i've got a page for main categories with an hero section where i display the main category name, the cover image of the category, the sub-categories and a description. (see on the attached image)

hero section

Now i need to keep this section like that when i go to the sub-category page. Keep the title, image, sub-categories, etc. Just the content must change.

For now there is my code to display this hero section, located in the layout-left-column.tpl (to be before all the main content and after the header) :

{block name="hero_content"}
    {if Tools::getValue('id_category') == 11 || 19 || 20 || 21 || 22 || 23}
        <div class="category_hero" style="background-image: url('{$urls.img_url}categories/volailles_image_cover.jpg')">
            {if $listing.pagination.items_shown_from == 1}
                <div class="category_hero_title">
                    <h1 class="h1">{$category.name}</h1>
                </div>
                <div class="category_hero_subcategories">
                    {include file='catalog/_partials/subcategories.tpl'}
                </div>
                <div class="category_hero_description">
                    {$category.description nofilter}
                </div>
            {/if}
        </div>
    {/if}
{/block}

But as i expected when i go to a sub-category this section change with the informations of this sub-category.

How can i keep this section with the parent category informations ?

Ps: I tried to hit the function Category::getParentsCategories(), but nothing. And i'm working with Prestashop 1.7.8.3

Thank's for your time.


Solution

  • [UPDATE] Problem Solved

    I finaly solved my problème with this topic : Prestashop subcategory parent

    And with adaptations :

    In a custom module class where i create a function :

    public function hookDisplayHeaderCategory($params) {
            if (isset($params['current_category']) && !empty($params['current_category'])) {
                $id_lang = $this->context->language->id;
                $parent_category = new Category((int) $params['current_category']);
                $sub_categories = Category::getChildren((int) $params['current_category'], $id_lang);
                if (Validate::isLoadedObject($parent_category)) {
                    $this->context->smarty->assign(array(
                        "parent_category" => $parent_category,
                        "parent_category_name"   =>  $parent_category->name,
                        "parent_category_description"   => $parent_category->description,
                        "sub_categories"    =>  $sub_categories,
                    ));
                }
            }
    
            return $this->display(__FILE__, 'category_hero.tpl');
        }
    

    Create a view in this module with the hero section :

    <div class="category_hero" style="background-image: url('{$urls.img_url}categories/volailles_image_cover.jpg')">
            <div class="category_hero_title">
                <h1 class="h1">{$parent_category_name[1]}</h1>
            </div>
            <div class="category_hero_subcategories">
                {include file='catalog/_partials/subcategories.tpl' subcategories=$sub_categories}
            </div>
            <div class="category_hero_description">
                {$parent_category_description[1] nofilter}
            </div>
    </div>
    

    If it can help somebody in the same case.