Jump to content

Modul for Brand images


Recommended Posts

I'm trying to create my first Prestashop module, but I can't get it to work.
I want to display the manufacturer's logos on the manufacturer's page.
To do this, I've added the image_url column to the *_manufacturer table.

I've created the following files so that the standard theme is overwritten by my module.
I'm calling my module "ameinmodul" so that it is displayed as the first module in the list in the back office.
To do this, I've created the following files:

ameinmodul.php
/classes/ManufacturerHelper.php
/views/templates/hook/brand_miniature_item.tpl
/override/classes/Manufacturer.php

The files contain the following content:
ameinmodul.php
 

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

// Externe Klasse für die Bildverarbeitung einbinden
require_once(_PS_MODULE_DIR_ . 'ameinmodul/classes/ManufacturerHelper.php');

class AmeinModul extends Module
{
    public function __construct()
    {
        $this->name = 'ameinmodul';
        $this->tab = 'administration';
        $this->version = '1.0.10';
        $this->author = 'Mein Name';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('1 Mein Modul');
        $this->description = $this->l('Verschiedene Funktionen: Hersteller Logos');
    }

    public function install()
    {
        return parent::install()
            && $this->registerHook('displayBrandMiniatureItem') // **Hook explizit registrieren**
            && $this->registerHook('displayManufacturerLogo') // Herstellerlogo laden
    }



    public function hookDisplayManufacturerLogo($params)
    {
        error_log("HOOK WIRD AUFGERUFEN!", 3, _PS_ROOT_DIR_ . "/var/logs/hook_debug.log");

        if (!isset($params['manufacturer']) || !isset($params['manufacturer']['id_manufacturer'])) {
            return '<p style="color:red;">Fehlende Hersteller-Daten</p>';
        }

        $manufacturer = $params['manufacturer'];
        $id_manufacturer = (int)$manufacturer['id_manufacturer'];
        $image_url = trim($manufacturer['image_url']);

        // Falls `image_url` leer ist, Standard-Pfad verwenden
        if (empty($image_url)) {
            $image_url = _PS_BASE_URL_ . "/img/m/" . $id_manufacturer . ".jpg";
        } else {
            $image_url = _PS_BASE_URL_ . "/img/m/" . $image_url;
        }

        // Prüfen, ob die Datei existiert
        if (!file_exists(_PS_ROOT_DIR_ . "/img/m/" . basename($image_url))) {
            return '<p style="color:red;">Kein Logo für ' . htmlspecialchars($manufacturer['name']) . '</p>';
        }

        // Bild ausgeben
        return '<img src="' . htmlspecialchars($image_url) . '" alt="' . htmlspecialchars($manufacturer['name']) . '" class="manufacturer-logo">';
    }


    public function hookDisplayBrandMiniatureItem($params)
    {
        error_log("HOOK displayBrandMiniatureItem WIRD AUFGERUFEN!", 3, _PS_ROOT_DIR_ . "/var/logs/hook_debug.log");

        if (!isset($params['brand']) || !isset($params['brand']['id_manufacturer'])) {
            error_log("Fehlende Hersteller-Daten", 3, _PS_ROOT_DIR_ . "/var/logs/hook_debug.log");
            return '<p style="color:red;">Fehlende Hersteller-Daten</p>';
        }

        $brand = $params['brand'];
        $id_manufacturer = (int)$brand['id_manufacturer'];

        // Falls `image_url` nicht vorhanden ist, nachladen
        if (empty($brand['image_url'])) {
            $sql = 'SELECT image_url FROM ' . _DB_PREFIX_ . 'manufacturer WHERE id_manufacturer = ' . $id_manufacturer;
            $brand['image_url'] = Db::getInstance()->getValue($sql);
        }

        // **Bild-URL aufbauen**
        $brand['image_url'] = !empty($brand['image_url']) 
            ? _PS_BASE_URL_ . "/img/m/" . $brand['image_url'] 
            : _PS_BASE_URL_ . "/img/m/default.jpg";

        error_log("Hersteller {$brand['name']} → Bild: {$brand['image_url']}", 3, _PS_ROOT_DIR_ . "/var/logs/hook_debug.log");

        return $this->display(__FILE__, 'views/templates/hook/brand_miniature_item.tpl');
    }
}

/classes/ManufacturerHelper.php
 

<?php

class ManufacturerHelper
{
    public static function getImageUrl($id_manufacturer)
    {
        if (!$id_manufacturer) {
            return _PS_BASE_URL_ . "/img/m/default.jpg"; // Fallback-Bild
        }

        $sql = 'SELECT image_url FROM ' . _DB_PREFIX_ . 'manufacturer WHERE id_manufacturer = ' . (int)$id_manufacturer;
        $image_name = Db::getInstance()->getValue($sql);

        // **Bild-URL aufbauen**
        return !empty($image_name) 
            ? _PS_BASE_URL_ . "/img/m/" . $image_name 
            : _PS_BASE_URL_ . "/img/m/default.jpg";
    }
}

/views/templates/hook/brand_miniature_item.tpl
 

{block name='brand_miniature_item'}
  <li class="brand">
    <div class="brand-img">
      <a href="{$brand.url}">
        <img src="{$brand.image_url}" alt="{$brand.name}" loading="lazy">
      </a>
    </div>
    <div class="brand-infos">
      <p><a href="{$brand.url}">{$brand.name}</a></p>
    </div>
    <div class="brand-products">
      <a href="{$brand.url}">{$brand.nb_products}</a>
      <a href="{$brand.url}">{l s='View products' d='Shop.Theme.Actions'}</a>
    </div>
  </li>
{/block}

/override/classes/Manufacturer.php
 

<?php

class Manufacturer extends ManufacturerCore
{
    public $image_url; // Neue Eigenschaft für das Bild

    public function __construct($id_manufacturer = null, $id_lang = null, $id_shop = null)
    {
        parent::__construct($id_manufacturer, $id_lang, $id_shop);

        if ($id_manufacturer) {
            $sql = 'SELECT image_url FROM ' . _DB_PREFIX_ . 'manufacturer WHERE id_manufacturer = ' . (int) $id_manufacturer;
            $this->image_url = Db::getInstance()->getValue($sql);
        }
    }
}

 

Link to comment
Share on other sites

Hello QuickUpdate,

thanks for your quick reply.
Where do I have to place these hooks or can I do it without these hooks?
I would prefer to do everything in the modules or with override and not make any entries in the themes.
Thanks in advance
Newbie25

Link to comment
Share on other sites

hookDisplayManufacturerLogo & hookDisplayManufacturerLogo kind of hook doesn't exist in the Pretashop. You need to create your own hook to use. 

 

Refer to the URL below to create own hook (Section: Going further: Creating your own hook)
https://devdocs.prestashop-project.org/8/modules/concepts/hooks/

 

Then call the hook in the manufacture page tpl. 

{hook h='hookName' mod='modulename'}

Link to comment
Share on other sites

  • 1 month later...
On 3/12/2025 at 12:57 AM, Newbie25 said:

Hello QuickUpdate,

thanks for your quick reply.
Where do I have to place these hooks or can I do it without these hooks?
I would prefer to do everything in the modules or with override and not make any entries in the themes.
Thanks in advance
Newbie25

Use the existing Prestashop hooks if you do not want to change anything in your theme. Here is a complete list of hooks.

https://devdocs.prestashop-project.org/8/modules/concepts/hooks/list-of-hooks/

Also, you can manipulate the design of existing hooks using CSS.

If you will use a custom hook, then you need to put these hooks somewhere in your theme files as per your requirement.

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