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 ...
随机推荐
- Windows环境下的NodeJS+NPM+Bower安装配置步骤
Windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL”按钮 ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- jQuery -> 获取元素的各种过滤器
转自http://blog.csdn.net/feelang/article/details/26613023
- 新建一个新的spring boot项目
简单几步,在Eclipse中创建一个新的spring Boot项目: 1.Eclipse中安装STS插件: Help -> Eclipse Marketplace... Search或选择&qu ...
- iOS frame从导航栏下面开始
iOS7.0 之后,页面布局默认延伸到了手机界面的边缘;导航栏背景成透明颜色. 解决fram从导航栏下面开始方法: 第一种: 将导航栏改成不透明即可 if( ([[[UIDevice currentD ...
- SQL Server 触发器(转)
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...
- LaTex 数学公式
\usepackage{amsmath} 常用宏包 \usepackage{arydshln} 此宏包带虚线 $ $ 行内公式 $$ $$ 行间公式 \[ \] 行间公式 \numberwithin{ ...
- 用refresh控制浏览器定时刷新
package cn.itcast.response; import java.io.IOException; import java.util.Random; import javax.servle ...
- Json的语法及使用方法
Json的语法及使用方法 Json(JavaScript Object Notation)对象表示标识,是一种轻量级的数据交换语言,比XML更容易解析,独立于语言和平台. 语法规则: 对象用{}保存 ...
- NPOI简单操作excel
本文仅当是个记录文件,仅供初学者参考. 首先得using几个npoi的空间名如下: using NPOI.HSSF.UserModel;using NPOI.HSSF.Util;using NPOI. ...