magento的开发原则:
1.不要修改magento的Core.
2.对于需要添加的功能,如果使用theme即可以解决的问题,即修改theme即可。如果必须做成插件才可以完成的,则考虑使用插件
3. 规范的书写代码
MAGENTO与theme的开发要点
常用的layout文件
catalog.xml layout文件的语法规则
xml 实际上是某一个页面的布局说明,它包含了多个reference(menu,header,left,right).
reference相当于在一个页面中的多个核心部位,比如header,content,left,right,footer etc.
reference包括了多个block(类似于HTML中的块状结构),为了保证重用性以及灵活性,一般都使用引用template.
对于block而言,可能它还可能有图片设置,文本设置,链接设置等,这就需要使用到action.
常见的layout文件
常见的reference块(默认情况下)
<reference name="root">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
</reference>
<reference name="top.menu"></reference>
<reference name="content"></reference>
<reference name="left"></reference>
<reference name="right"></reference>
<reference name="footer_links"></reference>
<reference name="head"></reference>
<reference name="product.info"></reference>
<reference name="minicart"></reference>
常见的block格式
<block type="catalog/navigation" name="catalog.topnav"
template="catalog/navigation/top.phtml"/>
常见的默认action:
<action method="addJs" ifconfig="dev/js/deprecation"><script>prototype/deprecation.js</script></action>
<action method="addCss"><stylesheet>css/styles.css</stylesheet></action>
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
<action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
<action method="setDefaultGridPerPage"><limit>3</limit></action>
<action method="addPagerLimit"><mode>list</mode><limit>2</limit></action>
<action method="unsetChildren"></action>
<block type="catalog/product_list_upsell" name="product.info.upsell" as="upsell_products" template="catalog/product/list/upsell.phtml">
<action method="setColumnCount"><columns>4</columns></action>
<action method="setItemLimit"><type>upsell</type><limit>4</limit></action>
</block>
<action method="addItem">
<type>skin_js</type><name>js/lightbox.js</name>
</action>
<action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>—></action>
<reference name="seo.sitemap.links">
<action method="addLink" translate="label title" module="catalog"><label>Categories Sitemap</label><url helper="catalog/map/getCategoryUrl"/><title>Categories Sitemap</title></action>
删除block或者是action
删除block
<reference name="content">
<remove name="right.permanent.callout_copy" /></reference>
删除reference
<reference name="root">
<remove name="right"/>
<remove name="left"/>
</reference>
block块的在同一reference中的位置顺序调整
You can use the ‘before’ and ‘after’ attribute in the block tag to position the blocks.
1. before="-" positions the block before all other blocks
2. after="-" positions the block after all other block
3. before="some_block" positions this block before another block with name="some_block"
4. after="some_block" position this block after another block with name="some_block"
For example <block name="this_block" before="that_block"/>
应用到themem中常用API
得到首页地址
<a href="<?php echo $this->getUrl('')?>"><?php echo $this->__('Home') ?></a>
Every Page and PAI in theme
$this->getUrl()
$this->getTitle()
$this->getTags()
CMS page中的各种变量引用
<img src="{{skin url='img_products/big_banner.jpg'}}" alt="Big banner" title="Big banner" />
此图片文件来自于skin中的theme目录
Block: This creates a static block with specified ID.
For example: {{block type='core/template' template='cms/your_page.phtml'}}
Layout: This renders an HTML layout output for the specified layout. The handle attribute expects the name of a layout handle.
For example: {{layout handle="default"}}
Media: This retrieves path of files from root/media folder.
For example: {{media url="images/logo.png"}}
Skin: This gets files from active theme in skin/frontend folder.
For example: <img src="{{skin url='images/media/about_us.png'}}"/>
Store: This creates routes and custom URLs for Magento store.
For example: {{store url="contact"}} would render the full URL for that path.
MAGENTO中的helper助手
$this->helper('catalogsearch')
$this->helper('catalogsearch')->getResultUrl()
$this->helper('catalogsearch')->getQueryParamName()
$this->helper('catalogsearch')->getEscapedQueryText()
Mage::helper('directory')
Mage::helper('directory')->currencyConvert()
Image
$this->helper('catalog/image')
$this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())->resize(640, 480); ?>"
shopping cart中的API
在模板中加载javascript或者CSS
var BLANK_IMG = '<?php echo $this->helper('core/js')->getJsUrl('spacer.gif') ?>';
var SKIN_URL = '<?php echo $this->helper('core/js')->getJsSkinUrl('') ?>';
CMS中的API
Mage::getSingleton('cms/page')
is_home()
$checkhome=true;
$page = Mage::app()->getFrontController()-
>getRequest()->getRouteName();
if ($page == 'cms'){
$checkhome=(Mage::getSingleton('cms/page')-
>getIdentifier()=='home') ? false : true;
}
Product 与 API
$cat_id = "35";
得到store ID
$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('catalog/product_
collection')
->addAttributeToSelect(array('name', 'price', 'small_image','short_description'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')
>setStoreId($storeId)
>addStoreFilter($storeId)
->load($cat_id));
if(!$_productCollection->count()){
echo $this->__('No products in the homepage category.');
}else{
foreach ($_productCollection as $_product){
$_product->getName()
}
}
get product images
$this->getGalleryImages()
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())->resize(640, 480); ?>"rel="lightbox[rotation]" title="<?php echo $_product->getName();?>">
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(68, 68); ?>" width="68" height="68" alt=""/>
</a>
</li>
<?php endforeach; ?>
for standalone image:
$this->getImageLabel();
price
$price = $_product->getFinalPrice();
$base_currency_code = Mage::app()->getStore()->getBaseCurrencyCode();
$current_currency_code = Mage::app()->getStore()->getCurrentCurrencyCode();
$conversion = Mage::helper('directory')->currencyConvert($price, $base_currency_code, $current_currency_code);
echo Mage::helper('core')->currency($conversion);
store 与 API
Mage::app()->getStore() 当前store中的各种属性值
$store = Mage::app()->getStore();
$store_id = $store->getId();
$store_name = $store->getName();
$baseCurrencyCode = $store->getBaseCurrencyCode();
Getting the base currency:
Mage::app()->getStore()->getBaseCurrencyCode();
Getting the current currency:
Mage::app()->getStore()->getCurrentCurrencyCode();
Getting store URL(getUrl--->CMS/Page 中的识别符)
Mage::app()->getStore()->getUrl("about-us-german");
Mage::getStoreConfig()
Mage::getStoreConfig('design/head/title_prefix')
Mage::getStoreConfig('design/head/title_suffix')
Mage对象
Mage::getBaseUrl() 返回当前网址(Document Root所mapping 的 URL值)
<img title="Don't miss ouroffers" src="<?php echo Mage::getBaseUrl(); ?>/skin/frontend/default/rafael/img_products/dont_miss_our_offers.jpg" /></a>
针对图片,JS,CSS等前端文件的获取
针对skin中的模板图片
phtml中得到skin目录中的图片
<img title="Don't miss ouroffers" src="<?php echo Mage::getBaseUrl(); ?>/skin/frontend/default/rafael/img_products/dont_miss_our_offers.jpg" /></a>
another way to get Image URL
<img src="<?php echo $this->getSkinUrl('images/payment_logos/masterCard.png'); ?>" alt="master card"/>
Tips: getSkinUrl的起始目录即为skin。