1. Magento: Get and set variables in session
To set a Magento session variable:
$myValue = 'Hello World';
Mage::getSingleton('core/session')->setMyValue($myValue);
To Retrieve:
$myValue = '';
$myValue=Mage::getSingleton('core/session')->getMyValue();
To Unset:
Mage::getSingleton('core/session')->unsMyValue();
或者
/* Core Session */
Mage::getSingleton('core/session')->setYourVariable('data');
$Data = Mage::getSingleton('core/session')->getYourVariable();
/* Customer Session */
Mage::getSingleton('customer/session')->setYourVariable('data');
$Data = Mage::getSingleton('customer/session')->getYourVariable();
/* Admin Session */
Mage::getSingleton('admin/session')->setYourVariable('data');
$Data = Mage::getSingleton('admin/session')->getYourVariable();
2. Magento’s Registry Pattern
The three registry methods are
Mage::register
Mage::unregister
Mage::registry
The register
method is how you set a global-like variable.
Mage::register('some_name', $var);
Then, later in the request execution, (from any method), you can fetch your variable back out
$my_var = Mage::registry('some_name');
Finally, if you want to make you variable unavailable, you can use the unregister
method to remove it from the registry.
Mage::unregister('some_name');
更多参考: http://alanstorm.com/magento_registry_singleton_tutorial
3. Create Global Function In Magento
This code will allow you to add a function that can be called from anywhere within Magento. It extends the helper class
1) Create a file named ‘Mycode.xml’
and copy it to app/etc/modules/
– it should look like this:
<?xml version="1.0"?>
<config>
<modules>
<Mycode_Function>
<active>true</active>
<codePool>local</codePool>
</Mycode_Function>
</modules>
</config>
2) Create the directory app/code/local/Mycode/Function/etc
and then create a file named ‘config.xml’
In it copy:
<?xml version="1.0"?>
<config>
<modules>
<Mycode_Function>
<version>1.0.0</version>
</Mycode_Function>
</modules>
<global>
<helpers>
<function>
<class>Mycode_Function_Helper</class>
</function>
</helpers>
</global>
</config>
3) Create the directory app/code/local/Mycode/Function/Helper
and then create a file named ‘Data.php’
In it copy:
<?php
class Mycode_Function_Helper_Data extends Mage_Core_Helper_Abstract
{
public function test(){
return 'works';
}
}
You can now call this function like so
<?php
echo Mage::helper('function')->test();
?>
来源: http://joe-riggs.com/blog/2011/06/create-global-function-in-magento/
本站相关:
1. 深入理解Magento-第九章-修改、扩展、重写Magento代码
2. magento 模块重写
分享到:
相关推荐
### Magento 1.4.2 简便生成订单函数详解 #### 一、引言 在Magento系统中,特别是1.4.2版本中,处理订单生成的过程相对较为复杂,尤其是在sales模块与checkout模块之间存在着较为紧密的关联。本文将详细介绍如何...
Magento是一款知名的开源电子商务平台,以其强大的功能和高度的可定制性而受到许多电商网站的青睐。然而,由于其复杂的架构,Magento在默认情况下可能会相对较慢,尤其是在处理大量产品和访问量时。为了解决这个问题...
Magento扩展:使用ajax设置会话变量 Magento Extension允许您轻松地为每个ajax设置会话变量。 安装 只需将文件复制并粘贴到您的Magento目录中 用 安装扩展程序后,您可以使用以下URL设置/取消设置Magento会话变量: ...
- **Mage::register()** 和 **Mage::registry()**:用于在全局注册表中存储和获取数据,常用于传递数据到视图层。 - **Mage::helper()**:提供助手类实例,助手类通常包含一些通用函数,如翻译、路径处理等。 - *...
此外,Magento支持层次性定价和目录定价规则,允许设置促销规则来调整价格,无论是基于购物车内容还是目录产品。 在开发方面,Magento插件开发是为平台添加新功能的主要手段。开发者可以通过创建扩展模块来定制...
1. Magento的网站和网店结构:Magento允许创建多个网站(Website),每个网站可以包含一个或多个商店(Store),每个商店可以有多个商店视图(Store View)。这三者之间的关系是层级结构:一个网站包含多个商店,每...
Magento-清理会话扩展概述此扩展名允许您安排Magento会话存储的清理(仅支持文件和数据库)。 当您无法编辑php.ini(以配置会话垃圾收集器)或不想使用计划的bash脚本时,非常方便。兼容性在Magento CE 1.6-1.9上...
10. **CORECONFIGURATION**:系统配置表,存储了Magento的全局配置设置。 11. **WEBSITE/STORE**和**ADMIN**:网站/商店表和管理员表,用于管理多个商店视图和管理员账户。 12. **TAG**, **SYSTEMLAYOUT**, **...
同时,"redisԭʼmagento-1.zip" 很可能是针对将 Redis 集成到 Magento 作为 session 存储的实现或优化的资源包。同样,解压并分析其中的内容,按照说明进行操作,以优化你的 Magento 应用。 总的来说,集成 Redis ...
在学习Magento的过程中,建议逐步实践,先从基础设置和模板修改开始,然后深入到XML配置和模块开发。同时,不断查阅官方文档、社区论坛和教程资源,以保持对最新版本和最佳实践的了解。通过不断的实践和学习,你将...
总而言之,这个“magento模块组件或插件开发教程”旨在帮助开发者建立起对Magento核心机制的深刻理解,从而能够自如地开发和维护自己的模块。无论是新手还是有经验的开发者,都能从中受益匪浅,进一步提升自己的...
8. **代码修改、扩展和重写**:教你如何不改变核心代码的情况下扩展或替换 Magento 功能。 9. **数据操作和数据收集器**:涉及数据的保存、检索和处理过程。 10. **其他开发相关文章**:包括 EAV 数据查询、缓存清理...
- **商店**:每个网站可以包含一个或多个商店,这些商店可以有不同的语言设置和风格配置。 - **商店界面**:针对不同语言的支持,例如一个商店可以同时提供英文和中文界面。 #### 四、Magento的安装与配置 - **...
magento结构和原理是magento框架的核心组成部分,了解magento的结构和原理是开发magento模块和主题的基础。本文将详细介绍magento的文件目录结构、URL路由与分发器、模板调用对应的JS、CSS、图片、重写核心模块等...
在本指南中,读者将了解到如何设置和配置Magento环境,以及如何通过编写PHP代码来扩展和定制Magento的默认行为。由于Magento是基于MVC(模型-视图-控制器)架构设计的,开发者需要了解这一架构模式,以便更好地理解...
3. **上传到Magento根目录**:使用FTP客户端将解压后的文件上传到Magento安装目录的`app/code`或`app/code/community`(取决于插件类型)。 4. **安装和启用**:在Magento后台,进入“系统”>“Magento Connect”>...
Magento-中文开发教程, ...如何使用和设置Cookie&Session Magento中我的账户访问权限判断 Magento时间/时区问题 重新安装 Magento 模块 Magnto获取当前店铺和店铺配置的方法 Magento如何重写或新建后台的页面
9. **配置支付网关和物流插件**:Magento集成了多种支付和物流接口,根据业务需求,配置相应的支付方式和物流解决方案。 通过以上步骤,你就能成功搭建起一个Magento电子商务平台。在后续的运营中,还可以通过...
Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性在电商领域广受青睐。本文将深入解析Magento的目录结构,帮助初学者快速理解Magento的开发流程。 Magento的目录结构设计复杂而有序,它按照MVC...