Jump to content

Problems retrieving form values to DB


Kokomante

Recommended Posts

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

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

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 by Kokomante (see edit history)
Link to comment
Share on other sites

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

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

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 by Kokomante (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...