Jump to content

How to 'Hello World' the ProductController?


TWDesign

Recommended Posts

Basic Smarty 101 question but I've looked all over the interwebs for

these baby steps and havent found anything for OOP newbs like me.

 

I want to figure out how to calculate stuff in the controller files (using PHP) and

then output the stuff in the template files.

 

So as a minimal case excercise, I have put

 

<p>{$myOutPut}</p> 

 

in the appropriate place of the product.tpl file.

 

Now I want to pass stuff to that variable from the ProductController file.

I have created a new file in the overrides directory as such:

/overide/controller/ProductController

 

with ONLY this code.

 

class ProductController extends ProductControllerCore
{

   /// What the heck goes in here ???

}

 

Everything I've tried so far has produced either a total whiteout or rendered

the page without the $myOutPut variable.

 

I have set the preferences/performance to force compile.

I am running PrestaShop 1.4

 

Could somebody give me just a tip in the right direction?

Link to comment
Share on other sites

hello again

 

good that you found how to override the controllers. the next thing is to override the function that provides smarty the variables. this is usually the "process" function.

 

the code below will provide you access to the cart and currency and call the parent product controller, ensuring that what typically occurs, will occur. once the normal product controller process is complete, then you can do whatever you need to do. for example, create a smarty variable called 'myOutPut', with value "Hello World!"

 

   public function process()
   {
       global $cart, $currency;
       parent::process();

    //insert your code here....
  	 self::$smarty->assign(array(
           'myOutPut' => "Hello World!",
  	 ));

   }

Link to comment
Share on other sites

Hmmm.

 

Well, the page only partly rendered.

The top of the page and part of the sidebar rendered but everything else failed.

 

I'm wondering about a couple of things.

Is it absolutely necessary to write the

global $cart, $currency;

 

part of the above function.

 

And is it absolutely necessary to write the name/value pair

as an array?

 

Could I simply use

 

self::$smarty->assign('myOutPut','Hello World');

OR SHOULD IT BE

self::$smarty->assign($myOutPut,'Hello World');

 

??

Link to comment
Share on other sites

The product info 'fails', because you do not assign the variables to the template file, as the 'process' method does that.

 

It is necessary to include 'global $cart, $currency;' if you want to use them.

 

You can use :

self::$smarty->assign('myOutPut','Hello World');

 

it is not necessary to put it in an array.

 

This:

self::$smarty->assign($myOutPut,'Hello World');

 

is incorrect, as $myOutPut wasn't initialised, it is NULL.

Link to comment
Share on other sites

The product info 'fails', because you do not assign the variables to the template file, as the 'process' method does that.

 

It is necessary to include 'global $cart, $currency;' if you want to use them.

 

You can use :

self::$smarty->assign('myOutPut','Hello World');

 

it is not necessary to put it in an array.

 

This:

self::$smarty->assign($myOutPut,'Hello World');

 

is incorrect, as $myOutPut wasn't initialised, it is NULL.

 

 

Thank you.

I can follow most of the advise above but still have an important question.

 

You said:

 

The product info 'fails', because you do not assign the variables to the template file, as the 'process' method does that.

 

I am confused about this.

Do you mean I have to list all the variables again in the process() function?

 

As a designer how to I put the "hello world" into the correct place on the page?

Am I incorrect in just using <p>{$myOutPut}</p> in the template file?

Link to comment
Share on other sites

its hard to say why the product info is failing, please post your entire custom controller code. you do not need to list all the variables again, as long as you included the parent::process() as I had previously suggested

 

to view the output of the custom variable, you are correct in using {$myOutPut}. you can format that using any html you like.

 

in config/config.inc.php, turn display_errors on. you are likely receiving an error in the product page, but the error is hidden.

@ini_set('display_errors', 'on');

Link to comment
Share on other sites

The product info is not displayed if you have overriden the process() method and didn't call the parent's process.

 

By creating the extended class, all functions/methods you have defined in it will overrule the parent's methods.

 

So you need use parent::process(), this will execute the method of the parent class too.

 

A very simple example to illustrate:

<?php
class A {
function example() {
	echo "I am A::example().<br />\n";
}
}
class B extends A {
function example() {
	echo "I am B::example()<br />\n";
	parent::example();
}
}
$b = new B;
$b->example();
?>

 

This will output 'I am B::example() I am A::example()'

 

If you removed parent::example() the output would be 'I am B::example()' .

Link to comment
Share on other sites

@CartExpert.net

 

Have you bothered to read the thread? What you are saying has already been stated and suggested in the sample code provided.

 

   public function process()
   {
       global $cart, $currency;
       parent::process();

           //insert your code here....
        self::$smarty->assign(array(
           'myOutPut' => "Hello World!",
        ));

   }

  • Like 1
Link to comment
Share on other sites

OK. Here is exactly what my ProductController file in the /overides/controllers directory looks like:

 

<?php

class ProductController extends ProductControllerCore
{

   public function process()
   {
    global $cart, $currency;
    parent::process();

    self::$smarty->assign(array(
	    'myOutPut' => "Hello World!",
	 ));

   }

}

 

I turned on error reporting and this is what I can see:

 

Fatal error: Access to undeclared static property: ProductController::$smarty in /home2/myserver/public_html/subdir/override/controllers/ProductController.php on line 11

 

line 11 is

 

self::$smarty->assign(array(
	    'myOutPut' => "Hello World!",
	 ));

 

 

Any advice peoples?

Link to comment
Share on other sites

OK, I got this to work.

After googling "undeclared static property" I figured out that there might be a small but vital

syntax error bellini13.

 

I tried this:

 

$this->smarty->assign(array(
		'myOutPut' => "Hello World!",
	 ));

 

And it works! So happy I almost peed with joy :P

Link to comment
Share on other sites

@CartExpert : Appreciate your help and patient explanations, too.

I really didnt understand the part about calling the parent function until you mentioned it.

 

This also works of course, as you suggested.

 

$this->smarty->assign('myOutPut','Hello World');

Link to comment
Share on other sites

One more question!

 

In the product.tpl file this

 

{if ({$lang_iso}=='en')}<p>{l s='Say hello?'}</p>{/if}
<p>{$myOutPut}</p>  

 

produces

 

Say hello?

 

BUT

 

<p>{$myOutPut}</p>
{if ({$lang_iso}=='en')}<p>{l s='Say hello?'}</p>{/if}

produces

 

Hello World

 

So the order in which the variables are introduced in the template file

has some impact?

Any way to safeguard against that?

Link to comment
Share on other sites

  • 2 years later...

Hello

How are you?

 

I saw your post carefully.

 

I am developing the prestashop online shop

I have one question.

 

I was override the initConent() function in ProductController , but it is not work properly.

That is , initcontent() function to be override is not working.

will you do me a favor?

 

 

Link to comment
Share on other sites

Hello

How are you?

 

I saw your post carefully.

 

I am developing the prestashop online shop

I have one question.

 

I was override the initConent() function in ProductController , but it is not work properly.

That is , initcontent() function to be override is not working.

will you do me a favor?

 

can  you show code of your override?

what you mean by "it's not work properly" ?

you've got some errors? blank page? or ... or what?

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