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 ...
随机推荐
- The beatles-Yesterday
轉載自https://www.youtube.com/watch?v=XNnaxGFO18o Yesterday Lyrics:Paul Mccartney Music:Paul Mccartney ...
- CentOS VirtualBox启动虚拟及报错:VirtualBox error: Kernel driver not installed (rc=1908)
VirtualBox error: Kernel driver not installed (rc=1908) Hi all, Let me first say that this is my fin ...
- golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法
golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法,查询中报了以下这个错 Scan error on column index 2: ...
- About_PHP
所谓PHP: 超文本预处理器 外文名称 Hypertext Preprocessor 编程范型 面向对象.命令式编程 php就是比js更高端的一种语言. 语法有两种: <?php ?& ...
- Hadoop.2.x_简单的测试文件读取与上传
代码如下, 后备参考: package com.bigdata.hadoop.hdfs; import java.io.File; import java.io.FileInputStream; im ...
- 前端技术-PS切图
页面制作部分之PS切图 <--本标签下,通过页面制作.页面架构.javascript程序设计.DOM编程艺术.产品前端架构五部分来分享总结笔记,总结笔记会陆续分享--> 网页设计在技术层面 ...
- c# select标签绑定枚举,并以Description做Text显示
今天在做项目时遇到一个问题: 开发中有些字段是枚举类型如 Dept 企业表中可能有个字段 Property 性质 0:事业单位,1:私企,2:外企,但有时我们不会单独为性质这个字段定义一张表, 而是在 ...
- Learn ZYNQ Programming(1)
GPIO LED AND KEY: part1:gpio leds and gpio btns combination. (include 1~4) part2:use gpio btns inter ...
- 数组API
1.数组的创建 var arrayObj = new Array();//创建一个默认数组,长度是0 var arrayObj = new Array(size);//创建一个size长度的数组,注意 ...
- eval 与 Function
var json='{"name":"lee","age":"15"}' function test () { var ...