这里是我做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()); }