Kokomante Posted April 17, 2015 Share Posted April 17, 2015 Hi, probably this is a newby question.I'm developing a module that adds 2 more values to products (activation_date, and deactivation_date). I have a form in a new tab of the back office product section. I can get the values from db, but i don't know how to update the values in DB when submit.I don't have idea if prestashop have some methods to do that or if i must do classical way.Can someone explain to me how to do that?This is the form: <div id="product-informations" class="panel product-tab"> <h3 class="tab">{l s='Crono Products'}</h3> <div class="separation"></div> <div class="form-group"> <form action="hookActionProductUpdate"> <div class="col-lg-1"><span class="pull-right">{include file="controllers/products/multishop/checkbox.tpl" field="activation_date" type="default"}</span></div> <label class="control-label col-lg-2" for="activation_date"> {l s='Activation date'} </label> <div class="col-lg-3"> <input type="date" name="activation_date" value="{$activation_date}" > </div> </div> <div class="form-group"> <div class="col-lg-1"><span class="pull-right">{include file="controllers/products/multishop/checkbox.tpl" field="deactivation_date" type="default"}</span></div> <label class="control-label col-lg-2" for="deactivation_date"> {l s='Deactivation date'} </label> <div class="col-lg-3"> <input type="date" name="deactivation_date" value="{$deactivation_date}"> </div> </div> <div class="panel-footer"> <a href="{$link->getAdminLink('AdminProducts')|escape:'html':'UTF-8'}" class="btn btn-default"><i class="process-icon-cancel"></i> {l s='Cancel'}</a> <button type="submit" name="submitAddproduct" class="btn btn-default pull-right" disabled="disabled"><i class="process-icon-loading"></i> {l s='Save'}</button> <button type="submit" name="submitAddproductAndStay" class="btn btn-default pull-right" disabled="disabled"><i class="process-icon-loading"></i> {l s='Save and stay'}</button> </form> </div> </div> Link to comment Share on other sites More sharing options...
jgamio Posted April 18, 2015 Share Posted April 18, 2015 Read these http://nemops.com/prestashop-products-new-tabs-fields 1 Link to comment Share on other sites More sharing options...
Kokomante Posted April 20, 2015 Author Share Posted April 20, 2015 First: Thanks for answering!I based the module on this turorial. My problem is adapting the code to update the form data. In the tutorial when you change the values of the field (string), it automatically updates the data. But i have date fields, and i don't know how to call de function to update DB.PD: Sorry about my english. Link to comment Share on other sites More sharing options...
jgamio Posted April 20, 2015 Share Posted April 20, 2015 are you using the same table or did you create a new one ? Link to comment Share on other sites More sharing options...
Kokomante Posted April 20, 2015 Author Share Posted April 20, 2015 I have created 2 new fields in "wp_products": "activation_date" and "deactivation_date", both date fields. Link to comment Share on other sites More sharing options...
tuk66 Posted April 22, 2015 Share Posted April 22, 2015 Your changed object have to follow ObjectModel class. Change $definition in /classes/Product.php as well. 1 Link to comment Share on other sites More sharing options...
Kokomante Posted April 23, 2015 Author Share Posted April 23, 2015 (edited) tuk66, thanks for helping too. I have added this two lines in $definition section: 'activation_date' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'), 'deactivation_date' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),It seems that is trying to introduce them but when updating, it allways introduces 0000-00-00 (this is my default value in MySQL fields). Also drops me "Successful update".Some suggestions about this? Edited April 23, 2015 by Kokomante (see edit history) Link to comment Share on other sites More sharing options...
jgamio Posted April 23, 2015 Share Posted April 23, 2015 Did you use the hookActionProductUpdate to save the data ? Link to comment Share on other sites More sharing options...
Kokomante Posted April 24, 2015 Author Share Posted April 24, 2015 This is my code: public function hookActionProductUpdate($params) { if(Tools::isSubmit('submitAddproduct')) { $activation = Tools::getValue('activation_date'); $deactivation = Tools::getValue('deactivation_date'); Db ::getInstance()->Execute("UPDATE"._DB_PREFIX_."'product' SET activation_date = ".$activation.", deactivation_date = ".$deactivation." , WHERE id_product = " . (int)$id_product); } } Link to comment Share on other sites More sharing options...
jgamio Posted April 24, 2015 Share Posted April 24, 2015 Where you take the product id you need $id_product = (int)Tools::getValue('id_product'); Use in these way to write less Db::getInstance()->update('product', array('activation_date'=> $activation ,'deactivation_date'=> '' . $deactivation . '') ,' id_product = ' . $id_product ) Link to comment Share on other sites More sharing options...
Kokomante Posted April 30, 2015 Author Share Posted April 30, 2015 (edited) Hi again! I solved the issue. Thanks to jgamio and tuk66 for your great help! This is the final tpl and hookActionProductUpdate function:TPL-------------------------------------------------------------------------------------------------- <div id="product-informations" class="panel product-tab"> <h3 class="tab">{l s='Crono Products'}</h3> <div class="separation"></div> <div class="form-group"> <div class="col-lg-1"><span class="pull-right">{include file="controllers/products/multishop/checkbox.tpl" field="activation_date" type="default"}</span></div> <label class="control-label col-lg-2" for="activation_date"> {l s='Activation date:'} </label> <div class="col-lg-9"> <div class="input-group fixed-width-md"> <input id="activation_date" name="activation_date" value="{$activation_date}" class="datepicker" type="text" /> <div class="input-group-addon"> <i class="icon-calendar-empty"></i> </div> </div> <p class="help-block">{l s='The date on which the product is automatically enabled.'}</p> </div> </div> <div class="form-group"> <div class="col-lg-1"><span class="pull-right">{include file="controllers/products/multishop/checkbox.tpl" field="deactivation_date" type="default"}</span></div> <label class="control-label col-lg-2" for="deactivation_date"> {l s='Deactivation date:'} </label> <div class="col-lg-9"> <div class="input-group fixed-width-md"> <input id="deactivation_date" name="deactivation_date" value="{$deactivation_date}" class="datepicker" type="text" /> <div class="input-group-addon"> <i class="icon-calendar-empty"></i> </div> </div> <p class="help-block">{l s='The date on which the product is automatically disbled.'}</p> </div> </div> <div class="panel-footer"> <a href="{$link->getAdminLink('AdminProducts')|escape:'html':'UTF-8'}" class="btn btn-default"><i class="process-icon-cancel"></i> {l s='Cancel'}</a> <button type="submit" name="submitAddproduct" class="btn btn-default pull-right" disabled="disabled"><i class="process-icon-loading"></i> {l s='Save'}</button> <button type="submit" name="submitAddproductAndStay" class="btn btn-default pull-right" disabled="disabled"><i class="process-icon-loading"></i> {l s='Save and stay'}</button> </form> </div> </div> PHP FUNCTION -------------------------------------------------------------------------------------------------------------------------- public function hookActionProductUpdate($params) { if (Tools::isSubmit('submitAddproductAndStay') || Tools::isSubmit('submitAddproduct')){ $id_product = (int)Tools::getValue('id_product'); $activation = Tools::getValue('activation_date'); $deactivation = Tools::getValue('deactivation_date'); Db::getInstance()->update('product', array('activation_date'=> $activation ,'deactivation_date'=> '' . $deactivation . '') ,' id_product = ' . $id_product ); } } Edited April 30, 2015 by Kokomante (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now