Jump to content

Fast stock update together with mailalerts module


Recommended Posts



I wrote a custom .php file which I can use to easily update the stock in prestashop. This php file is simply a web page which has a input where I can enter the EAN13 code of any product. When I hit enter the quantity of the product is adjusted (increased/decreased). Whenever I receive a lot of say over 100 products, I just use a barcode scanner to scan the EAN13 codes and the stock is updated in a couple of minutes (instead of wasting half of hour in backoffice).

The problem is that the module mailalerts does not generate the necessary e-mails to inform the customers that the product is once again back in stock. This is because mailalerts hooks in prestahsop using UpdateProduct hook and UpdateProductAttribute hook and my .php file is a separate program..


I'm not sure which is the best way to do it?

  1. Can I call/invoke mailalerts from my .php file? (my .php is not entirely isolated from prestahsop, it uses a little bit of prestashop code; see bellow)
  2. Should I transform my .php file into a prestashop module; then access it using backoffice and add the two hooks in my module so mailalerts will hook in it?
  3. Other way I should approach the issue?

Any help is appreciated.

PS. I'm not asking for code but for a sensible approach to the issue so I can focus in the right direction.




How my custom php file starts




define('_PS_ADMIN_DIR_', getcwd());

define('PS_ADMIN_DIR', _PS_ADMIN_DIR_); // Retro-compatibility




@ini_set('display_errors', 'off');

Link to comment
Share on other sites

Once you've finished loading


you've got all of PrestaShop ready and waiting, even if you don't use it.


So you certainly could trigger the UpdateProduct hook manually by adding one or two lines to your code.


That said, I would choose path #2 - make it a full-fledged Module. The basic framework for the simple, admin-only module like you describe shouldn't be more than hundred lines total, including comments. I am a tinkerer at heart, so I'm constantly refining my approaches and adding new functionality. A Modules' ability to move it easily from install to install ( dev -> test -> staging -> prod ) is a big plus for me.



Link to comment
Share on other sites

At the beginning I think you are suggesting me to go with the first approach. I think there are too many things I don't know about. I think I have to use a tpl file, smarty and other things and tools before I can have a hook that will manage to call mailalerts.


The you suggest #2 I would do this but I have one concern. If I try to make my .php file a module, then I go to backoffice -> modules -> my module -> configure and I type a serial code and hit enter, prestashop will go a step back to modules page and I would have to click configure again before I can enter the next serial code (not practical). Is it possible to make a button which will "save and stay on same page"?



Link to comment
Share on other sites

Yes, you can make a 'save and stay' button - but you'll need to do a tiny bit more coding. My trick was to make the HTML form action point back to the configuration screen by taking the current URL and stripping it of any stuff that I may have added. Then I used the standard _processPost() and _setConfigurationForm() methods to handle updating the values and displaying the results.


If you're more comfortable with javascript and AJAX operations with PrestaShop, you may want to use those instead and bypass needing to code the 'save and stay' button/form.



Link to comment
Share on other sites

I looked in other modules and I think I can make the button to stay on the same page by writing something like (copied form CMS module):

<a href="'.$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&addBlockCMS"><img src="'._PS_ADMIN_IMG_.'add.gif" alt="" /> '.$this->l('Add a new CMS block').'</a>

but I must say your trick sounds nice and simple. Can you write here a example or maybe upload the file/module where you used this trick?


Later edit: I've done this tutorial http://www.bazingadesigns.com/en/blog/tutorials/how-to-write-a-prestashop-module-tutorial-making-of-kinky-slider-part-1 and surprisingly when I press the save button to save the module's configuration points to the same page (exactly what I need).


For now I'm OK, I just need to learn a little and code.


Thanks Terragg for your suggestions.


PS. I will stick with php and whatever Prestashop is using. Don't know much about javascript and AJAX.

Edited by Marius86 (see edit history)
Link to comment
Share on other sites

I've bumped into some problems ...

1) I was using sessions to save the processed bar-codes and display them in a table so the user can track down if something went wrong. I was using:

//read history
$history = $_SESSION['se_history'];
//save history
$_SESSION['se_history'] = $history;

This worked perfectly in a .php file but it doesn't work in a prestahsop module. What can I do instead?


2) In the .php file I was using a code to set the focus on a input where the barcode goes:


function setFocus()
{ document.getElementById("my_input").focus(); }
<body onload="setFocus()">

I was inserting the code in the header and near the body tag but now I don't know how to "hook" this code in the proper location.


Anybody any ideas?




For moderators: Can you please change the title of the thread to "Developing a module for fast&easy stock update using the EAN13 barcodes" Thanks

Edited by Marius86 (see edit history)
Link to comment
Share on other sites

  • 5 weeks later...



I'm very close to finishing this module. I have just one last issue.

Because I am updating the stock of the products I need to include two hooks in my codding:



I wrote the first one - it is simple; but I don't know what do I need to pass in the $object variable.

I looked in the AdminProducts.php file and the code prior to the hook is:

$object = new $this->className($id);

I don't understand that line. I can't find the className() to see what is does.


Can anybody help?




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