Opencart – ‘The Twig_Autoloader class is deprecated…’ error

I was contacted by a client last week who was getting this error message on her site.

It appears on Opencart 3.x up to, and is caused by an older version of the Twig template engine not playing nicely with PHP 8. Thus the problem often occurs if your hosting company has upgraded your PHP version without telling you. Searching for the error mainly finds a number of sites with the same issue, plus a number of threads on the (sadly rather unhelpful and not uncommonly abusive) Opencart forums, none of which have what I think is a clear answer. So, for the benefit of others, here are your options if this happens to you:

Update your Opencart version

This is the best option. Upgrading from 3.x to a newer version is relatively straightforward and documented elsewhere.

Revert to an earlier version of PHP

This is good for a quick fix, and is generally done from your hosting Control Panel. If your hosting provides cPanel, then it’s accessed via MultiPHPManager. Find the affected domain, and change the PHP version from 8 to 7.4. Apply the change and your errors should have disappeared.

To restate, this is just a temporary fix as at some point in the future your hosting company may remove the option to run PHP7.

Manually update files on the server

At this point all the purists will be throwing their hands in the air and listing lots of reasons why this is bad idea. However, we live in the real world, and it may be you have multiple hard coded customisations on your install, and you really don’t want to take any risks. If this is the case, read on, this is what I did:

  1. Take whatever backups of your filesystem and database you need to sleep soundly
  2. Download a copy for the latest Opencart ( at the time of writing) to your PC
  3. From your new download, upload the whole of the system folder on top of your current version.
  4. Similarly, upload the catalog/controller/event/theme.php file on top of your current version
  5. If you currently have your storage folder outside your Opencart directory, edit config.php and admin/config.php to set the storage folder back to ...system/storage withhin your Opencart directory

And that’s it. You’ll want to run your own checks of course, but that worked for me.

Opencart will prompt your to move your storage folder when you log in to admin.

If for whatever reason you haven’t uploaded the system folder on top of the existing one, but deleted the old one instead, then you will get a different error complaining about Openbay. This is because Openbay support was removed in OC3.0.3.6. Assuming you don’t need this then find this line in both catalog/controller/startup/startup.php and admin/controller/startup/startup.php:

registry->set(‘openbay’, new Openbay($this->registry));

and delete it or comment it out.


Useful? Interesting? Leave me a comment

I've yet to find a way of allowing code snippets to be pasted into Wordpress comments - so if you're trying to do this you'd be better off using the contact form.