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