How to get values from my object model and display my banner on header in Prestashop

408 Views Asked by At

I developed a custom multi-banner module to display on the header of my prestashop page. When i save my changes on form i would like to display it on header.

My module form:

My module form

My table to see all banner inserted:

enter image description here

I tried to use smarty and orm to retrieve all information but i don't know how to display on my header.

Any issues? I hope i can understand my trobles :)

Thanks

Here's the code of some of my files:

//in main file:
   public function hookDisplayHeaderBanner($params) {
        $banner = Banner::getBannerToDisplay();

        // If there is a banner to display
        if ($banner) {
            $this->context->smarty->assign([
                'banner' => $banner
            ]);
    
            return $this->display(__FILE__, 'views/templates/hook/display-custom-banner.tpl');
        }

        // Nothing to display
        return false;
    }



//In classes/Banner.php
class Banner extends ObjectModel
{
    public $id;
    public $color;
    public $background_color;
    public $content;

    public static $definition = [
        'table' => 'custom_banner',
        'primary' => 'id_custom_banner',
        'multilang' => false,
        'fields' => [
            'color' => [
                'type' => self::TYPE_STRING,
            ],
            'background_color' => [
                'type' => self::TYPE_STRING,
            ],
            'content' => [
                'type' => self::TYPE_STRING
            ]
        ]
    ];

    /*
     *
     * Return the banner to display
     * 
     * Here we put the logic to select the right banner
    */
    public static function getBannerToDisplay()
    {
        $sql = 'SELECT *
                    FROM `' . _DB_PREFIX_ . self::$definition['table'] . '`
                    WHERE active = 1';

        return Db::getInstance()->getRow($sql);
    }
}


//In display-custom-banner.tpl
<div id="banner" style="background-color:{$banner.background_color}!important;">
    <p style="color:{$banner.color}!important;">
        {$banner.content}
    </p>
</div> 
1

There are 1 best solutions below

0
gennaris On

Using:

return Db::getInstance()->getRow($sql);

only retrieve the first row of the result, if you would like to return more than one banner you would like to use Db::getInstance()->executeS($sql); to get all results.

Also check your DB table structure and a var_dump() of the involved variables, this would help understanding if there are more issues.