Jump to content

Different banner in each category? With if statement?


Recommended Posts

Hey ;-)

How can i check in my .tpl file with if statement, if category is equal to some category id or name?

I've tryed with {if $page_name == '2-music-ipods'}banner for this category{/if} and I also tryed with $category_id, but it doesn't work.

So what is the proper way to check for the category?

P.S. - i also tryed with $category->id == 2, but it doesn't work either.

Thank you,
Housy

Link to comment
Share on other sites

Yes, i'm talking about the category page and I've already tryed your code, but it didn't work.

Is there any other way to do that?

Thanks,
Housy

which page are you talking about? Category page?

It it is Category page, try following code

{if $category->id == 2}  something  {/if}

Link to comment
Share on other sites

Maybe I didn't understand what you wanted to do.
First, each category you can have category image which can be uploaded in catalog back office. Why not use that image?


2nd, I think my code should work, if you use it correctly. Can you upload your code category.tpl file by attachment not by copy and paste, so that I can take a look what is going wrong. Also please let me know site URL, you can PM me if you don't want it show up here.

Link to comment
Share on other sites

First of all thanks for helping me out on this.

Maybe i didn't explain clear enough.

I want to have a different flash banner and i want to do that in header.tpl file. So when you click on ipods category for example, you get the flash banner for ipods and then if you click on the computers category, you get the flash banner for computers category. So different flash banner on each category.

How can i do that? With if statements?

Does the $category->id works only in category.tpl file?

Maybe I didn't understand what you wanted to do.
First, each category you can have category image which can be uploaded in catalog back office. Why not use that image?


2nd, I think my code should work, if you use it correctly. Can you upload your code category.tpl file by attachment not by copy and paste, so that I can take a look what is going wrong. Also please let me know site URL, you can PM me if you don't want it show up here.
Link to comment
Share on other sites

Does the $category->id works only in category.tpl file?


Yes. In order to use object in theme file (.tpl), you have to assign the object in smarty first.
I assumed you use in category.tpl

Based on your description, you want to use it header.tpl, so will you have to assign the object int header.php.
But what about if the page is other pages, not category page? what are you going to display?

But here is solution for you.
1. Change file header.php as following

before change

$smarty->assign(array(
   'HOOK_HEADER' => Module::hookExec('header'),
   'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn'),
   'HOOK_TOP' => Module::hookExec('top'),
   'static_token' => Tools::getToken(false),
   'token' => Tools::getToken(),
   'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
   'content_only' => intval(Tools::getValue('content_only'))
));



after change (one line my_category_id is added)

$smarty->assign(array(
   'HOOK_HEADER' => Module::hookExec('header'),
   'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn'),
   'HOOK_TOP' => Module::hookExec('top'),
   'static_token' => Tools::getToken(false),
   'token' => Tools::getToken(),
   'my_category_id' => intval(Tools::getValue('id_category')),
   'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
   'content_only' => intval(Tools::getValue('content_only'))
));




2. add following code in your header.tpl where ever you want.

{if $my_category_id >0}
       {if $my_category_id == 2}
           code here for category 2
       {/if}
       {if $my_category_id == 3}
           code here for category 3
       {/if}
{/if}




I have tested this code and it works fine.

  • Like 3
Link to comment
Share on other sites

  • 1 month later...

Hello again shokinro :coolsmile:

Do you maybe know, how to check for example -> {if language == "en"}?

Because i have some text-images, which are in german and english language.

How can you do that?

Thank you,
Housy

Link to comment
Share on other sites

Greetings,

I am trying to do the same thing: I want to have a different Block Banner for each category.

My link: http://www.texasmatrix.net

What I have
1. theme with block banner on home page
2. theme with advertising banner on home page sidebar

What I want
1. extra block banner on category pages assigned by category id number

Answers I need:
1. How to duplicate banner module
2. code to edit in banner.php and banner.tpl

3. smarty code + what file to edit + where to place code
4. what code + what file to edit + where to place code on category file (or whatever file so it displays)
5. Not sure what to edit so all of this successfully displays on a category page. Not sure if an extra hook is needed or where.

blockbanner.php

<?php

class BlockBanner1 extends Module
{
   public $adv_link;
   public $adv_img;
   public $adv_imgname;

   function __construct()
   {
       $this->name = 'blockbanner1';
       $this->tab = 'Blocks';
       $this->version = 0.1;

       parent::__construct();

       $this->displayName = $this->l('Block Banner1');
       $this->description = $this->l('Adds a block to display an advertising');

       $this->adv_imgname = 'advertising_custom.jpg';

       if (!file_exists(dirname(__FILE__).'/'.$this->adv_imgname))
           $this->adv_img = _MODULE_DIR_.$this->name.'/advertising.jpg';
       else
           $this->adv_img = _MODULE_DIR_.$this->name.'/'.$this->adv_imgname;
       $this->adv_link = htmlentities(Configuration::get('BLOCKADVERT_LINK1'), ENT_QUOTES, 'UTF-8');
   }


   function install()
   {
       Configuration::updateValue('BLOCKADVERT_LINK1', 'http://www.prestashop.com');
       if (!parent::install())
           return false;
       if (!$this->registerHook('home'))
           return false;
       return true;
   }

   public function postProcess()
   {
       global $currentIndex;

       $errors = false;
       if (Tools::isSubmit('submitAdvConf'))
       {
           $file = false;
           if (isset($_FILES['adv_img']) AND isset($_FILES['adv_img']['tmp_name']) AND !empty($_FILES['adv_img']['tmp_name']))
           {
               if ($error = checkImage($_FILES['adv_img'], 4000000))
                   $errors .= $error;
               elseif (!move_uploaded_file($_FILES['adv_img']['tmp_name'], dirname(__FILE__).'/'.$this->adv_imgname))
                   $errors .= $this->l('Error move uploaded file');

               $this->adv_img = _MODULE_DIR_.$this->name.'/'.$this->adv_imgname;
           }
           if ($link = Tools::getValue('adv_link'))
           {
               Configuration::updateValue('BLOCKADVERT_LINK1', $link);
               $this->adv_link = htmlentities($link, ENT_QUOTES, 'UTF-8');
           }
       }
       if ($errors)
           echo $this->displayError($errors);
   }

   public function getContent()
   {
       $this->postProcess();
       echo '
<form action="'.$_SERVER['REQUEST_URI'].'" method="post" enctype="multipart/form-data">
'.$this->l('Advertising block configuration').'
adv_link.'" target="_blank" title="'.$this->l('Advertising').'">';
       if ($this->adv_img)
           echo 'adv_img.'" alt="'.$this->l('Advertising image').'" style="margin-left: 100px;"/>';
       else
           echo $this->l('no image');
       echo '





'.$this->l('Change image').'  <input id="adv_img" type="file" name="adv_img" />




'.$this->l('Image link').'  <input id="adv_link" type="text" name="adv_link" value="'.$this->adv_link.'" />




<input class="button" type="submit" name="submitAdvConf" value="'.$this->l('validate').'" style="margin-left: 200px;"/>

</form>
';
   }

   /**
   * Returns module content
   *
   * @param array $params Parameters
   * @return string Content
   */
   function hookhome($params)
   {
       global $smarty, $protocol_content, $server_host;

       $smarty->assign('image', $protocol_content.$server_host.$this->adv_img);
       $smarty->assign('adv_link', $this->adv_link);

       return $this->display(__FILE__, 'blockbanner1.tpl');
   }

}

?>





blockbanner.tpl

{if $page_name=='index'}
<!-- MODULE Block advertising -->



<!-- /MODULE Block advertising -->
{/if}





header.php

<?php

// P3P Policies (http://www.w3.org/TR/2002/REC-P3P-20020416/#compact_policies)
header('P3P: CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"');

require_once(dirname(__FILE__).'/init.php');

/* CSS */
$css_files[_THEME_CSS_DIR_.'global.css'] = 'all';

/* Hooks are volontary out the initialize array (need those variables already assigned) */
$smarty->assign(array(
   'HOOK_HEADER' => Module::hookExec('header'),
   'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn'),
   'HOOK_TOP' => Module::hookExec('top'),
   'static_token' => Tools::getToken(false),
   'token' => Tools::getToken(),
                 'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
   'content_only' => intval(Tools::getValue('content_only'))
));

if(isset($css_files) AND !empty($css_files)) $smarty->assign('css_files', $css_files);
if(isset($js_files) AND !empty($js_files)) $smarty->assign('js_files', $js_files);

$smarty->display(_PS_THEME_DIR_.'header.tpl');

?>

Link to comment
Share on other sites

After searching for qualified answers on this forum and not finding any I have successfully acheived my goal on my own.

Here is how:

1. Creat a new hook (also ad new table in ps_modules in database)
2. Duplicate or create a new module
3. Assign new module to hook in MODULES positions
4. Edit .tpl to include smarty assign code
5. edit .php to inlclude hook code

I have yet to see a proper answer from anyone on how to assign a banner module via category number using the header file. The above answer from other poster only works if you are calling it in a .tpl file and not a module file.

Link to comment
Share on other sites

  • 3 weeks later...

Hello Shokinro =)

Your code works perfect but this is only for the categories, right? So the code works only, if i visit a category, where all products are listed.

Is it maybe possible to show the same banner on all product pages, that are under category 2 for example?

So when i visit a category - Home>iPods, or product within that category - Home>iPods> iPod shuffle, then the banner would stay the same.

I'll also demonstrate in the code.

{if $my_category_id >0}
       {if $my_category_id == 2 && products in that category}
           banner for category 2 and all products within that category
       {/if}
       {if $my_category_id == 3 && products in that category}
           banner for category 3 and all products within that category
       {/if}
{else}
default banner
{/if}




Hope, you'll understand, what do i have in mind.

Thank you,
Housy

Link to comment
Share on other sites

  • 2 weeks later...
{if $my_category_id == 2}second category{/if}



In the newest PS version 1.4.1 you must modify the FrontController.php within classes directory to check for the categories like the example above. Find public function displayHeader() (line 429) and add my_category_id between logo_image_height and priceDisplayPrecision and you're done.

Your code should look like this:

self::$smarty->assign(array(
           'time' => time(),
           'img_update_time' => Configuration::get('PS_IMG_UPDATE_TIME'),
           'static_token' => Tools::getToken(false),
           'token' => Tools::getToken(),
           'logo_image_width' => Configuration::get('SHOP_LOGO_WIDTH'),
           'logo_image_height' => Configuration::get('SHOP_LOGO_HEIGHT'),
           'my_category_id' => intval(Tools::getValue('id_category')),
           'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
           'content_only' => (int)(Tools::getValue('content_only'))
       ));



Thanks again to shokinro and I hope, this will be helpful to someone.

Cheers, Housy

Link to comment
Share on other sites

  • 5 months later...

Hello Shokinro =)

Your code works perfect but this is only for the categories, right? So the code works only, if i visit a category, where all products are listed.

Is it maybe possible to show the same banner on all product pages, that are under category 2 for example?

So when i visit a category - Home>iPods, or product within that category - Home>iPods> iPod shuffle, then the banner would stay the same.

I'll also demonstrate in the code.

{if $my_category_id >0}
	{if $my_category_id == 2 && products in that category}
		banner for category 2 and all products within that category
	{/if}
	{if $my_category_id == 3 && products in that category}
		banner for category 3 and all products within that category
	{/if}
{else}
default banner
{/if}

Hope, you'll understand, what do i have in mind.

Thank you,

Housy

 

do you have a solution for this? to display the banner even in products from a cateogory? prestashop 1.4.5.1

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
  • 4 weeks later...
  • 4 weeks later...

Does the $category->id works only in category.tpl file?

Yes. In order to use object in theme file (.tpl), you have to assign the object in smarty first.

I assumed you use in category.tpl

 

Based on your description, you want to use it header.tpl, so will you have to assign the object int header.php.

But what about if the page is other pages, not category page? what are you going to display?

 

But here is solution for you.

1. Change file header.php as following

 

before change

$smarty->assign(array(
'HOOK_HEADER' => Module::hookExec('header'),
'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn'),
'HOOK_TOP' => Module::hookExec('top'),
'static_token' => Tools::getToken(false),
'token' => Tools::getToken(),
'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
'content_only' => intval(Tools::getValue('content_only'))
));

 

after change (one line my_category_id is added)

$smarty->assign(array(
'HOOK_HEADER' => Module::hookExec('header'),
'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn'),
'HOOK_TOP' => Module::hookExec('top'),
'static_token' => Tools::getToken(false),
'token' => Tools::getToken(),
'my_category_id' => intval(Tools::getValue('id_category')),
'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
'content_only' => intval(Tools::getValue('content_only'))
));

 

 

2. add following code in your header.tpl where ever you want.

 

{if $my_category_id >0}
	{if $my_category_id == 2}
		code here for category 2
	{/if}
	{if $my_category_id == 3}
		code here for category 3
	{/if}
{/if}

 

 

I have tested this code and it works fine.

 

 

hi im a newbei and learning fast thanks what do i put in the line "code here for category 2" and so on? please help this is exactly what we need. :)

  • Like 1
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...