Jump to content

Specials module on HomeTab


Recommended Posts

If you want to place it there, you have to modify the code and add a method named hookDisplayHomeTabContent to the php file, returning the same content of hookRightColumn. Of course then you need to style css and change the tpl file accordingly!

  • Like 1
Link to comment
Share on other sites

I registered new hooks, put in these two methods, try to do similar like in other 3 modules. 

I managed to show tab on front but it's not clickable, because all other tabs have attributes data-toggle="tab" and href assigned to them. If you hardcode them into tab.tpl it doesn't really do anything. Guess is some javascript or something, I don't really know.

 

I tried, but this is way over my knowledge of PHP. Maybe someone will do this in the future heh. Till then...

 

Thanks for all the help anyway.

Link to comment
Share on other sites

He means you need to add that hook to display the tab label :)

 

i have tried Transplanting the Specials module to displayHomeTab. is that what he means?

 

however, i get this error, "This module cannot be transplanted to this hook."

 

is there something i'm missing? thanks

Link to comment
Share on other sites

yes, you need to add the hookDisplayHomeTab method to the module as well. It should return a template file similar to 

 

<li><a class="homefeatured">{l s='Popular' mod='homefeatured'}</a></li>

 

care to share how to do it in details? apologies as i am new to prestashop i'm still crawling my way to customizing :)

thank you!

Link to comment
Share on other sites

Add this to the module:

	public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

As well as the one I mentioned above, the create the tab.tpl file and add the <li> content, and use the blockspecials.tpl one for the tab content function :)

Link to comment
Share on other sites

Add this to the module:

	public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

As well as the one I mentioned above, the create the tab.tpl file and add the <li> content, and use the blockspecials.tpl one for the tab content function :)

 

in which php file do i need to add this function? sorry taking it slow. as i still need a step by step procedure on this hehehe.. please be patient with me :D

Link to comment
Share on other sites

okay i already added

public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

in blockspecials.php. next is? :)

Link to comment
Share on other sites

I registered new hooks, put in these two methods, try to do similar like in other 3 modules. 

I managed to show tab on front but it's not clickable, because all other tabs have attributes data-toggle="tab" and href assigned to them. If you hardcode them into tab.tpl it doesn't really do anything. Guess is some javascript or something, I don't really know.

 

I tried, but this is way over my knowledge of PHP. Maybe someone will do this in the future heh. Till then...

 

Thanks for all the help anyway.

 

hi drugmirko. were you able to solve your issue? care to help by sharing me the steps you went to finally make it? i'm already lost after adding the code to blockspecials.php :(  thanks in advance!

Link to comment
Share on other sites

what actually is the problem? 

you've got trobules with .... ?

 

hi vekia. thank you for replying. i'm kinda lost with what to do next after adding the function in blockspecials.php. i'm not sure what's next to be done. would appreciate much a step by step after it :)

Link to comment
Share on other sites

i only added this code. transplanted the module and i get a blank page.

public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

Nemo1 is telling about

 

If you want to place it there, you have to modify the code and add a method named hookDisplayHomeTabContent to the php file, returning the same content of hookRightColumn. Of course then you need to style css and change the tpl file accordingly!

 

how about this? this is the one i can't understand. thanks Vekia :)

Link to comment
Share on other sites

You need to add homeDisplayTabContent as well :)

You can copy it from blocknewproducts for example, then just make sure you use the same content of the hookDisplayLeftCOlumn method of blockspecials.php :)

 

hi Nemo. thank you for replying. i have added this code in blockspecials.php

public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

public function hookdisplayHomeTabContent($params)
	{
		if (Configuration::get('PS_CATALOG_MODE'))
			return;
		
		// We need to create multiple caches because the products are sorted randomly
		$random = date('Ymd').'|'.round(rand(1, max(Configuration::get('BLOCKSPECIALS_NB_CACHES'), 1)));

		if (!Configuration::get('BLOCKSPECIALS_NB_CACHES') || !$this->isCached('blockspecials.tpl', $this->getCacheId('blockspecials|'.$random)))
		{
			if (!($special = Product::getRandomSpecial((int)$params['cookie']->id_lang)) && !Configuration::get('PS_BLOCK_SPECIALS_DISPLAY'))
				return;

			$this->smarty->assign(array(
				'special' => $special,
				'priceWithoutReduction_tax_excl' => Tools::ps_round($special['price_without_reduction'], 2),
				'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
			));
		}

		return $this->display(__FILE__, 'blockspecials.tpl', (Configuration::get('BLOCKSPECIALS_NB_CACHES') ? $this->getCacheId('blockspecials|'.$random) : null));
	}

then transplanted the Specials block module in displayHomeTab & displayHomeTabContent.

i still get a blank page. something else i'm still missing? sorry for the trouble :)

Link to comment
Share on other sites

blank page = turn error reporting on

then you will see detailed log information what and where doesnt work :)

 

okay i got this error

Fatal error: Call to undefined method BlockSpecials::_cacheProducts() in /var/www/prestaPOL/modules/blockspecials/blockspecials.php on line 219 

some lines of code i'm missing? :)

Link to comment
Share on other sites

Hello

 

I have added to blockspecials.php the following code =

 

public function hookDisplayHomeTab($params)

    {
        if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
            $this->_cacheProducts();

        return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
    }

 

 

I managed to hook specials block module in displayHomeTab.

 

BUT after that my webpage turns blank and nothing will appear.

 

I guess I miss something ...

Can you help?

 

Thanks !!!!

Herve

Link to comment
Share on other sites

@roz add this as well

	public function _cacheProducts()
	{
		if (!isset(HomeFeatured::$cache_products))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
		}

		if (HomeFeatured::$cache_products === false || empty(HomeFeatured::$cache_products))
			return false;
	}

Of course change the homefeature related variables!!

 

@Herve, enable error reporting as described in my signature ;)

Link to comment
Share on other sites

@roz add this as well

	public function _cacheProducts()
	{
		if (!isset(HomeFeatured::$cache_products))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
		}

		if (HomeFeatured::$cache_products === false || empty(HomeFeatured::$cache_products))
			return false;
	}

Of course change the homefeature related variables!!

 

@Herve, enable error reporting as described in my signature ;)

 

hi Nemo1. sorry again :)  

so i already added this chunk of code replaced the variables (which i'm not sure if correct :D )

and i get this error

Fatal error: Access to undeclared static property: BlockSpecials::$cache_products in /var/www/prestaPOL/modules/blockspecials/blockspecials.php on line 253

so i guess i'm using a wrong variable? :rolleyes:

Link to comment
Share on other sites

no, you have to declare it at the beginning of the file ;)

 

protected $cache_products;

 

hi Nemo1! i already have declare it but i still get the same error  :(

Fatal error: Access to undeclared static property: BlockSpecials::$cache_products in /var/www/prestaPOL/modules/blockspecials/blockspecials.php on line 254 

these are the lines of codes at the end of my file so far.

	public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

	public function hookdisplayHomeTabContent($params)
	{
		if (Configuration::get('PS_CATALOG_MODE'))
			return;
		
		// We need to create multiple caches because the products are sorted randomly
		$random = date('Ymd').'|'.round(rand(1, max(Configuration::get('BLOCKSPECIALS_NB_CACHES'), 1)));

		if (!Configuration::get('BLOCKSPECIALS_NB_CACHES') || !$this->isCached('blockspecials.tpl', $this->getCacheId('blockspecials|'.$random)))
		{
			if (!($special = Product::getRandomSpecial((int)$params['cookie']->id_lang)) && !Configuration::get('PS_BLOCK_SPECIALS_DISPLAY'))
				return;

			$this->smarty->assign(array(
				'special' => $special,
				'priceWithoutReduction_tax_excl' => Tools::ps_round($special['price_without_reduction'], 2),
				'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
			));
		}

		return $this->display(__FILE__, 'blockspecials.tpl', (Configuration::get('BLOCKSPECIALS_NB_CACHES') ? $this->getCacheId('blockspecials|'.$random) : null));
	}
	
	public function _cacheProducts()
	{
		if (!isset(BlockSpecials::$cache_products))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('BLOCKSPECIALS_NB_CACHES');
			BlockSpecials::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
		}

		if (BlockSpecials::$cache_products === false || empty(BlockSpecials::$cache_products))
			return false;
	}

the additional declaration i added here because if i put it on top of it it gives error

class BlockSpecials extends Module
{
	protected $cache_products;
	private $_html = '';
	private $_postErrors = array();

something wrong with my codes?

thank you :)

Link to comment
Share on other sites

Uh error! Weird, what kind of error? The declaration looks fine to me (or I am still sleeping!)

 

this was the error :)

Fatal error: Access to undeclared static property: BlockSpecials::$cache_products in /var/www/prestaPOL/modules/blockspecials/blockspecials.php on line 254
Link to comment
Share on other sites

i upgraded the module hoping it will fix then added the codes again. but still the same error :(

Fatal error: Access to undeclared static property: BlockSpecials::$cache_products in /var/www/prestaPOL/modules/blockspecials/blockspecials.php on line 255 

the error is referring to this line,

self::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');

anyone please help :(

Link to comment
Share on other sites

yess i replied and then removed it because my post was not useful :/ (i noticed that after posting it)

 

and regarding to your problem (new idea)

i see that here, around the forum, you can find several topics related to problems with this "tab" feature.

problems are related to caching,

so maybe in this case it is worth to remove caching from this module?

 

you will hit two birds with one stone:

- it's a fix for actual problem

- you will avoid problems with caching in the future

Link to comment
Share on other sites

yess i replied and then removed it because my post was not useful :/ (i noticed that after posting it)

 

and regarding to your problem (new idea)

i see that here, around the forum, you can find several topics related to problems with this "tab" feature.

problems are related to caching,

so maybe in this case it is worth to remove caching from this module?

 

you will hit two birds with one stone:

- it's a fix for actual problem

- you will avoid problems with caching in the future

 

i see. so.. what exactly do i need to do here? :)

do you mean i need to remove caching from the codes? or where else? 

let me know please. will try to read on other "tab issue" topics too. but thank you if you'll guide me :)

Link to comment
Share on other sites

remove whole funciton:
 

public function _cacheProducts()
	{
		if (!isset(BlockSpecials::$cache_products))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('BLOCKSPECIALS_NB_CACHES');
			BlockSpecials::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
		}

		if (BlockSpecials::$cache_products === false || empty(BlockSpecials::$cache_products))
			return false;
	}

change:

return $this->display(__FILE__, 'blockspecials.tpl', (Configuration::get('BLOCKSPECIALS_NB_CACHES') ? $this->getCacheId('blockspecials|'.$random) : null));

to:
 

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

change:
 

	public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

to:

	public function hookDisplayHomeTab($params)
	{


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

change:

		return $this->display(__FILE__, 'blockspecials.tpl', (Configuration::get('BLOCKSPECIALS_NB_CACHES') ? $this->getCacheId('blockspecials|'.$random) : null));

to:

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

Link to comment
Share on other sites

 

remove whole funciton:

 

public function _cacheProducts()
	{
		if (!isset(BlockSpecials::$cache_products))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('BLOCKSPECIALS_NB_CACHES');
			BlockSpecials::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
		}

		if (BlockSpecials::$cache_products === false || empty(BlockSpecials::$cache_products))
			return false;
	}

change:

return $this->display(__FILE__, 'blockspecials.tpl', (Configuration::get('BLOCKSPECIALS_NB_CACHES') ? $this->getCacheId('blockspecials|'.$random) : null));

to:

 

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

change:

 

	public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

to:

	public function hookDisplayHomeTab($params)
	{


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

change:

		return $this->display(__FILE__, 'blockspecials.tpl', (Configuration::get('BLOCKSPECIALS_NB_CACHES') ? $this->getCacheId('blockspecials|'.$random) : null));

to:

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

 

hi Vekia! 

 

okay i finally got rid of the error but this message is appearing on the tabs area 

No template found for module blockspecials

oxDsq.jpg

and then this block appears below

 

GbgB1.jpg

 

what did i do wrong this time? :D  :rolleyes:  sorry for the trouble  :rolleyes:

Link to comment
Share on other sites

 

remove whole funciton:

 

public function _cacheProducts()
	{
		if (!isset(BlockSpecials::$cache_products))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('BLOCKSPECIALS_NB_CACHES');
			BlockSpecials::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
		}

		if (BlockSpecials::$cache_products === false || empty(BlockSpecials::$cache_products))
			return false;
	}

change:

return $this->display(__FILE__, 'blockspecials.tpl', (Configuration::get('BLOCKSPECIALS_NB_CACHES') ? $this->getCacheId('blockspecials|'.$random) : null));

to:

 

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

change:

 

	public function hookDisplayHomeTab($params)
	{
		if (!$this->isCached('tab.tpl', $this->getCacheId('specialproducts-tab')))
			$this->_cacheProducts();

		return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('specialproducts-tab'));
	}

to:

	public function hookDisplayHomeTab($params)
	{


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

change:

		return $this->display(__FILE__, 'blockspecials.tpl', (Configuration::get('BLOCKSPECIALS_NB_CACHES') ? $this->getCacheId('blockspecials|'.$random) : null));

to:

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

 

hi Vekia! 

 

okay i finally got rid of the error but this message is appearing on the tabs area 

No template found for module blockspecials

oxDsq.jpg

and then this block appears below

 

GbgB1.jpg

 

what did i do wrong this time?  :D   :rolleyes:  sorry for the trouble   :rolleyes:

Link to comment
Share on other sites

Hi,

 

"No template found for module blockspecials" : you have to create a "blockspecials" folder in your theme/modules/ and create a "tab.tpl".

{counter name=active_li assign=active_li}
<li{if $active_li == 1} class="active"{/if}><a data-toggle="tab" href="#blockspecials" class="blockspecials">{l s='Specials' mod='blockspecials'}</a></li>
Link to comment
Share on other sites

 

Hi,

 

"No template found for module blockspecials" : you have to create a "blockspecials" folder in your theme/modules/ and create a "tab.tpl".

{counter name=active_li assign=active_li}
<li{if $active_li == 1} class="active"{/if}><a data-toggle="tab" href="#blockspecials" class="blockspecials">{l s='Specials' mod='blockspecials'}</a></li>

 

hi weelh1! 

thanks! i had the tab name sorted out. 

now i think i only need to know how to display the products the way featured, and new arrivals are presented.

any idea how? :) 

Link to comment
Share on other sites

 

Hi,

 

"No template found for module blockspecials" : you have to create a "blockspecials" folder in your theme/modules/ and create a "tab.tpl".

{counter name=active_li assign=active_li}
<li{if $active_li == 1} class="active"{/if}><a data-toggle="tab" href="#blockspecials" class="blockspecials">{l s='Specials' mod='blockspecials'}</a></li>

 

hey weelh1! i received an email notification of your reply but i see nothing here? :) did you deleted it or something? :)

Link to comment
Share on other sites

  • 4 months later...

Guys if you want to keep caching you have to fix declaration "protected static $cache_products" and everything works now i have a problem with it showing only one product.

 

from where? module .php file? you're reffering to recently released version? and what exactly you mean by "fix declaration" ?

Link to comment
Share on other sites

  • 1 month later...

Hello,

 

I follow this topic to add specials tab and it works fine.

I would like now to add a translation to the word "Specials" and I don't know where to do this !

Here are the files I use :

 

modules/blockspecials/tab.tpl  :

<li><a data-toggle="tab" href="#blockspecials" class="blockspecials">{l s='Specials' mod='blockspecials'}</a></li>

 

modules/blockspecials/blockspecials.php with hooks :

    public function hookDisplayHomeTab($params)
    {
        if (Configuration::get('PS_CATALOG_MODE'))
            return;

        if (!$this->isCached('tab.tpl', $this->getCacheId('blockspecials-tab')))
            BlockSpecials::$cache_specials = Product::getPricesDrop((int)$params['cookie']->id_lang, 0, Configuration::get('BLOCKSPECIALS_SPECIALS_NBR'));

        if (BlockSpecials::$cache_specials === false)
            return false;

        return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('blockspecials-tab'));
    }

    public function hookDisplayHomeTabContent($params)
    {
        if (Configuration::get('PS_CATALOG_MODE'))
            return;

        if (!$this->isCached('blockspecials-home.tpl', $this->getCacheId('blockspecials-home')))
        {
            $this->smarty->assign(array(
                'specials' => BlockSpecials::$cache_specials,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))
            ));
        }

        if (BlockSpecials::$cache_specials === false)
            return false;

        return $this->display(__FILE__, 'blockspecials-home.tpl', $this->getCacheId('blockspecials-home'));
    }
 

Thanks for your help !!

Link to comment
Share on other sites

  • 2 months later...

Hello,

 

I follow this topic to add specials tab and it works fine.

I would like now to add a translation to the word "Specials" and I don't know where to do this !

Here are the files I use :

 

modules/blockspecials/tab.tpl  :

<li><a data-toggle="tab" href="#blockspecials" class="blockspecials">{l s='Specials' mod='blockspecials'}</a></li>

 

modules/blockspecials/blockspecials.php with hooks :

    public function hookDisplayHomeTab($params)

    {

        if (Configuration::get('PS_CATALOG_MODE'))

            return;

 

        if (!$this->isCached('tab.tpl', $this->getCacheId('blockspecials-tab')))

            BlockSpecials::$cache_specials = Product::getPricesDrop((int)$params['cookie']->id_lang, 0, Configuration::get('BLOCKSPECIALS_SPECIALS_NBR'));

 

        if (BlockSpecials::$cache_specials === false)

            return false;

 

        return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('blockspecials-tab'));

    }

 

    public function hookDisplayHomeTabContent($params)

    {

        if (Configuration::get('PS_CATALOG_MODE'))

            return;

 

        if (!$this->isCached('blockspecials-home.tpl', $this->getCacheId('blockspecials-home')))

        {

            $this->smarty->assign(array(

                'specials' => BlockSpecials::$cache_specials,

                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))

            ));

        }

 

        if (BlockSpecials::$cache_specials === false)

            return false;

 

        return $this->display(__FILE__, 'blockspecials-home.tpl', $this->getCacheId('blockspecials-home'));

    }

 

Thanks for your help !!

 

To end this guide you have to go to modules --> position -.> transplant modules --> select Block Specials with displayHomeTab --> Save and after select Block Special with displayHomeTabContent --> Save.

 

Good Work!!!

Link to comment
Share on other sites

×
×
  • Create New...