Jump to content

[solved] New Tab in product back-office


skorupa

Recommended Posts

Hi,

I try to build plugin that adds new tab to product add/edit in back office. Found out that it is quite easy with displayAdminProductsExtra hook:

 

public function install()
{
 if(parent::install() == false
  || !$this->registerHook('displayAdminProductsExtra'))
return false;
 return true;
}

 

and my function:

public function hookDisplayAdminProductsExtra($params)
{
 // ... some stuff
 return 'output';
}

 

But unfortunately function that call this hook doesn't give us proper product info (AdminProductsController.php:4098):

public function initFormModules($obj)
{
 $id_module = Db::getInstance()->getValue('SELECT `id_module` FROM `'._DB_PREFIX_.'module` WHERE `name` = \''.pSQL($this->tab_display_module).'\'');
$this->tpl_form_vars['custom_form'] = Hook::exec('displayAdminProductsExtra', array(), (int)$id_module);
}

 

My solution for this problem is overrite:

public function initFormModules($obj)
{
 $id_module = Db::getInstance()->getValue('SELECT `id_module` FROM `'._DB_PREFIX_.'module` WHERE `name` = \''.pSQL($this->tab_display_module).'\'');
$this->tpl_form_vars['custom_form'] = Hook::exec('displayAdminProductsExtra', array('obj' => $obj), (int)$id_module);
}

 

But my main concern is why it is not default functionality. Is there some disadvanatages to it, or just oversign.

Link to comment
Share on other sites

  • 1 month later...

Hello skorupa. I hope is not too late for you. I have a solution for you:

 

public function hookDisplayAdminProductsExtra($params) {
		$id_product = Tools::getValue('id_product');
		$product = new Product($id_product);
		//..
		return 'output';
}

 

I hope this would be helpful for you (or anyone else reading this topic ^_^ )

 

Best Regards,

 

Axel

------------------

Check this cool modules (must have) :

MultiTabsForProducts

LoginAsCustomer for PS1.5

Cart Details

Edited by axelmdp (see edit history)
  • Like 3
Link to comment
Share on other sites

Hello skorupa. I hope is not too late for you. I have a solution for you:

 

public function hookDisplayAdminProductsExtra($params) {
		$id_product = Tools::getValue('id_product');
		$product = new Product($id_product);
		//..
		return 'output';
}

 

I hope this would be helpful for you (or anyone else reading this topic ^_^ )

 

Best Regards,

 

 

 

thanks for sharing the solution

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

Hi,

After creating the tab, and a form in the tab

How do i control where the data is saved, when the buttton "Save" or "save and stay" is pressed?

Thanks

Pedro

 

Hello Pedro, how are you? You can use the hook "actionAdminProductsControllerSaveAfter"

public function hookActionAdminProductsControllerSaveAfter($params) {
 //Your code...
}

 

Of course, don't forget to register and unregister the hook inside the install and uninstall methods, respectively.

 

Bests Regards,

 

Axel

------------------

Check this cool modules (must have) :

MultiTabsForProducts

LoginAsCustomer for PS1.5

Cart Details

Link to comment
Share on other sites

Thanks axelmdp.

 

Just one last thing.

Say i have a input text with the name "desc1". How can i get the result inside that hook? Is it as simple as $params[desc1]?

My experience with hooks with "action" is little. And it's hard to get an output to see the result.

 

Thanks again.

Link to comment
Share on other sites

Thanks again.

 

You're welcome Pedro.

 

Well, some considerations about the mentioned hook:

1 - You can get all parameters that you have put into the tab as "input" in this way:

 $myVar = Tools::getValue('INPUT_NAME');

2 - You can get the product object in this way:

  $product = $params['return'];

3 - You should check if the save operation was sucessfull during the request processing. To do that, you could put your code inside this condition:

if ($product AND Tools::getValue('MY_TAB_WAS_LOADED') == '1' AND $product->id) {
   //Put your code here
}

Where "MY_TAB_WAS_LOADED" it would be a field that you also should had added as input on the tab html code:

<input name="MY_TAB_WAS_LOADED" type="hidden" value="1">

I'd recommend this strategy in order to ensure that your tab was properly loaded. Take in account that the loading tabs process is done via ajax, and it is possible that the user presses the save button when your tab has not even been loaded.

 

I hope this was useful for you and the developers community. If you like it, click on the button. If you love it, make me a donation. :)I'd really appreciate it.

 

Best Regards,

 

Axel

------------------

Check this cool modules (must have) :

MultiTabsForProducts

LoginAsCustomer for PS1.5

Cart Details

Link to comment
Share on other sites

  • 2 years later...
  • 2 months later...

thanks for sharing the solution

 

This is more a workaround than a solution IMO because the SQL logic for retrieve the product is executed twice AFAIK.

 

Why the product object is not passed directly on the parameters array? @skorupa override make sense, it should be integrated on the core.

 

EDIT: I made a PR about that: https://github.com/PrestaShop/PrestaShop/pull/5095

Edited by Soullivaneuh (see edit history)
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...