Magento: How to run Magento code in an external website?

Suppose you have a Magento shop installed. You also have another php installation in your server (may it be wordpress, joomla, drupal, or any other php code system).

Now, you want to get Magento shop data in your external website which is not Magento. So, how to do this?

It’s simple. You just need to include ‘app/Mage.php‘ of your Magento installation into your new system code. Then, you can code as you do in Magento.

In the example below, I will be fetching Product and Customer data from Magento shop in my external site.


/**
 * Include Mage.php of your Magento installation 
 */
require_once("../magento-shop/app/Mage.php");
umask(0);

/**
 * Initialize Magento
 */
Mage::app('default');

/**
 * Getting Product Data
 * Suppose the product ID is '52'
 */
$productId = 52;
$product = Mage::getModel('catalog/product')->load($productId);

// echo "<pre>"; print_r($product->getData()); exit;

/**
 * Print name, type, price and quantity of the product
 */
echo "Name: ".$product->getName()."<br />";
echo "Type: ".$product->getStockItem()->getTypeId()."<br />";
echo "Price: ".$product->getFinalPrice()."<br />";
echo "Quantity available: ".(int)$product->getStockItem()->getQty()."<br />";

/**
 * Get all products
 */
$allProducts = Mage::getModel('catalog/product')->getCollection();


/**
 * Getting Customer Data
 * Suppose the customer email is 'test@example.com'
 * Website id = 1
 */
$customerEmail = "test@example.com";
$websiteId = 1;
$customer = Mage::getModel('customer/customer')->setWebsiteId($websiteId)->loadByEmail($customerEmail);

// echo "<pre>"; print_r($customer->getData()); exit;

echo "Customer Name: ".$customer->getFirstname().' '.$customer->getLastname();

/**
 * Get all customers
 */
$allCustomers = Mage::getModel('customer/customer')->getCollection();

Hope this helps. Thanks.