码迷,mamicode.com
首页 > 其他好文 > 详细

Magento开发常用方法

时间:2016-04-19 00:11:56      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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

Magento开发常用方法

标签:

原文地址:http://www.cnblogs.com/ximotao/p/5406400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!