Is It Possible To Integrate CakePHP and Magento?

Absolutely! Magento is built as an e-commerce shopping cart solution first, and a CMS (Content Management System) second. A client wanted to keep their existing CakePHP website, and take advantage of the power of the Magento shopping cart. This tutorial will help you if you want to use some elements of Magento such as a product page driven by content in CakePHP. I have seen code out there to put the folder into the /app/webroot directory of CakePHP but seemed to give unnecessary headaches with htaccess rewrites and so on. I decided to put it into the root hosting folder along with CakePHP’s app, cake, and vendors folders. I named my folder with all of the Magento files in it “store”. The CakePHP and Magento database tables are in the same MySQL database with the same database user. Here are the following things you’ll need to do to get this up and running:

CakePHP

File: /app/app_controller.php
Location To Place Code: Top of file, above the class AppController extends Controller
Code:

require_once 'store/app/Mage.php';
umask(0);

Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));

Notes: You’ll want to replace the first line of code with the location of your Mage.php file. Mine happens to be at store/app because I made a root folder of store.

Magento

File: {folder_name_magento_is_inside}/app/Mage.php
Code Line Reference: 27
Old Code:

define('DS', DIRECTORY_SEPARATOR);

New Code:

if (!defined('DS')){define('DS', DIRECTORY_SEPARATOR); }

Notes: You need to replace the code around line 27 that defines the directory separator. This is because CakePHP also defines DS so it will throw an error at you telling you that DS is already defined.


File:
{folder_name_magento_is_inside}/app/code/core/Mage/Core/functions.php

Code Line Reference: 94
Old Code:

function __()
{
 return Mage::app()->getTranslator()->translate(func_get_args());
}

New Code:

if (!function_exists('__')){
function __()
{
 return Mage::app()->getTranslator()->translate(func_get_args());
}
}

Notes: You need to replace the code around line 94 that defines the translation function. This is because CakePHP also defines a function to translate strings, so it throws you an error telling you its been declared already.

 

Conclusion:
Once you’ve completed the above steps, you will be able to use any of the functionality of Magento right inside your CakePHP files. So for example, you can run the following code to print out a product name:

$productId = 3;
$magento_product = Mage::getModel('catalog/product')->load($productId);

$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($magento_product);
$simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();

echo "<a href='#'>".$magento_product->getName()."</a><br />";
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Share