All about Magento E-commerce Store.......MagentoForum: Run Magento Code Outside of Magento

Tuesday, July 12, 2011

Run Magento Code Outside of Magento

Hi All -
Finally time for a new post! (Something I'm surprised I haven't covered yet).
This post will inform you on how to run Magento code outside of Magento. All you need is to have access to Magento's 'app/Mage.php' file.
This will be handy code for a few things:

  • Integration with 3rd party items – shared sessions, or just shared themes
  • Ajax calls – although not the preferred solutions for Ajax calls, it is a quick and easy one

To expand on these ideas a bit more:
Integration:
-You can use this code to output HTML that is outputted in Magento anywhere. You might want to integrate Wordpress and steal the navigation from Magento, for instance. You might want to share sessions and users between your CMS and Magento (and even share the databases). This can help you get started on doing that.
Ajax:
-Because you can use this code to output any block/template, you can use it for Ajax calls in your Magento build. You build your block and template (and any other needed objects) as usual and output them via this code.
Here is a sample:
01.<?php
02.require_once 'app/Mage.php';
03.umask(0);
04./* not Mage::run(); */
05.Mage::app('default');
06. 
07.// get layout object
08.$layout = Mage::getSingleton('core/layout');
09. 
10.//get block object
11.$block $layout->createBlock('catalog/product_ajax');
12. 
13./* choose whatever category ID you want */
14.$block->setCategoryId(3);
15.$block->setTemplate('catalog/product/ajaxevents.phtml');
16. 
17.echo $block->renderView();
18. 
19.?>
We can see in this block of code that we are grabbing the custom block 'catalog/product_ajax'.
This is simply a block that grabs a product collection. In this case, we are able to set the category id to 3. (See the post on custom blocks to help you get a feel for what this might look like).
This block is then setting the .phtml template to 'ajaxevents.phtml' and rendering the view. You hopefully can see how this would be useful for Ajax calls.
Other code that might help you along your way:
From php architect's book (might be outdated!!! We haven't tested this particular code):
1.include('app/Mage.php');
2.Mage::App('base'); //might be "default"
3. 
4.$customer = Mage::getModel('customer/customer');
5.$customer->loadByEmail('some@email.address'); /* need a users email address */
6.$session = Mage::getSingleton('customer/session');
7.$session->start();
Here is some session code that will grab cart information. Notice that this code doesn't start a session:
01.<?php
02. 
03.$mageFilename 'app/Mage.php';
04.require_once $mageFilename;
05. 
06.umask(0);
07.Mage::app();
08. 
09./* Magento uses different sessions for 'frontend' and 'adminhtml' */
10.Mage::getSingleton('core/session'array('name'=>'frontend'));
11. 
12.// $cart = Mage::getSingleton('checkout/cart')->getItemsCount();
13.// $cart = Mage::helper('checkout/cart')->getItemsCount();
14. 
15.$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
16. 
17.echo 'cart items count: ' $cart;
18. 
19.?>
Yet another block of code with some interested stuff:
01.require_once 'app/Mage.php';
02.umask(0);
03. 
04.$app = Mage::app('default');
05. 
06./* Init User Session */
07.$session = Mage::getSingleton('customer/session',array('name'=>'frontend'));
08. 
09.if ($session->isLoggedIn()) {
10./* do something if logged in */
11.else {
12./* do something else if not logged in */
13.}

No comments:

Post a Comment