搜档网
当前位置:搜档网 › magento常用方法总结

magento常用方法总结

**********************************************************
*********************获取某分类商品***********************
**********************************************************
$category = Mage::getModel('catalog/category')->load(38); //38代表该类别的ID
**********************************************************
**********************************************************

**********************************************************
************************后台cms页面加载block***************
**********************************************************
{{block type="catalog/product_list" category_id="6" template="catalog/product/free_list.phtml"}}
获取Magento当前分类名称和ID的方法
$_cat= newMage_Catalog_Block_Navigation();$curent_cat= $_cat->getCurrentCategory();$curent_cat_id= $curent_cat->getId(); $category= Mage::registry('current_category')->getName();
**********************************************************
**********************************************************


**********************************************************
Magento 常用技巧与方法抢沙发
**********************************************************

1:获取session

$session = Mage::getSingleton('customer/session');

2:Request对象

Mage::app()->getRequest()

3:调用Model对象

Mage::getModel('infinity/model');

4:获取当前时间

Mage::getModel('core/date')->date();

date("Y-m-d", Mage::getModel('core/date')->timestamp(time()));

5:session,cookie设置

5.1 Model:

Mage::getModel(‘core/cookie’);

Mage::getModel(‘core/session’);

5.2 Set Method:

Mage::getSingleton(‘core/cookie’)->set(‘name’,'value’);

Mage::getSingleton(‘core/session’)->set(‘name’,'value’);

5.3 Get method:

Mage::getSingleton(‘core/cookie’)->get(‘name’);

Mage::getSingleton(‘core/session’)->get(‘name’);

6:输出配置文件

//header(‘Content-Type: text/xml’);

header(‘Content-Type: text/plain’);

echo $config = Mage::getConfig()

->loadModulesConfiguration(‘system.xml’)

->getNode()

->asXML();

exit;

7:Get URL for a Magento Category

Mage::getModel('catalog/category')->load(17)->getUrl();

8:build your URL with valid keys

Mage::helper("adminhtml")->getUrl("mymodule/adminhtml_mycontroller/myaction/",array("param1"=>1,"param2"=>2));

9:create key values

Mage::getSingleton('adminhtml/url')->getSecretKey("adminhtml_mycontroller","myaction");

10:disable security feature in the admin panel

admin panel -> System -> Configuration -> Admin section: “Add Secret key to Urls”.

11:后台模块跳转:

Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl("adminhtml/promo_quote/index"));

12:产品属性操作

$product = Mage::getModel('catalog/product')->getCollection()->getFirstItem();

foreach($product->getAttributes() as $att)

{

$group_id = $att->ge

tData('attribute_group_id');

$group = Mage::getModel('eav/entity_attribute_group')->load($group_id);

var_dump($group);

}

$attrSetName = 'my_custom_attribute';

$attributeSetId = Mage::getModel('eav/entity_attribute_set')

->load($attrSetName, 'attribute_set_name')

->getAttributeSetId();

13:get a drop down lists options for a mulit-select attribute

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');

foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){

$attributeArray[$option['value']] = $option['label'];

}

14:或取栏目图片

public function getImageUrl($category)

{
return Mage::getModel('catalog/category')->load($category->getId())->getImageUrl();
}

public function getThumbnailUrl($category)

{

$image=Mage::getModel('catalog/category')->load($category->getId())->getThumbnail();

if ($image) {

$url = Mage::getBaseUrl('media').'catalog/category/'.$image;

}

return $url;

}

15:产品缩略图

$_thumb = Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(50, 50)->setWatermarkSize('30x10');

16:判断是否首页:$this->getIsHomePage()

Mage::getSingleton('cms/page')->getIdentifier() == 'home' &&

Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms'

)

17:

$cms_id = Mage::getSingleton('cms/page')->getIdentifier();

$cms_title = Mage::getSingleton('cms/page')->getTitle();

$cms_content = Mage::getSingleton('cms/page')->getContent();

18 :

$attributes = $_product->getAttributes();
$themeColor = $attributes['theme_color']->getFrontend()->getValue($_product);

19:获取configurable产品simple product

if($_product->getTypeId() == "configurable"):

$ids = $_product->getTypeInstance()->getUsedProductIds();

foreach ($ids as $id) :

$simpleproduct = Mage::getModel('catalog/product')->load($id);

$simpleproduct->getName()." - ".(int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty();

$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);

20: get the attributes of Configurable Product.

$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);

21:当前路径

$currentUrl = $this->helper('core/url')->getCurrentUrl();

22:通过资源配置方式创建目录

$installer = $this;

$installer->startSetup();

// make directory for font cache

try {

$domPdfFontCacheDir = join(DS, array('lib', 'Symmetrics', 'dompdf', 'fonts'));

$domPdfFontCacheDir = Mage::getBaseDir('var') . DS . $domPdfFontCacheDir;

if (!file_exists($domPdfFontCacheDir)) {

mkdir($domPdfFontCacheDir, 0777, true);

}

} catch(Exception $e) {

throw new Exception(

'Directory ' . $domPdfFontCacheDir . ' is not writable or couldn\'t be '

. 'created. Please do it manually.' . $e->getMessage()

);

}

$installer->endSet

up();

23:





wishlist_link





Hometrue0

helpHelptrue90

My Account10









example

example

true



100



class="top-link-example"









24:在controllers 实现跳转

Mage::app()->getFrontController()
->getResponse()
->setRedirect('https://www.sodocs.net/doc/157631993.html,');

25:获取当前站点货币符号

$storeId = (int) $this->getRequest()->getParam('store', 0);

$store=Mage::app()->getStore($storeId);

$currencyCode=$store->getBaseCurrency()->getCode()

$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);

$attributeData = $attribute->getData();

$frontEndLabel = $attributeData['frontend_label'];

$attributeOptions = $attribute->getSource()->getAllOptions();

26:获取产品属性集

$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')

->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())

->load()

->toOptionHash();

27:magento使用sql

$resource = Mage::getSingleton('core/resource');

$read= $resource->getConnection('core_read');

$tempTable = $resource->getTableName('infinity_contacts');

$_storeid = Mage::app()->getStore()->getId();

$wheres = "`status`=1 AND ( FIND_IN_SET(0, `store_id`)>0 OR FIND_IN_SET($_storeid, `store_id`)>0 )";

$select = $read->select()

->from($tempTable, array('count(*) as num'))

->where($wheres);

28:设置meta信息

$template = Mage::getConfig()->getNode('global/page/layouts/'.Mage::getStoreConfig("featuredproducts/general/layout").'/template');

$this->loadLayout();

$this->getLayout()->getBlock('root')->setTemplate($template);

$this->getLayout()->getBlock('head')->setTitle($this->__(Mage::getStoreConfig("featuredproducts/general/meta_title")));

$this->getLayout()->getBlock('head')->setDescription($this->__(Mage::getStoreConfig("featuredproducts/general/meta_description")));

$this->getLa

yout()->getBlock('head')->setKeywords($this->__(Mage::getStoreConfig("featuredproducts/general/meta_keywords")));

$this->renderLayout();

**********************************************************
**********************************************************

https://www.sodocs.net/doc/157631993.html,/create-a-the-magento-classification-tree-in-the-foreground.html

https://www.sodocs.net/doc/157631993.html,/wolf_62/blog/item/d0528409e1ce6e0094ca6b6d.html


顶部导航菜单
getChildHtml('topMenu') ?>



*************************************************************************
*************************************************************************
**********************根据商品id获取商品详细信息*************************
*************************************************************************

$model = Mage::getModel('catalog/product') //getting product model

$_product = $model->load($productid); //getting product object for particular product id

echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
echo $_product->getName(); //product name
echo $_product->getPrice(); //product's regular Price
echo $_product->getSpecialPrice(); //product's special Price
echo $_product->getProductUrl(); //product url
echo $_product->getImageUrl(); //product's image url
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url

?>
*************************************************************************
*************************************************************************




***************************************************************************************************
***************************************************************************************************
在Magento中添加一个static block(https://www.sodocs.net/doc/157631993.html,/post-47.html magento加载static block内容)
***************************************************************************************************
***************************************************************************************************

可以按照如下方法在前台显示一个static block

1、在后台CMS页面中加入static block,代码如下:

1
{{block type="cms/block" block_id="your_block_identifier" template="cms/content.phtml"}}
2、在前台模板文件中加入static block

1
getLayout()->createBlock('cms/block')->setBlockId('your_block_identifier')->toHtml() ?>
代码中的'your_block_identifier'就是在你建立block中填写的identifier。

3、在XML布局文件中加入static block

1

2
your_block_identifier

如何在static block中加入链接和图片呢?

在Magento中的static block加入链接和图片有特殊的语法格式

,加入链接要用到两个大括号"{{"开始,然后两个"}}"结束,图片路径也是如此。

加入网站中的链接语法:

1
{{store url=''}}
例如:Login

加入图片的语法:

1
{{skin url=''}}
例如:

*****************************************************************************************************
*****************************************************************************************************

相关主题