Jump to content

Recommended Posts

Hi,


 


I want to extend category controller of a field of type "switch" - [value = 1 or 0]


My questions:


1. Can I create new table in database with id, id_category, value, do I have to unconditionally enter additional values to the table ps_category ?


2. If I can make it through another table, how can I save these values using overwriting the controller ?


 

Link to comment
Share on other sites

You can add new attribute table in ps_category by using phpmyadmin and adding this line in SQL: 

ALTER TABLE `ps_category` ADD `switch` TINYINT NOT NULL DEFAULT '0'

Then go to classes/category.php and in "public static $definition" add new element in fields:

'switch' =>  array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),

and save it in override/classes/category.php

 

Now you can use it.

 

For example when save category you can use:

$myCategory = new Category(...);
$myCategory->switch = 1;
$myCategory->add();

Also you can create a new table but it have to save values independtly. E.g

$myCategory = new Category(..);
$switch = 1;
dB::getInstance()->insert('my_table_created', array(
     'id_category' => $myCategory->id,
     'switch' => $switch
)); 
  • Like 1
Link to comment
Share on other sites

Normally database data have no change when prestashop update. Your tables and columns created will have no changes.

 

But to avoid problems is good practice save database and files before update it.

 

There are some problems to files. If you override Category.php file. Changes will no affect to that file and some error could appear. You have to override it again will your specific changes.

Link to comment
Share on other sites

I decided to create a new table.

I added a new field to the category controller, and in the main file adds data to the database method "hookActionCategoryAdd"

 



public function hookActionCategoryAdd($params)
{
$switch = Tools::getValue( 'switch' );
$insertData = array(
'id_category' => $params['category']->id,
'value' => $switch
);

DB::getInstance()->insert('pmcategorylast', $insertData);
}


 

The question is: how to display the saved data when I edit category, I mean how can I edit in the back office ?

Edited by dwojod (see edit history)
Link to comment
Share on other sites

My module is already finished.

To edit and display saved data I used hookDisplayBackOfficeCategory.

All functionality has been written using the appropriate hooks.
 
jllramos thank for your help ;)
Link to comment
Share on other sites

  • 1 month later...

Thanks you just saved me lots of time!  I was looking all over for such an module and searched the addons and didn't find it, I even asked prestashop support 3 times and they didn't come across that gem! I was actually looking to change a standard template based on the category, but this seems like you can setup each category by itself, which is fine.

 

I was about to try doing this instead.

http://digitaldisseny.com/en/blog/195-prestashop-different-layouts-per-category

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