Thursday, August 26, 2010

magento bits and pieces



//this snippet will create a black bg for a product image
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->backgroundColor(array(0,0,0))->resize(135,135); ?>

//this snippet will add to cart a product in the homepage
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product) ?>')"><span ><span  style="padding:0px; font-size:12px;"><?php echo $this->__('Bestel') ?></span></span></button>

//this will get the correct translation for you custom attribute, should be enabled for product listing
$category_label = $_product->getResource()->getAttribute('category_name')->getStoreLabel($isyan);

$isyan = Mage::getModel('catalog/product')->load($_product->getId());

how to get label and value of magento attribute separately
$kwaliteit_label = $_product->getResource()->getAttribute('kwaliteit_omschrijving')->getFrontend()->getLabel($_product);
$kwaliteit_value = $_product->getResource()->getAttribute('kwaliteit_omschrijving')->getFrontend()->getValue($_product);

then echo them
echo $kwaliteit_label;
echo $kwaliteit_value;


code snippet that will ge the products of every category
_getCategoryInstance()->getProductCount() ?>

snippet that will get the homepage url of your magento website
Mage::getBaseUrl()

this snippet will load a specific directory
load(3) ?>

then you can append the methods below to get something more specific
load(3)->getUrl() ?>

methods of Magento Category

  • getName()
  • getUrl()
  • getImageUrl()
  • getProductCount()
            ////CUT THE PRODUCT NAME BECAUSE TOO LONG FOR THE TEMPLATE
          $productname = $this->htmlEscape($_product->getName());
          $productname = substr($productname , 0, 15) . '...';
  ?>

Singles

//get current controller name
$this->getRequest()->getControllerName(); //returns string

//get Action Name
$this->getRequest()->getActionName(); //returns string

//get Module Name
$this->getRequest()->getModuleName(); //returns string

//get Number of items in user cart
$this->helper('checkout/cart')->getSummaryCount(); //returns number of products

//check if user is logged in
if ($this->helper('customer')->isLoggedIn()) //returns true/false

Combinations

//check if user is on home page
if ($this->getIsHomePage()) { } //returns true/false
//check for home page only works when in header.phtml
//use method below if outside of this file or
//create new instance of:
//Mage_Page_Block_Html_Header() like so:
//$newHeaderObject = new Mage_Page_Block_Html_Header();//
if($newHeaderObject ->getIsHomePage()){} // returns true/false

//alternative method
if
(($this->getRequest()->getModuleName() == 'cms') &&
($this->getRequest()->getActionName() == 'index')) { } //returns
true/false
//use this to check if home page in other template (.phtml) files
//works by checking the module name and the action request.


//this is an alternative if getLayout is not working
<?php
$L = Mage::getSingleton('core/layout');
echo $L->createBlock('cms/block')->setBlockId('STATIC_BLOCK_IDENTIFIER')->toHtml();
?> 


Load the product attributes
$isyan=Mage::getModel('catalog/product')->load($_product->getId());

//Getting an attribute and displaying it in product view
Then use "get" + attribute code to display the value
$brandname = $isyan->getMerknaam_artikelen();

No comments:

Post a Comment