Jump to content

Updating Custom Product Field


Recommended Posts

I have problems with updating a Product (custom fields) in Prestashop.

 


 

It works quite fine, when I save a product the normal way. But Prestashop 1.6 has an autosave function, when changing stock or prices. This makes my custom fields empty. By trial & error I found out, that my hookActionProductUpdate function causes the problem, but I dont know why:

 



public function hookActionProductUpdate($params) {
    $id_product = Tools::getValue('id_product');
    $sampleObj = Belvg_Sample::loadByIdProduct($id_product);
    $sampleObj->lagerplatz = Tools::getValue('lagerplatz');
    $sampleObj->artnrlfr = Tools::getValue('artnrlfr');
    $sampleObj->archiv = Tools::getValue('archiv');
    $sampleObj->bundle = Tools::getValue('bundle');
    $sampleObj->beschrieb = Tools::getValue('beschrieb');
    $sampleObj->oldartnr = Tools::getValue('oldartnr');


    $sampleObj->id_product = $id_product;


    if(!empty($sampleObj) && isset($sampleObj->id)){
        $sampleObj->update();
    } else {
        $sampleObj->add();
    }


What is wrong with it?

 

Thanks in advance!

Link to comment
Share on other sites

 

I usually add an hidden field in the form

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

Then in the update function

if(!Tools::getValue('CUSTOM_TAB_LOADED'))

return;

this way you only save it if it was loaded.

 

Got this answer in stackoverflow. It's a nice trick!

Link to comment
Share on other sites

  • 2 years later...

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