Run Magento Code In An External PHP File

Written by Zak on November 25, 2016

In this post I’m going to explain a nifty (and simple) little trick for accessing Magento functionality from an external PHP script. This is something I use all the time when developing Magento sites, and hopefully you will find it useful too!

So, what can you use this for?

There are plenty of applications for this, here are some of the common tasks I use it for…

  • A quick way to test models and collections when developing a site
  • Writing simple scripts for creating a large number of categories, cms pages, static blocks etc… Strictly speaking, the correct way to do this is with a module install script however sometimes a shortcut is more appropriate
  • Manually triggering a cron task or event observer in a module in order to test it
  • Displaying Magento content in another platform or website

 

Great! How do you do it?

It’s surprisingly easy, just 3 neat little lines…

The example above assumes the script is placed in the root of your Magento install. If you’re including this elsewhere you will have to update ‘app/Mage.php’ with the correct path to that file.

At Big Eye Deers, we have a vast amoutn of experience in both Magento eCommerce and bespoke WordPress web design and web development. Our technical director, Zak, is Magento certified and we keep up to date with the latest trends in the digital world to ensure we are always giving our clients the best solutions out there.

If you do have any questions about running magento code in an external PHP file or about your current site or an upcoming project, feel free to get in touch and we would be more than happy to help out! Head to our contact page for the details to get in touch with the team in either our Cardiff or Cornwall offices!