这里是我做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开发常用方法的更多相关文章

  1. offline页面开发常用方法及页面控件验证

    offline页面开发常用方法及页面控件验证,对一些CheckBoxList操作进行封装,新人可以直接使用该代码. 1.返回上一页网址 /// <summary> /// Descript ...

  2. magento开发手册之目录结构

    magento是一个很优秀的电商系统,很多朋友会用它部署自己的电商网站,少不了二次开发.下面我们随着ytkah来一起认识一下magento开发手册之目录结构吧. /app – 程序根目录 /app/e ...

  3. magento开发中文手册

    Magento开发 第一章 手册简介Introduction 对一个开发人员来说,电子商务开发也许是现今最具创造性的工作.在这个瞬息万变的网络世界,为了保持始终领先竞争对手一步,无论是对你自己还是你的 ...

  4. magento开发必备插件列表汇总

    magento和wordpress一样,因为开放而倍加优秀 ,搜集下常用的magento插件以便大家使用时更加方便快捷 导航放到右侧:magento-community/RicoNeitzel_Ver ...

  5. Django开发常用方法及面试题

    目录 1.对Django的认识? 2.Django .Flask.Tornado的对比 3.什么是wsgi,uwsgi,uWSGI? 4. django请求的生命周期? 5. 简述什么是FBV和CBV ...

  6. Java-WEB开发常用方法整理

    /** * 此类中收集Java编程中WEB开发常用到的一些工具. * 为避免生成此类的实例,构造方法被申明为private类型的. * @author */ import java.io.IOExce ...

  7. Magento开发基础知识之RequireJs

    一.RequireJS概述 RequireJS是一个工具库,主要用于客户端的模块管理.实现异步或动态加载,从而提高代码的性能和可维护性. RequireJS的基本思想是,通过define方法,将代码定 ...

  8. magento开发 -- 修改当前用户的客户组

    $customer = Mage::getSingleton('customer/session')->getCustomer(); $customer->setData( 'group_ ...

  9. 微信小程序开发常用方法

    1.函数中访问data中的数据 _this.setData({ // 日历数据 signList: dataList, // 当前日期 todayDay: str }) 2.if判断 wx:if=&q ...

随机推荐

  1. LBS基站数据解析接口

    http://www.cellocation.com/interfac/#hybrid http://www.cellid.cn/ https://www.juhe.cn/docs/api/id/8

  2. Linux-CentOS 6.5 mini 中没有curses.h的问题

    1.直接贴过程 [fengbo@CentOS: jigsaw]$ rpm -q ncursesncurses-5.7-3.20090208.el6.i686[fengbo@CentOS: jigsaw ...

  3. windows2008 IIS下配置FTP服务

    一.服务器管理器 1.2008的系统使用服务器管理器,选择角色,因为我之前已经开启了IIS服务器角色,所以我现在只要添加角色服务即可,如果你没有开启过的话,直接添加角色即可. 2.选择WEB服务器,打 ...

  4. Js实现MD5加密

    在页面中引用md5.js文件,调用方法为 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8&qu ...

  5. 使用数据泵导入(impdp)和导出(expdp)

    数据泵技术是Oracle Database 10g 中的新技术,它比原来导入/导出(imp,exp)技术快15-45倍.速度的提高源于使用了并行技术来读写导出转储文件. expdp使用 使用EXPDP ...

  6. 黑马程序员----java基础笔记中(毕向东)

    <p>------<a href="http://www.itheima.com" target="blank">Java培训.Andr ...

  7. [LintCode] Wiggle Sort II 扭动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  8. jquery中对动态生成的标签响应click事件(二)…与ajax交互使用

    <%@ page language="java" contentType="text/html; charset=GB18030" pageEncodin ...

  9. c#面向对象基础 类、方法、方法重载

    C#是纯粹的面向对象编程语言,它真正体现了“一切皆为对象”的精神.在C#中,即使是最基本的数据类型,如int,double,bool类型,都属于System.Object(Object为所有类型的基类 ...

  10. document.body.clientHeight 和 document.documentElement.clientHeight的区别

    document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.document ...