天天看點

Magento 常用方法和插件

得到路徑e:\ddl\trunk\3_development\03_sourcecode\test.com\www\app\design\frontend\default\ddl_v2\template

Magento 常用方法和插件

mage::getdesign()->getbasedir(array('_area'=>'frontend','_package'=>'default','_theme'=>'ddl_v2','_type'=>'template'))  

獲得客戶ip:

Magento 常用方法和插件

mage::helper('core/http')->getremoteaddr(true)  

截取字元串

Magento 常用方法和插件

<?php echo $this->htmlescape(mage::helper('core/string')->truncate($str,10,'…')) ?>  

在購物車中清除所有産品代碼:

Magento 常用方法和插件

foreach( mage::getsingleton('checkout/session')->getquote()->getitemscollection() as $item ){  

    mage::getsingleton('checkout/cart')->removeitem( $item->getid() )->save();  

}  

清除購物車所有session:

Magento 常用方法和插件

mage::getsingleton('checkout/session')->clear();  

登入資訊customer

Magento 常用方法和插件

mage::helper('customer')->isloggedin();  

mage::helper('customer')->getcustomer();  

/* 擷取背景使用者登入資訊 */  

mage::getmodel('admin/session')->getuser()->getusername()  

controller,phtml中

Magento 常用方法和插件

echo $this->getlayout()->createblock('core/template')->settemplate('cms/jifen.phtml')->tohtml();  

echo $this->getlayout()->createblock('rebates/vendor')->settemplate('cms/rebate.phtml')->tohtml();   

$this->getlayout()->createblock('clientnumber/inputform', 'number')->settemplate('clientnumber/input.phtml')->tohtml();//不需要xml  

$this->getlayout()->getblock('xml_block_name')->tohtml(); //xml中配置了template  

$this->getlayout()->getblock('core/templete')->settemplate('dianyin/'.$forwardpage.'.phtml')->tohtml();//xml 沒有template  

magento中是怎樣獲得可配置商品的所有簡單商品

Magento 常用方法和插件

if($_product->isconfigurable()){  

    $allproducts=$_product->gettypeinstance(true)->getusedproducts(null, $_product);  

    foreach($allproducts as $simpleproduct){  

        echo $simpleproduct->getsku();  

    }  

magento擷取産品的銷售數量

Magento 常用方法和插件

$productorderedqty = mage::helper('catalog/product')->getquantityorderedbysku($psku);  

magento如何得到産品的庫存

Magento 常用方法和插件

(int) mage::getmodel('cataloginventory/stock_item')->loadbyproduct($_product)->getqty();  

 magento 擷取指定分類下的産品

Magento 常用方法和插件

$products= mage::getmodel('catalog/category')->load($category_id)->getproductcollection()  

->addattributetoselect('*')  

    ->addattributetofilter('status', 1)  

    ->addattributetofilter('visibility', 4);  

1:擷取session

Magento 常用方法和插件

$session = mage::getsingleton('customer/session');  

2:request對象

Magento 常用方法和插件

mage::app()->getrequest();  

$post = mage::app()->getrequest()->getparam();  

mage::app()->getrequest()->getcontrollername();  

mage::app()->getrequest()->getactionname();  

mage::app()->getrequest()->getroutename();  

mage::app()->getrequest()->getmodulename();   

mage::app()->getfrontcontroller()->getaction();  

mage::app()->getfrontcontroller()->getrequest()->getroutename() ;  

3::擷取目前時間

Magento 常用方法和插件

mage::getmodel('core/date')->date();  

mage::getsingleton('core/date')->gmtdate();  

date("y-m-d", mage::getmodel('core/date')->timestamp(time()));  

獲得cache的值:

Magento 常用方法和插件

mage::app()->loadcache('admin_notifications_lastcheck');  

儲存cache中:

Magento 常用方法和插件

mage::app()->savecache(time(), 'admin_notifications_lastcheck');  

4:session,cookie設定

4.1 set session,cookie:

Magento 常用方法和插件

mage::getsingleton('core/cookie')->set('name','value');  

mage::getsingleton('core/session')->setdata('name','value');  

4.2 get session,cookie:

Magento 常用方法和插件

mage::getsingleton('core/cookie')->get('name');  

mage::getsingleton('core/session')->getdata('name');  

5.generate skin url

Magento 常用方法和插件

mage::getdesign()->getskinurl('images/our_shops/shop_logo_default.jpg');  

11:背景子產品跳轉 :

Magento 常用方法和插件

mage::app()->getfrontcontroller()->getresponse()->setredirect('http://your-url.com');  

mage::app()->getresponse()->setredirect(mage::helper('adminhtml')->geturl("adminhtml/promo_quote/index"));  

12:産品屬性操作

$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:或取欄目圖檔

Magento 常用方法和插件

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:産品縮略圖

Magento 常用方法和插件

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

17:cms/page

Magento 常用方法和插件

$cms_id = mage::getsingleton('cms/page')->getidentifier();  

$cms_title = mage::getsingleton('cms/page')->gettitle();  

$cms_content = mage::getsingleton('cms/page')->getcontent();  

21:目前路徑referer加密

Magento 常用方法和插件

$currenturl = $this->helper('core/url')->getcurrenturl();  

mage::helper('core')->urlencode($url);  

mage::helper('core')->getcurrentbase64url()  

25:擷取目前站點貨币符号

Magento 常用方法和插件

//擷取店鋪對象  

 mage::app()->getstore();  

//擷取目前店鋪id  

 mage::app()->getstore()->getstoreid();  

//擷取目前店鋪code,該code在建立店鋪時填寫  

 mage::app()->getstore()->getcode();  

//擷取目前店鋪所屬的website id  

 mage::app()->getstore()->getwebsiteid();  

//擷取目前店鋪的name  

 mage::app()->getstore()->getname();  

//擷取目前店鋪的狀态  

 mage::app()->getstore()->getisactive();  

//擷取目前店鋪的url  

 mage::app()->getstore()->gethomeurl();  

//擷取目前貨币code  

echo $currency_code = mage::app()->getstore()->getcurrentcurrencycode();  

//擷取目前貨币符号  

echo mage::app()->getlocale()->currency(mage::app()->getstore()->getcurrentcurrencycode())->getsymbol();  

26:擷取産品屬性集

Magento 常用方法和插件

$sets = mage::getresourcemodel('eav/entity_attribute_set_collection')->setentitytypefilter(mage::getmodel('catalog/product')->getresource()->gettypeid())->load()->tooptionhash();  

27:設定meta資訊

Magento 常用方法和插件

$this->loadlayout();  

$this->getlayout()->getblock('head')->settitle('title');  

$this->getlayout()->getblock('head')->setkeywords('keywords');  

$this->getlayout()->getblock('head')->setdescription('description');  

$this->renderlayout();  

mage類中屬register,unregister,registry,getbaseurl,geturl,getmodel,getsingleton,getresourcemodel,helper.我比較常用,其他的都要看情況了。

mage::register($key, $value, $graceful = false)

作用:注冊變量,這個一般都是傳值,比如controller控制器傳值到block,或者model或者helper.

參數:$key,鍵名,随便一個不存在的鍵名。

$value,鍵值。既是要儲存的值。

$graceful,這個是标志是否要覆寫原有的值,預設是false.如果鍵名存在,而$graceful又是false,則抛出異常。

mage::unregister($key)

作用:銷毀已經注冊的變量值。

參數:$key,鍵名,任何已經注冊了的鍵名。

mage::registry($key)

作用:擷取注冊了的變量。既是擷取了已經用mage::register方法注冊了的變量,如沒有則傳回null,在magento中,如果擷取了沒有注冊的變量,那麼可能會出現異常。

mage::getbaseurl()

作用:擷取網站的根url,也就是url位址。

參數:一般不涉及複雜操作都用預設變量。

mage::geturl($route = ”, $params = array()) 

作用:擷取網站的url路勁,也就是路由。

參數:$route路由名稱,類似于cms/index/index/,cms屬于子產品名稱,該名稱由config.xml的frontname标簽指定。

$params(),參數,形如index.php?ss=mm的參數。格式為數組

mage::getmodel($modelclass = ”, $arguments = array())

作用:擷取資料模型執行個體對象。

參 數:$modelclass 參數形式:’catalog/product’,前面的catalog是子產品名,也就是分組的意思,在magento 中,block,model,helper的類都是分組的,而product,也就是catalog子產品的model檔案夾下的product檔案。

$arguments 擷取該類所需要的參數

mage::getsingleton($modelclass=”, array $arguments=array())

作用:該方法與getmodel取同樣的模型,隻是該方法會注冊變量

參數:與getmodel無異。

mage::getresourcemodel($modelclass, $arguments = array())

作用:擷取資源模型。一般都會形如catalog/product_collection的,傳回數組集合,項目屬于mage::getmodel所産生的對象。

調用block中的方法; mage::getblocksingleton()

Magento 常用方法和插件

$obj = mage::getblocksingleton('rewards/checkout_rewardspent');  

mage::helper($name)

作用:擷取幫助類對象,一般形如catalog/類名 ,傳回幫助類對象,這個也可以直接用catalog,預設返貨helper檔案下的data類。

參數:catalog或者catalog/類名

好了,mage靜态類中這幾個是我經常用到的,其他的在各方面各方面可能用到,這裡就不再多說了。

當我們在設計開發magento 主題模闆 時候,有個函數一定要知道,那就是getbaseurl 函數,用getbaseurl 函數我們可以在.phtml裡調用js、image、file.

1getbaseurl 基本參數調用路徑

Magento 常用方法和插件

// http://magentonotes.com/  

echo mage::getbaseurl(mage_core_model_store::url_type_web);  

// http://magentonotes.com/js/  

echo mage::getbaseurl(mage_core_model_store::url_type_js);  

//http://magentonotes.com/index.php/  

echo mage::getbaseurl(mage_core_model_store::url_type_link);  

//d:\wamp\www\magento\media  

echo mage::getbasedir(mage_core_model_store::url_type_media);  

//http://magentonotes.com/skin/  

echo mage::getbaseurl(mage_core_model_store::url_type_skin);  

按照上面這個類,我們可以使用簡化代碼來實作路徑 的調用

Magento 常用方法和插件

echo mage::getbaseurl('skin');  

mage_core_helper_url類在 ./app/code/core/mage/core/helper/url.php檔案裡

Magento 常用方法和插件

//擷取首頁  

echo mage::helper('core/url')->gethomeurl();  

str_replace(mage::getbaseurl(),'',mage::helper('core/url')->getcurrenturl());  

 還有種顯示圖檔的方法:

Magento 常用方法和插件

<?php echo $this->getskinurl('images/ddbtn.gif');?>  

$this->getjsurl('varien/accordion.js')  

<?php echo $this->helper('core/js')->includescript('varien/accordion.js') ?>  

magento 一步支付插件 onestepcheckout

圖檔上傳 magento-community/multibanner_module

magento訂單删除插件key:magento-community/boutikcircus_deleteorders

magento 支付寶key:magento-community/cosmo_community_alipaypaymentgateway

背景管理産品顯示圖檔 key:magento-community/tbt_enhancedgrid

多列的下拉菜單key:magento-community/raptor_explodedmenu

産品圖檔放大鏡key:magento-community/moo_cloudzoom 

推薦分類key: magento-community/sfc_featuredcategories 

進入magento背景,點選catalog > manage categories 選擇你要推薦的分類進入,通過"featured category?" 及 "featured   sub-category?" 兩個選項就可以設定你的推薦分類了

Magento 常用方法和插件

分類清單 magento-community/categorymodule 

分類中的暢銷産品 magento-community/arboresys_bestseller_per_category 

熱銷産品magento-community/luxe_bestsellers

每一個開發magento模闆的程式員手中必備的blank主題,相對預設的magento主題,blank進行了大瘦身并且清除了不必要的xml,和模闆中的多餘代碼,對性能的提供也是有很大幫助的。

magento blank theme:

magento-core/interface_frontend_default_blank

magento-core/interface_frontend_default_modern

magento-community/free_mage_theme

magento-community/basic_template

magento-community/templatemonster_theme

magento-community/dc_catalog 可以根據品牌或各種屬性來篩選産品

Magento 常用方法和插件

 購物車預覽 extension key:magento-community/cartview

Magento 常用方法和插件