[SOLVED] (opencart v3.x extension creation) is it possible to run update logic on Extension install?
Hi,
I am maintaining a payment extension, and in the new update I'll be publishing there is a necessity to update some configs by rerunning some logic like making an API request and updating the extension config.
This could be achieved by adding this logic in the install() method and tell (leave a note in the release page) the user to go to "Home > Extensions" select "Payments" for extension type and uninstall and reinstall the extension, but I think most users will miss that.
So I was looking for help or some information on how to run that logic right when the user uploads "extenstion.ocmod.zip" file.
And: could you elaborate your idea a bit further?
Currently it's not clear what and why you want to update what when a user installs your extension new?
Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.
I'll start by describing the normal flow of instalation and configuration for the current version:
- a user installs the payment extension for the first time by uploading the extension.ocmod.zip file in the "Home > Extension Installer page";
- as a result, about six payment methods become available for installation/configuration at "Home > Extensions - Payments";
- a user then installs the payment method and clicks the "Edit" button to configure it;
- (this is the important part) when the user configures it and clicks "Save", an API request is made to paymentGateway to register it, and a config is saved to the 'oc_setting' table.
Now imagine the user will update the extension to the new version of the extension:
- a user updates by installing the payment extension for the first time by uploading the extension.ocmod.zip file in the "Home > Extension Installer page";
- the user must uninstall the payment method/s and reinstall it again to make the API request and save the config in order to work with the new update;
The problem with this is that a user may ignore my warning to reinstall the extension, and after updating the extension, it will stop working correctly.
That is why I'd like to run that logic in the background at the point of installation, so that the user only needs to update the extension and be ready to go.
I have read that the install.xml can be used to edit opencart core, and thus allow to add some function calls, but I don't like the idea of messing with the core just for that, actually I would not want to touch it in the first place.
Perhaps you can build a similar feature in your extension so that when the user runs an update, you can call your API endpoint to retrieve the necessary configurations. Alternatively, how about just automatically calling your API endpoint in the background, i.e. at the index function so that the API is automatically called when the extension's page is loaded? Maybe you can add additional checks to prevent unnecessary API calls, such as detecting the version and whether the configurations have been set already before deciding whether to call your API to retrieve new configurations.
Also, most of the extensions on OpenCart usually allow updating without reinstalling.
Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager Pro • Drag & Drop Sort Order
Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.
Means:
(1) every time the extension is installed, this API is called and check are there are updates
(2) same goes when the extension is called AND there are new settings for it (I don't know if that can happen and is important to run that payment extension)
What I do not understand is, when a user makes a new installation (usually only once a time) - why is there a need to check for new settings?
These "new" settings should be already inside the new installation .. or not?
Regarding (2): I hate when an extension is called several times a day in the background just for checking the version - I say to that: "home calling"
Do it (like I do) only once a day AND only when the user has agreed to that process.
Additionally place a button << Check for updates >> in your extension the user can click when HE wants to check for any updates ..
Now for the further process, whenever YOUR API is called and there are updates, you have two possibilities:
#1 automatic update
#2 on users demand (when he click a button after he sees the update message
#1 may be fine for endusers (they have nothing to do anymore and can't forget to update).
Also convinient when the extension do need an update.
#2 has the disadvantage that the user may "forget" to make any update while the extension do need it
A third method could be (see above (1) ) that a message will be displayed (starting page of administration) after the update check, that a new version or news settings are available for your payment extension.
This is one way, another (or additional) possibility could be (because how many times a user is opening the adminstration - I have clients open the administration maybe max. once a week), to send an email (configurable inside your extension) to that user.
But in both cases the user should be notified that an update should be made - together what has changed!
So, whenever the API is called and an update has to be made, the files are then fetched your domain (through the API), unzipped and installed.
The installation is an own process, created by you and your extension!
Inside that function you check existing settings and update if required.
Also files (of your extension) can be changed this way.
And finally - again: inform the user what has changed and why.
What not should be made is, that the update message is displayed - and nothing else (why and what is changing).
And also after the update was made (successfully or not): the user should now what happened in the background.
All the functions and logis has to be deployed by you - the creator of the extension.
Going this way it's a transparent process for the user and understandable.
Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.
The extension already has logic in place to check for updates when the user accesses it, by showing the changelog and a button to download the extension.
To give an example in another platform, in Prestashop there is an upgrade folder where you can add scripts that will check the versions (currently installed and to be installed) and when you upload the module, it runs those scripts if necessary. This is normally used when altering existing table columns of the module.
Although I don’t need to modify a table, the underlying requirement is similar.
softmoke's suggestion seem good enough "Alternatively, how about just automatically calling your API endpoint in the background, i.e. at the index function so that the API is automatically called when the extension's page is loaded?"
That could work, but I was aiming for something that executes when the .ocmod.zip file is installed, though if not possible ill stick with softmonke's suggestion.
Also, I have been updating the extension by uploading the new version at "Home > Extension Installer" (view image in attachments), is that not correct?
Nothing else I wrote above.josesoares wrote: ↑Fri Dec 06, 2024 4:49 pmsoftmoke's suggestion seem good enough "Alternatively, how about just automatically calling your API endpoint in the background, i.e. at the index function so that the API is automatically called when the extension's page is loaded?"
Everytime you install an extension (and a payment method is an extension) the system installer checks if your extension has a function called install().josesoares wrote: ↑Fri Dec 06, 2024 4:49 pmThat could work, but I was aiming for something that executes when the .ocmod.zip file is installed, though if not possible ill stick with softmonke's suggestion.
And if existing, it will be called and everything you have inside this function (e.g. calling another controller, a model or even your API) is executed.
Simple.
As wrote above, everything is an extension and has therefore to be installed as extension.josesoares wrote: ↑Fri Dec 06, 2024 4:49 pmAlso, I have been updating the extension by uploading the new version at "Home > Extension Installer" (view image in attachments), is that not correct?
The more, because OpenCart 4.x allows installations only this way!
One last note, you wrote:
Well quite fine .. but (!!), the payment method is called only from the catalog (frontend) - also when the admin use it.The extension already has logic in place to check for updates when the user accesses it, by showing the changelog and a button to download the extension.
So how do you want to show the user that an update has to be made?
Or otherwise, when the extension is called in the catalog - you cannot make any administration tasks ..
Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.
Read the documentation https://docs.opencart.com/en-gb/developer/module/
You have to write an interface. This gets installed by Opencart using the install button. This is then separately installed by the user which creates any tables and there you have options to save settings. When you install and upgrade, these settings are not lost, since you do not uninstall the module.
Add an upgrade button to your own interface. Nothing to do with Opencart Install function.
Opencart Upload and Install SHOULD NOT INSTALL YOUR MODULE SETTINGS, your module should have an install, uninstall, save and upgrade buttons.
You may not need to modify a table, but ideally you should create your own tables with your own settings and the user should be able to delete your tables and settings (as well as disable your module without uninstalling)
On your upgrade, why not edit the colour of the upgrade button to red and add a warning that after the settings are upgraded and the new version matches the settings, that note can be added to your interface settings and using your interface css.
That is it. It is very simple, very secure, very controllable.
What you are misunderstanding is that Opencart upgrades in the same way as Wordpress and it doesnt.
This is not a flaw in Opencart it is better.
DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.
https://www.youtube.com/watch?v=zXIxDoCRc84
But I only verified that it is executed when installing in the payments (check image in attachment).
Thought the only way to tell the users an update is available was in admin extension configuration page.OSWorX wrote: ↑Fri Dec 06, 2024 5:42 pmWell quite fine .. but (!!), the payment method is called only from the catalog (frontend) - also when the admin use it.
So how do you want to show the user that an update has to be made?
Or otherwise, when the extension is called in the catalog - you cannot make any administration tasks ..
I thought it was not, since each platform has its way of going about it, so I asked how to.
No that takes you to YOUR INTERFACEjosesoares wrote: ↑Fri Dec 06, 2024 6:34 pmThought the only way to tell the users an update is available was in admin extension configuration page.
It AFTER you click that your interface kicks in.
When you click INSTALL - that runs the install function on in YOUR INTERFACE CONTROLLER that OSWorX explains above.
So AFTER you go into that payment module there should be buttons to the right and THERE you you add an UPGRADE button.
Whether you have to install using ocmod install a new interface or not - it is within the actual INTERFACE PAGE that runs whatever it is you want to do. For example to add a warning that there is a new upgrade available (which requires an api), or you must refresh your settings to complete the installation (which does not require an api just a check whether the settings matches the new installed version). Another method is to have a button that runs a check whether there is a new version or not.
DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.
https://www.youtube.com/watch?v=zXIxDoCRc84
Not completely true .. because you could call a cron job (defined again by your extension) which calls YOUR API once a day do see if there is an update.josesoares wrote: ↑Fri Dec 06, 2024 7:10 pmThought the only way to tell the users an update is available was in admin extension configuration page.
That's an easy task.
More work is, then to inform the user that an update is available.
But this could be done also ..
And you should not forget (!!) - wrote that already above - not every shop owner (or admin) is opening the administration page every day.
But you extension seems to need this update - what happens if your extension does not get the update?
Will it work seemless again further, or will it miss some (important) features?
Finally: we here (and I really mean all who answered until now) will not teach you how to develop an extension for OpenCart.
I would say, we helped you in many ways - but as it seems you should hire an OpenCart Professional!
Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.
You did explain concepts of Opencart that I was struggling to grasp.
I'll do just like you suggested and add the upgrade button in the top right corner of the extension config interface.
And ill look into the cron job, since it's the most reliable way to make sure the users know a new update is available.
Once again, thank you.
Users browsing this forum: No registered users and 28 guests