Magento开发常用方法
这里是我做Magento开发常用到的方法,现在总结出来,后续会把更多有用的方法总结出来。
1.直接操作数据库
查找数据:
$read = Mage::getSingleton("core/resource")->getConnection('core_read');
$sql = "select * from `abc`";
$result = $read->fetchAll($sql); //fetchRow查找一条
增,删,改:
$write = Mage::getSingleton("core/resource")->getConnection('core_write');
$sql = "insert into abc (name)values('hello')";
$write->query($sql);
2.Customer的Session对象的操作
Mage::getModel('customer/session') #得到Mage_Customre_Model_Session对象
1.可以得到用户 getCustomer()方法
2.判断登录状态 isLoggedIn()方法
3.获取IP地址
$ip = Mage::helper('core/http')->getRemoteAddr()
4.Mage.php常用方法
dispatchEvent() #事件分发
getUrl() #得到URL
getStoreConfig()#得到配置信息,定义在XML
5.常用载入数据方法
Mage::getModel('sales/order')->load(); #得到指定订单
Mage::getModel('customer/customer')->load(); #得到指定用户
Mage::getModel('catalog/product')->load(); #得到指定产品
6.得到常见集合
Mage::getModel('catalog/product')->getCollection() #得到产品的集合
Mage::getModel('customer/customer)->getCollection() #得到用户的集合
Mage::getModel('sales/order')->getCollection() #得到订单集合
7.获取当前店铺信息
Mage::app()->getStore() #得到 Mage_Core_Model_Store对象,最对象里面找对应方法得到相应店铺信息
8.得到当前产品ID
$product_id = Mage::registry('current_product')->getId()
9.得到related products
$associatedProductId = Mage::getModel('catalog/product_type_grouped')->getAssociatedProductIds($_product);
$associatedProduct = Mage::getModel('catalog/product')->load($associatedProductId[0]);
10.获取指定分类产品
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
11.快速开启模板提示
A.找到app/code/core/Mage/Core/Block/Template.php这个文件
B.在getShowTemplateHints这个方法里面添加 return true
12.得到分类子分类
$currCat = Mage::getModel('catalog/category')->load($id) ; //当前分类
$collection = Mage::getModel('catalog/category')->getCategories($currCat->getEntityId());
13.获取指定目录子分类
if($category->hasChildren()) { //判断是否有子目录
$ids = $category->getChildren(); //提取子目录id清单
$subCategories = Mage::getModel('catalog/category')->getCollection();
$subCategories->getSelect()->where("e.entity_id in ($ids)"); //提取指定目录ids的上当清单
$subCategories->addAttributeToSelect('name'); //指定查找目录名称
$subCategories->load();
foreach ($subCategories AS $item) {
echo " - " ;
echo '<a href="'. $item->getUrl() . '">'; //获取目录链接
echo $item->getName(); //获取目录名
echo "</a>(";
echo $item->getProductCount(); //获取目录下的产品数量
//echo $item->getChildrenCount(); //获取目录下子目录数量
echo ")";
echo "<br/>";
}
}
14.获取URL的方法
Mage::getUrl(地址)
Mage::getUrl(地址,array())
Mage::helper('core/url')->getCurrentUrl()
Mage::helper('core/url')->getHomeUrl()
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK)
15.获取商品库存
(int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty()
16.首页判断
$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
if ($routeName == 'cms' && $identifier == 'home')
{
//当前页是首页
}
17.获得订单的所有item
$quote = Mage::getModel('checkout/session')->getQuote();
$items = $quote->getAllActiveItem();
$data = array();
foreach($items as $key => $item){
$data[$key] = $item->toArray(array('属性1','属性2','属性3'));
}
return $data;
18.格式化价格
$price = Mage::helper('core')--->currency($product->getPrice());
$price = Mage::helper('checkout')->formatprice($price);
19.top.links添加
添加:
<action method="addLink" translate="label title">
<label>帮助中心</label>
<url>help</url>
<title>帮助中心</title>
<prepare>true</prepare>
<urlParams helper="core/url/getHomeUrl"/>
<position>555</position>
<liParams/>
<!--<aParams>class="top-link-about-us"</aParams>-->
<beforeText></beforeText>
<afterText></afterText>
</action> 删除:
1、<remove name="checkout_cart_link" />
2、<action method="removeLinkByUrl"><url helper="customer/getAccountUrl"/></action>
20.获得产品的销量
$productOrderedQty = Mage::helper('catalog/product')->getQuantityOrderedBySku($Sku);
21.cms page 加载动态数据
在design中设置如下内容:
<reference name="content">
<block type="catalog/product_new" name="product_new" template="catalog/product/list.phtml">
<action method="setCategoryId"><category_id>10</category_id></action>
<action method="setColumnCount"><column_count>6</column_count></action>
<action method="setProductsCount"><count>0</count></action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager" />
<action method="setDefaultGridPerPage"><limit>12</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>12</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>24</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>36</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>48</limit></action>
<action method="addPagerLimit" translate="label"><mode>grid</mode><limit>all</limit><label>All</label></action>
</block>
<action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>6</count></action>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>
或者:
{{block type="catalog/product_new" column_count="6" products_count="100" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}
22.获得指定目录产品
$products=Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
23.得到可配置产品的下属所有产品
$product = Mage::getModel('catalog/product');
$parent = $product->load(产品ID);
$children = Mage::getModel('catalog/product_type_configurable')
->getUsedProductCollection($parent);
foreach($children as $child){
$child = $product->load($child->getId());
var_dump($child->getName());
}
Magento开发常用方法的更多相关文章
- offline页面开发常用方法及页面控件验证
offline页面开发常用方法及页面控件验证,对一些CheckBoxList操作进行封装,新人可以直接使用该代码. 1.返回上一页网址 /// <summary> /// Descript ...
- magento开发手册之目录结构
magento是一个很优秀的电商系统,很多朋友会用它部署自己的电商网站,少不了二次开发.下面我们随着ytkah来一起认识一下magento开发手册之目录结构吧. /app – 程序根目录 /app/e ...
- magento开发中文手册
Magento开发 第一章 手册简介Introduction 对一个开发人员来说,电子商务开发也许是现今最具创造性的工作.在这个瞬息万变的网络世界,为了保持始终领先竞争对手一步,无论是对你自己还是你的 ...
- magento开发必备插件列表汇总
magento和wordpress一样,因为开放而倍加优秀 ,搜集下常用的magento插件以便大家使用时更加方便快捷 导航放到右侧:magento-community/RicoNeitzel_Ver ...
- Django开发常用方法及面试题
目录 1.对Django的认识? 2.Django .Flask.Tornado的对比 3.什么是wsgi,uwsgi,uWSGI? 4. django请求的生命周期? 5. 简述什么是FBV和CBV ...
- Java-WEB开发常用方法整理
/** * 此类中收集Java编程中WEB开发常用到的一些工具. * 为避免生成此类的实例,构造方法被申明为private类型的. * @author */ import java.io.IOExce ...
- Magento开发基础知识之RequireJs
一.RequireJS概述 RequireJS是一个工具库,主要用于客户端的模块管理.实现异步或动态加载,从而提高代码的性能和可维护性. RequireJS的基本思想是,通过define方法,将代码定 ...
- magento开发 -- 修改当前用户的客户组
$customer = Mage::getSingleton('customer/session')->getCustomer(); $customer->setData( 'group_ ...
- 微信小程序开发常用方法
1.函数中访问data中的数据 _this.setData({ // 日历数据 signList: dataList, // 当前日期 todayDay: str }) 2.if判断 wx:if=&q ...
随机推荐
- MySQL 服务无法启动。服务没有报告任何错误。
MySQL数据库在升级到5.7版本后,和之前的版本有些不一样,没有data文件夹,我们都知道MySQL数据库文件是保存在data文件夹中的,网上有人说把5.6版本的data文件夹拷贝一个,这种说法听听 ...
- GO语言练习:实现最简单的http helloword 服务器
用Go语言实现一个最简单的http服务器端,主要用到了package io, log, net/http 这个3个库. 用到的函数包括: http.Handle() http.HandlerFunc( ...
- javascrit2.0完全参考手册(第二版) 第2章第4节 基本的数据类型
每一个变量都有一个确定的类型表明它存储什么样的数据.js基本的数据类型有strings字符串.numbers数字.Booleans布尔类型.字符串是使用双引号或单引号包含的一串字符:数字包括整数或浮点 ...
- Radar Installation
Radar Installation 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/C 题目: De ...
- [CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字
17.7 Given any integer, print an English phrase that describes the integer (e.g., "One Thousand ...
- Hadoop.2.x_无秘钥设置
1.在实际生产环境中为Hadoop配置无秘钥登录非常有必要 # 在没有配置时: [liuwl@linux-66-64 hadoop-2.5.0]$ jps 26163 Jps [liuwl@linux ...
- Oracle 部分函数使用说明
oracle有些函数可能我知道是什么作用,但是具体其实说不清楚,这里是我这几天看到的函数使用方法及说明,记录一下,以后看看 --1.replace('str',oldVal,newVal)替换功能方法 ...
- HTML元素事件说明
blur( ) 元素失去焦点 a, input, textarea, button, select, label, map, area change( ) 用户改变域的内容 input, textar ...
- app设计需注意的
手机上同步photoshop设计稿: ps play应用 设计: 资源: 1.音乐上传问题 音乐控制在2M以内,推荐使用[格式工厂]进行压缩. 2.视频上传问题 为了保证在线的播放效果,上传的视频大小 ...
- HttpSessionListener和HttpSessionBindingListener监听session的销毁
1. 使用HttpSessionListener public class OnlineUserListener implements HttpSessionListener { public voi ...