Run Magento Code Outside of Magento
来源:互联网 发布:淘宝刷客怎么判刑2016 编辑:程序博客网 时间:2024/06/05 08:30
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:
- <?php
- require_once ‘app/Mage.php’;
- umask(0);
- /* not Mage::run(); */
- Mage::app(‘default’);
- // get layout object
- $layout = Mage::getSingleton(‘core/layout’);
- //get block object
- $block = $layout->createBlock(‘catalog/product_ajax’);
- /* choose whatever category ID you want */
- $block->setCategoryId(3);
- $block->setTemplate(‘catalog/product/ajaxevents.phtml’);
- echo $block->renderView();
- ?>
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):
- include(‘app/Mage.php’);
- Mage::App(‘base’); //might be ”default”
- $customer = Mage::getModel(‘customer/customer’);
- $customer->loadByEmail(’some@email.address’); /* need a users email address */
- $session = Mage::getSingleton(‘customer/session’);
- $session->start();
Here is some session code that will grab cart information. Notice that this code doesn’t start a session:
- <?php
- $mageFilename = ‘app/Mage.php’;
- require_once $mageFilename;
- umask(0);
- Mage::app();
- /* Magento uses different sessions for ’frontend’ and ’adminhtml’ */
- Mage::getSingleton(‘core/session’, array(‘name’=>‘frontend’));
- // $cart = Mage::getSingleton(’checkout/cart’)->getItemsCount();
- // $cart = Mage::helper(’checkout/cart’)->getItemsCount();
- $cart = Mage::helper(‘checkout/cart’)->getCart()->getItemsCount();
- echo ‘cart items count: ’ . $cart;
- ?>
Yet another block of code with some interested stuff:
- require_once ‘app/Mage.php’;
- umask(0);
- $app = Mage::app(‘default’);
- /* Init User Session */
- $session = Mage::getSingleton(‘customer/session’, array(‘name’=>‘frontend’));
- if ($session->isLoggedIn()) {
- /* do something if logged in */
- } else {
- /* do something else if not logged in */
- }
- Run Magento Code Outside of Magento
- make magento run faster
- Magento
- magento
- Magento Code Snippets
- Magento All kinds of Theme
- Markup Tags of magento CMS
- magento -- 通过Shipment Code找到Order信息
- magento促销---优惠码设定 Coupon Code
- magento中在.htaccess设置website code
- magento, 根据attribute code获取attribute value
- magento -- magento站点优化
- magento -- magento站点优化
- magento -- Magento事件一览表
- magento -- magento站点优化
- magento -- ebay收购magento
- Improve the Speed of Your Magento Site
- magento out of stock and disable items
- 整理了下电脑,发现好多以前学习的东东,现在要Delete掉。555555~~
- 委托事件概述(简单学习)
- C#/asp.net下如果显示保留小数位数及百分号的解决方法
- 认真做事真的是特别难
- C实现MySQL数据库操作
- Run Magento Code Outside of Magento
- Creating an Ext JS grid using the Zend Framework
- Magento与OpenERP的集成模块
- 如何创建一个水晶报表用户自定义函数库 UFls (Creating a Crystal Reports Custom Function Library)
- C++虚函数表解析
- Demon de Laplace
- 注意不可见的字符!
- 找回ext3文件系统的Linux下误删的数据
- QuickCSharp框架开发(1)-系统表结构设计