Run Magento Code Externally

piaoling  2011-06-23 18:59:51

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.

?
1
2
3
4
5
6
7
8
9
10
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.

?
1
2
3
4
5
6
7
8
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();

This post was posted in General

类别 :  magento(258)  |  浏览(3349)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: