Jump to content

Help inserting and updating database from hook


cfr55

Recommended Posts

I am doing a new module and I want to know how to update and insert values in a table when I save the product options in the back office.

I try this but I dont know if the code its ok or where to place it:

Db::getInstance()->update('ps_product_shop', array(
      'customizable' => 2,
    ));

$query = "UPDATE `"._DB_PREFIX_."product` SET customizable=1 WHERE id_product =".$product->id_product.";";
 Db::getInstance()->Execute($query);

I try both in hookdisplayAdminProductsExtra and in hookActionProductUpdate

Link to comment
Share on other sites

Hi,

hookDisplayAdminProductExtra will not work here. Most of the displayHook is for mainly for display purpose (To modify the view of the page).

You can achieve the same using the hookActionProductUpdate function. Refer to the code below.

public function hookActionProductUpdate($params)
{
	if (isset($params['id_product']) && $params['id_product'] != '') {
		$updateSQL = "UPDATE `"._DB_PREFIX_."product` SET customizable=1 WHERE id_product = '".(int) $params['id_product']."'";
		DB::getInstance()->execute($updateSQL);
	}
} 

 

Kindly ensure that hookActionProductUpdate is registered through your module.
 

Link to comment
Share on other sites

En 20/8/2018 a las 7:30 PM, Knowband Plugins dijo:

Hi,

hookDisplayAdminProductExtra will not work here. Most of the displayHook is for mainly for display purpose (To modify the view of the page).

You can achieve the same using the hookActionProductUpdate function. Refer to the code below.


public function hookActionProductUpdate($params)
{
	if (isset($params['id_product']) && $params['id_product'] != '') {
		$updateSQL = "UPDATE `"._DB_PREFIX_."product` SET customizable=1 WHERE id_product = '".(int) $params['id_product']."'";
		DB::getInstance()->execute($updateSQL);
	}
} 

 

Kindly ensure that hookActionProductUpdate is registered through your module.
 

Still not work, I don't know why, but don't update when I push save on admin product template.

I have this exactly code and the hook is resgistered through my module.

 

Link to comment
Share on other sites

hace 50 minutos, PrestaRalph dijo:

How exactly is it registered?

Like this:

in install function

if (!parent::install() || !$this->_installSql() ||
      !$this->registerHook('actionProductUpdate') ||
      !Configuration::updateValue('CUSTOMIZATIONMODULE_NAME', 'module')
    )

Link to comment
Share on other sites

  • 4 months later...
En 20/8/2018 a las 7:30 PM, Knowband Plugins dijo:

Hi,

hookDisplayAdminProductExtra will not work here. Most of the displayHook is for mainly for display purpose (To modify the view of the page).

You can achieve the same using the hookActionProductUpdate function. Refer to the code below.


public function hookActionProductUpdate($params)
{
	if (isset($params['id_product']) && $params['id_product'] != '') {
		$updateSQL = "UPDATE `"._DB_PREFIX_."product` SET customizable=1 WHERE id_product = '".(int) $params['id_product']."'";
		DB::getInstance()->execute($updateSQL);
	}
} 

 

Kindly ensure that hookActionProductUpdate is registered through your module.
 

  public function hookActionProductUpdate($params)
  {

    $sqlInstall = "UPDATE " . _DB_PREFIX_ . "product "
            . "SET customizable = 1";

     $returnSql = Db::getInstance()->execute($sqlInstall);

  }


Doing that set as customizable all products of the table less the product what I am saving.

I dont understand.

Link to comment
Share on other sites

On 1/18/2019 at 5:36 PM, cfr55 said:

  public function hookActionProductUpdate($params)
  {

    $sqlInstall = "UPDATE " . _DB_PREFIX_ . "product "
            . "SET customizable = 1";

     $returnSql = Db::getInstance()->execute($sqlInstall);

  }


Doing that set as customizable all products of the table less the product what I am saving.

I dont understand.

"UPDATE " . _DB_PREFIX_ . "product " . "SET customizable = 1";

Sets all products at customizable=1; But later on the product that you are working on is updated. And as its settings were in memory already and didn't contain customizable=1 it sets that value back.

Link to comment
Share on other sites

  • 5 months later...

Hello, i got the same issue....

I try to insert customizable fields on product update...
But smtg seems to remove my fields from the database just after my script insert it....
(my items are well inserted in the database, i checked this with a query just after the insert)

Any idea how to solve this ?

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