众所周知,Magento的多店铺功能非常强大,通过多店铺功能,我们可以在一个Magento应用当中创建在外观上看来完全不相关联的多个店铺,并且可以绑定不同的域名。在Magento的开发当中,经常需要获取当前所在店铺的一些相关信息,例如店铺id,店铺名称及当前店铺所属Website的ID值。通过Magento的Mage类,我们可以获取到关于店铺的所有信息。
//获取店铺对象 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();
1. Magento判断当前是否为首页
Here is a quick code to check if the current page is homepage or not.
if($this->getIsHomePage()) { echo 'You are in Homepage!'; } else { echo 'You are NOT in Homepage!'; }
Below is an alternative way to check for homepage:-
$routeName = Mage::app()->getRequest()->getRouteName(); $identifier = Mage::getSingleton('cms/page')->getIdentifier(); if($routeName == 'cms' && $identifier == 'home') { echo 'You are in Homepage!'; } else { echo 'You are NOT in Homepage!'; }
2. Magento获取当前页面URL地址
在Magento中,可以通过core/url助手类中的getCurrentUrl()方法获取当前页面的URL地址
$currentUrl = $this->helper('core/url')->getCurrentUrl();
应用:
//在社会化分享中,当前页面的URL地址非常有用 <a addthis:url="<?php echo $this->helper('core/url')->getCurrentUrl(); ?>">Share on Facebook</a><span>
3. Magento获取当前分类 Get the current category in Magento
名称:
Mage::registry('current_category')->getName();
或者:
$this->getCurrentCategory()
$_category = Mage::registry('current_category'); print_r($_category); [_data:protected] => Array ( [store_id] => 1 [entity_id] => 10 [entity_type_id] => 3 [attribute_set_id] => 3 [parent_id] => 3 [created_at] => 2012-09-12 14:20:11 [updated_at] => 2012-09-12 14:20:11 [path] => 1/2/3/10 [position] => 1 [level] => 3 [children_count] => 0 [name] => Ballet Flats [url_key] => ballet-flats [meta_title] => [display_mode] => PRODUCTS [custom_design] => [page_layout] => [url_path] => collection/ballet-flats.html [is_active] => 1 [include_in_menu] => 1 [landing_page] => [is_anchor] => 0 [custom_use_parent_settings] => 0 [custom_apply_to_products] => 0 [description] => [meta_keywords] => [meta_description] => [custom_layout_update] => [available_sort_by] => [custom_design_from] => [custom_design_to] => [filter_price_range] => [path_ids] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 10 ) )