Jump to content
paddle07

Product Custom Tab/Field - not updating

Recommended Posts

Hi

 

I build a custom products field module following this tutorial:

http://nemops.com/prestashop-products-new-tabs-fields/

 

I added the shop id as I'm using multistore.

 

Everything works fine until I want to update an existing product… as soon as I edit a product (e.g. price or quantity) and save it (either with save and stay or save) the content of the custom field turns blank. The content of the custom field is only saved correctly when I click on the “custom-field-Tab” before I save e.g. a new price…

 

Here's my hookActionProductUpdate code:

public function hookActionProductUpdate($params)
		{
		    // get all languages
		    // for each of them, store the new field
		    $id_product = $params['product']->id;
		    $id_shop = $this->context->shop->id;
		    
		    $languages = Language::getLanguages(true);
		    foreach ($languages as $lang) {
		        if(!Db::getInstance()->update('product_lang', array('custom_field'=> pSQL(Tools::getValue('custom_field_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_shop = ' .$id_shop. ' AND id_product = ' .$id_product ))
		            $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
		    }
		}

Thanks!

Share this post


Link to post
Share on other sites

you are probably erasing the content in the database when you click on save.

 

The product tabs are probably not being loaded unless you click on the tab.  Prestashop uses ajax to load the tab data when you click on the tab.

 

So if you do not click the tab, then the content is blank, and clicking on save will execute your hook and your hook is saving blank to the database.

Share this post


Link to post
Share on other sites

Hi, here  a solution is proposed by Jeff (see comments)

 

I've found how to solve your bug in hookActionProductUpdate, you've to check if POST vars are set before save it with Tools::getIsset.

public function hookActionProductUpdate($params)
{
// get all languages
// for each of them, store the new field

$id_product = (int)Tools::getValue('id_product');

$languages = Language::getLanguages(true);
foreach ($languages as $lang) {
if (Tools::getIsset('custom_field_'.$lang['id_lang']))
if(!Db::getInstance()->update('product_lang', array('custom_field'=> pSQL(Tools::getValue('custom_field_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_product = ' .$id_product ))
$this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
}

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More