Jump to content

Add Few Lines After Action


Recommended Posts

Greetings,
I would like to perform PHP/SQL actions after saving a product sheet, so I should hook into the actionProductUpdate action.

Reading through the various discussions here and elsewhere, I've only found the solution via module, while I'd rather create a simple override to add a few lines of code to the actionProductUpdate function.

It is feasible?

Share this post


Link to post
Share on other sites

Posted (edited)

I prefer the option of using a module. You can install the module on other prestashops, or you can disable the function by just deactivating the module.

But if it's something very specific to that shop, maybe it's more ok to do direct override.

Edited by Ress (see edit history)

Share this post


Link to post
Share on other sites

Can you be specific?
What do you want to add?
Edit the update hook in ./classes/Product.php, or add your own there.

 

public function update($null_values = false)
    {
        $return = parent::update($null_values);
        $this->setGroupReduction();

        // Sync stock Reference, EAN13 and UPC
        if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && StockAvailable::dependsOnStock($this->id, Context::getContext()->shop->id)) {
            Db::getInstance()->update('stock', array(
                'reference' => pSQL($this->reference),
                'ean13' => pSQL($this->ean13),
                'isbn' => pSQL($this->isbn),
                'upc' => pSQL($this->upc),
            ), 'id_product = ' . (int) $this->id . ' AND id_product_attribute = 0');
        }

        Hook::exec('actionProductSave', array('id_product' => (int) $this->id, 'product' => $this));
        Hook::exec('actionProductUpdate', array('id_product' => (int) $this->id, 'product' => $this));
        if ($this->getType() == Product::PTYPE_VIRTUAL && $this->active && !Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE')) {
            Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', '1');
        }

        return $return;
    }

 

 

  • Like 1

Share this post


Link to post
Share on other sites

I'd agree that doing it via a module is better than editing core code. Doesn't have to be anything fancy. This would do it 🤷‍♀️

<?php   
if (!defined('_PS_VERSION_'))
  exit;
  
 class CustomAfterProductUpdate extends Module
{
	public function __construct()
  {
    $this->name = 'customafterproductupdate';
    $this->tab = 'others';
    $this->version = '0.0.1';
    $this->author = 'John Doe';
    $this->need_instance = 0;
    $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); 
    parent::__construct();
    $this->displayName = $this->l('Custom After Product Update');
    $this->description = $this->l('Run custom code after product update');
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
 
  }
  
  public function install()
  {  
      if (!parent::install()
      || !$this->registerHook('actionProductUpdate')
  )
      return false;
    return true;
  }

  public function uninstall()
  {
    if (!parent::uninstall())
      return false;
    return true;
  }

  public function hookActionProductUpdate($params)
  {
    /* YOUR CODE HERE */
  }

}

In the hookActionProductUpdate method you can access product id with $params['id_product'] and the full Product object with $params['product']

Share this post


Link to post
Share on other sites

Hi sir and thanks for your reply, but as I specified in the first post, I would like to run these few lines of code without going through the module.
What I would like to do I would like to do it via an override.

Share this post


Link to post
Share on other sites

Posted (edited)

And why do you want to go with your head against the wall?
By overriding you can get many errors after upgrade.

@lordignus gave you the full code of the module.

Can you tell us what exactly you want to save in the product database after the Update event?

Edited by knacky (see edit history)

Share this post


Link to post
Share on other sites

Hooks before and after the update can also be used.

public function generateRandomString($length = 10) 
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
                              
public function hookActionObjectProductUpdateAfter($object)
{
	$idProduct = $object['object']->id;
	/* your code here */

 	/* SAMPLE, update reference and deactive */
	Db::getInstance()->update('product', array('reference' => 'ABCDEFG', 'active' => (int)'0'), 'id_product' => (int)$idproduct);
	Db::getInstance()->update('product_shop', array('active' => (int)'0'), 'id_product' => (int)$idproduct);
}

public function hookActionObjectProductUpdateBefore($object)
{
	$idProduct = $object['object']->id;
	/* your code here */

	/* SAMPLE, update reference (generated automatically) and deactive */
	Db::getInstance()->update('product', array('reference' => $this->generateRandomString(), 'active' => (int)'0'), 'id_product' => (int)$idproduct);
	Db::getInstance()->update('product_shop', array('active' => (int)'0'), 'id_product' => (int)$idproduct);
}

 

Share this post


Link to post
Share on other sites

Posted (edited)

And why didn't you write it in the first question?
You should already have the solution ready.
This is how we write codes for you here and nothing like that.
In my example, you are clearly shown how to solve it.

If you don't know how to create a module, or you don't know the basics of PHP, please enter a request in the JOB section.

It's a 30 minute job for a programmer to create a module.

Your table: ps_my_table_name

public function hookActionObjectProductUpdateAfter($object)
{
	$idProduct = $object['object']->id;
	$idEmployee = $this->context->employee->id;
	
	if ($idEmployee && $idProduct){
		$employee = new Employee((int)$idEmployee);
		$getEmployeeName = $employee->firstname.' '.$employee->lasname;
		Db::getInstance()->insert('my_table_name', array('id_product' => (int)$idProduct, 'id_employee' => $idEmployee, 'employee_name' => pSQL($getEmployeeName), 'date_upd' => pSQL(date('Y-m-d H:i:s')));
	}
}

 

Edited by knacky (see edit history)

Share this post


Link to post
Share on other sites

19 hours ago, knacky said:

And why didn't you write it in the first question?
You should already have the solution ready.
This is how we write codes for you here and nothing like that.
In my example, you are clearly shown how to solve it.

If you don't know how to create a module, or you don't know the basics of PHP, please enter a request in the JOB section.

It's a 30 minute job for a programmer to create a module.

Your table: ps_my_table_name

public function hookActionObjectProductUpdateAfter($object)
{
	$idProduct = $object['object']->id;
	$idEmployee = $this->context->employee->id;
	
	if ($idEmployee && $idProduct){
		$employee = new Employee((int)$idEmployee);
		$getEmployeeName = $employee->firstname.' '.$employee->lasname;
		Db::getInstance()->insert('my_table_name', array('id_product' => (int)$idProduct, 'id_employee' => $idEmployee, 'employee_name' => pSQL($getEmployeeName), 'date_upd' => pSQL(date('Y-m-d H:i:s')));
	}
}

 

I did not write it because if you had told me how to create an override on that action, then I knew how to proceed, I would not have expected you to write the code for me.

I have already created other modules and I'm a PHP programmer, so that's not the problem, the point is just that this time I didn't want to use a module but an override, also to gain experience, but as far as I understand I will not receive any answers about it, as you continue to insist on creating a module.

Share this post


Link to post
Share on other sites

I wrote you the reason why no override.
It is not well maintainable between Prestashop versions and causes errors after upgrading Prestashop.

I also showed you where to put some of your code in the override.

Share this post


Link to post
Share on other sites

28 minutes ago, knacky said:

I wrote you the reason why no override.
It is not well maintainable between Prestashop versions and causes errors after upgrading Prestashop.

I also showed you where to put some of your code in the override.

If I understand correctly, writing an override means copying the entire code, can't just write the extra lines..?

Share this post


Link to post
Share on other sites

21 hours ago, knacky said:

File ./override/classes/Product.php

obrazek.thumb.png.1073ca3148dccf4c36d06f3698afa8af.png

I mean that the update function must be copied before adding custom lines.
I can't just tell PrestaShop, run this code at the end or at the beginning, without first copying the entire code..

And if the code changes during an update, I'll have to manually intervene.

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

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