Jump to content

Problem to simplify function


almeidaz

Recommended Posts

Hi , presta community

 

i'm looking to simplify this function  :

 


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

        $id_product = (int)Tools::getValue('id_product');
        $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_product = ' .$id_product ))
                $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
        }

    }

i've done some modifications : custom_field is direclty in product table then i don't want anymore $lang

 

i've try to do this but don't seem to work :

public function hookActionProductUpdate($params)
    {
        
        $id_product = (int)Tools::getValue('id_product');
    
        Db::getInstance()->update('product', array('custom_field'=> pSQL(Tools::getValue('custom_field')) .' AND `id_product` = ' .$id_product)
                $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();

    }

any idea about update syntax ?

 

 

thx.

Link to comment
Share on other sites

The syntax is wrong. Use this one instead:

public function hookActionProductUpdate($params)
    {
        
        $id_product = (int)Tools::getValue('id_product');
    
        Db::getInstance()->update('product', array('custom_field'=> pSQL(Tools::getValue('custom_field')),'`id_product` = ' .$id_product);
                $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();

    }
Link to comment
Share on other sites

Thx so much for reply !

 

i add ) at end of line   ....$id_product));  too

 

now when i try to update i got this :

 

Unknown column '0' in 'field list'
 

UPDATE `ps_product` SET `custom_field` = '',`0` = '`id_product` = 1'

to be sure of what we are talking about , i want this in sql :

 

UPDATE ps_product SET custom_field ='hello' WHERE id_product =1

Link to comment
Share on other sites

Missed to add a ")". Try now:

public function hookActionProductUpdate($params)
    {
        $id_product = (int)Tools::getValue('id_product');
    
        Db::getInstance()->update('product', array('custom_field'=> pSQL(Tools::getValue('custom_field'))),'`id_product` = ' .$id_product);
    }
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...