Jump to content

Full-width module when no sub-categories - not working


Recommended Posts

Hi
Im trying to create a module, which will set the category page to Full-width if there is no sub-categories. I want to display all products with no left or right column, but only if there is no sub-categories.  

This is the code, but I cannot figure out wants not working

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}

class Autofullwidth extends Module
{
    public function __construct()
    {
        $this->name = 'autofullwidth';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'ExampleAuthor';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Auto Fullwidth - Disable Columns');
        $this->description = $this->l('Disables left and right columns for categories without subcategories.');
    }

    public function install()
    {
        return parent::install()
            && $this->registerHook('actionDispatcher');
    }

    public function uninstall()
    {
        return parent::uninstall();
    }

    /**
     * Hook called for every front office request.
     */
    public function hookActionDispatcher($params)
    {
        // 1) Log that the hook was fired
        PrestaShopLogger::addLog(
            '[autofullwidth] hookActionDispatcher fired, controller_class=' . ($params['controller_class'] ?? 'unknown'),
            1 // severity=1 (highest)
        );

        // 2) Only proceed if we're on the CategoryController
        if (($params['controller_class'] ?? '') !== 'CategoryController') {
            return;
        }

        // 3) Check if we actually have a controller object
        if (empty($params['controller'])) {
            PrestaShopLogger::addLog('[autofullwidth] No $params["controller"] found', 2);
            return;
        }

        /** @var CategoryControllerCore $controller */
        $controller = $params['controller'];
        $category = $controller->getCategory();

        if (!$category) {
            PrestaShopLogger::addLog('[autofullwidth] Could not retrieve the category object', 2);
            return;
        }

        // 4) Get subcategories
        $subcategories = $category->getSubCategories($this->context->language->id, true);
        $countSub = count($subcategories);

        PrestaShopLogger::addLog(
            '[autofullwidth] Category ' . (int)$category->id . ' has ' . $countSub . ' subcategories',
            1
        );

        // 5) If there are no subcategories -> disable columns
        if ($countSub === 0) {
            PrestaShopLogger::addLog(
                '[autofullwidth] No subcategories -> disabling left/right columns',
                1
            );

            // These methods are standard in modern PrestaShop. Some themes respect them, others do not.
            if (method_exists($controller, 'disableLeftColumn')) {
                $controller->disableLeftColumn();
            }
            if (method_exists($controller, 'disableRightColumn')) {
                $controller->disableRightColumn();
            }
        }
    }
}

 

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