当前位置

网站首页> 程序设计 > 开源项目 > 程序开发 > 浏览文章

Magento code cheatsheet

作者:小梦 来源: 网络 时间: 2024-08-02 阅读:

Add JS and CSS from Layout

<action method="addItem">  <type>skin_css</type>  <name>css/yourstyle.css</name></action><action method="addItem">  <type>skin_js</type>  <name>js/yourscript.js</name>  <params/></action>

Calling Static Blocks directly from .phtml files

echo $this->getLayout()->createBlock("cms/block")->setBlockId("block_identifier")->toHtml();

Breadcurmbs anywhere

echo $this->getLayout()->getBlock("breadcrumbs")->toHtml();

Calling Block/Template from admin CMS pages and static blocks

{{ block type="module/block" template="module/template.phtml" category_id="25" }}

Image re-size in Magento

echo $this->helper('catalog/image')->init($_product, 'image')    ->constrainOnly(TRUE)    ->keepAspectRatio(TRUE)    ->keepFrame(FALSE)    ->resize(150,null)

Keep height fixed (In the code below, fixed to 200px)

echo $this->helper('catalog/image')->init($_product, 'image')    ->constrainOnly(TRUE)    ->keepAspectRatio(TRUE)    ->keepFrame(FALSE)    ->resize(null,200)

Re-size image proportionally and keep the image width and height not greater than the specified width and height

echo $this->helper('catalog/image')->init($_product, 'image')    ->constrainOnly(TRUE)    ->keepAspectRatio(TRUE)    ->keepFrame(FALSE)    ->resize(200,200)

In the example above, either width or height, whichever is bigger will be re-sized to 200px and the other will be re-sized proportionally.
Re-size image proportionally to the specified width and height
In the example above, if the original image’s height was bigger than the width, height will be 200px and width will be smaller. However, if we want the new re-sized image to be 200px by 200px, the following code will make the height 200px and width will also be 200px by filling in the blank space with white.

echo $this->helper('catalog/image')->init($_product, 'image')    ->constrainOnly(TRUE)    ->keepAspectRatio(TRUE)    ->keepFrame(TRUE)    ->resize(200,200)

Working with product attributes

// Get attribute collection$attribute = $_product->getResource()->getAttribute('my_attribute');// Get attribute label$attribute->getFrontendLabel();// Get attribute default value$attribute->getDefaultValue();// Get attribute value$attributeValue = Mage:getModel('catalog/product')    ->load($_product->getId())->getMyAttribute();

Get information

//Get host nameecho Mage::app()->getFrontController()->getRequest()->getHttpHost();//Get base direcroryecho Mage::getBaseDir();//Get base URLecho Mage::getBaseURL();//or echo Mage::getURL();//Get default skin folderecho $this->getSkinURL();//Get the referer URL$this->getRequest()->getServer('HTTP_REFERER'); //Get Product Name$product->getName();//Get product sku$product->getSku();//Get product stockMage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();//Get a product attribute value (like color/size)$product->getResource()->getAttribute('color')->getFrontend()->getValue($product);//checks if a product is salable$product->isSaleable();//checks if a product type is salable$product->isisAvailable();//Gets the product final price after all catalog rules have been appliedMage::getModel('catalogrule/rule')->calcProductPriceRule($product,$product->getPrice());//Gets the product tier price if can be applied to the customer$product->getTierPrice(1);

热点阅读

网友最爱