这里是我做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.获取用户登录状态
Mage::getSingleton(‘customer/session‘)->isLoggedIn()
3.获取当前用户
$customer = Mage::getSingleton(‘customer/session‘)->getCustomer()
4.获取IP地址
$ip = Mage::helper(‘core/http‘)->getRemoteAddr()
5.事件分发
Mage::dispatchEvent($name,array $data = array())
6.得到产品详情
$model = Mage::getModel(‘catalog/product‘) $_product = $model->load($productid)
7.得到所有商品集合
$model = Mage::getModel(‘catalog/product‘) $collection = $model->getCollection()
8.获取当前店铺信息
$store_id = Mage::app()->getStore()->getStoreId() $code = Mage::app()->getStore()->getCode() $website_id = Mage::app()->getStore()->getWebsiteId() $name = Mage::app()->getStore()->getName() $status = Mage::app()->getStore()->getIsActive() $url = Mage::app()->getStore()->getHomeUrl()
9.得到当前产品ID
$product_id = Mage::registry(‘current_product‘)->getId()
10.得到related products
$associatedProductId = Mage::getModel(‘catalog/product_type_grouped‘)->getAssociatedProductIds($_product); $associatedProduct = Mage::getModel(‘catalog/product‘)->load($associatedProductId[0]);
12.获取指定分类产品
$products = Mage::getModel(‘catalog/category‘)->load($category_id) ->getProductCollection() ->addAttributeToSelect(‘*‘) ->addAttributeToFilter(‘status‘, 1) ->addAttributeToFilter(‘visibility‘, 4);
13.快速开启模板提示
A.找到app/code/core/Mage/Core/Block/Template.php这个文件 B.在getShowTemplateHints这个方法里面添加 return true
14.得到分类子分类
$currCat = Mage::getModel(‘catalog/category‘)->load($id) ; //当前分类 $collection = Mage::getModel(‘catalog/category‘)->getCategories($currCat->getEntityId());
15.获取指定目录子分类
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/>"; } }
16.获取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)
时间: 2024-11-08 21:13:28