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();

9 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:

    <?php

    require_once('app/Mage.php'); //Path to Magento
    umask(0);
    Mage::app(); // Change default to whatever store you want to run

    echo "it's worked!";

    ?>

    If the string "It's Worked!" is displayed then it has all worked fine

    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:

    <?php

    $products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', 1);

    $block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list')
    ->setTemplate('catalog/product/list.phtml')
    ->setCollection($products);

    echo $block->toHtml();

    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
Leave a Reply
Post your comment

FishPig Ltd