Run Magento Code Externally

If you're working with Magento often you'll find sometimes you need to use Magento functions and classes outside of the Magento platform. This can be easily achieved with the following lines of code.

<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

// Now you can run ANY Magento code you want

// Change 12 to the ID of the product you want to load
$_product = Mage::getModel('catalog/product')->load(12);

echo $_product->getName();

This was only a quick post but hopefully it will be useful to someone.

Here's an example how to get the site navigation.

<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

$_layout  = Mage::getSingleton('core/layout');
$_block   = $_layout->createBlock('catalog/navigation')->setTemplate('catalog/navigation/left.phtml');

echo $_block->toHtml();

11 thoughts on “Run Magento Code Externally”

  • ziomo

    hello ? how to execute this script ? i make a php file, load in magento root category, run it and nothing happend...

    Reply
  • BT

    Create a file in your Magento root and add the following:

    Reply
  • handoyo

    Hi,thanks for the tutorials. I got a question beyond this tutorial. I'm trying to create a php file that run automatically when the web was loaded. Do you mind to teach me how to achieve it? Thanks a lot.. :d

    Reply
  • yusuff

    hai,, give any idea for create static magento page externally.. i want to display menu bars... please help me...

    Reply
  • Max

    Thanks a million for your job. But it is echoing the english menu, though the default menu is german. Please what do i change to echo the default menu which is german.

    Best Regards

    Reply
  • Matt Johnson

    Could someone give a quick example on how you would use this code with the block "catalog/product_list"

    No matter what I do it tells me that there are no products.

    Reply
  • BT

    You will need to specifically set the product collection on the list block. For example:

    Reply
  • sol

    For the first example about gettting the product I wanted, it works. But for the second example I get just empty page. My magento installation is at magento-learn/. I placed the external file at the server root outside magento-learn folder. Why I do not get the site navigations? I am using magento version 7.

    Reply
  • Richard

    I need to do a query to order database and get the status for specific order ID. Then change the status.

    Any help please?

    Reply
  • Laila

    Nice "straight-to-the-point" article.
    Although I have a question on the best way to mass update products from an external app. Is magmi the only option ? Indeed, doing mass save of products can take a very long time and stress the store ..?
    Your thoughts on the subject are appreciated ! :)

    Reply
    • BT

      The quickest way is to update the database directly. This requires a good understanding of how the Magento database works. If you're confident you can do it, you can update 1000 products in roughly a second. You also need to ensure that you update the index tables (if required for the type of update you're doing). I have done this on several sites to great affect, allowing me to update 10,000 product stock records (and indexes) in less than a minute.

      Reply
Leave a Reply
Post your comment

FishPig Ltd