Skip to content

Instantly share code, notes, and snippets.

@asaelx
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asaelx/242a0d5bdf79b6675f66 to your computer and use it in GitHub Desktop.
Save asaelx/242a0d5bdf79b6675f66 to your computer and use it in GitHub Desktop.

Magento Cheat Sheet

Page path:

<?php echo $this->getUrl('mypage'); ?>

Image path:

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

Product link:

<?php echo $this->getProductData()->getProductUrl(); ?>

Product name:

<?php echo $this->htmlEscape($this->getProductData()->getName()); ?>

Display all of a product’s categories:

<?php
	$categories = $_product->getCategoryIds();
	foreach($categories as $k => $_category_id):
		$_category = Mage::getModel('catalog/category')->load($_category_id);
	?>
		<a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?>
	<?php endforeach; ?>

Use a static block in a template file:

<?php echo $this->getLayout()->createBlock('cms/block')->
setBlockId('YOURBLOCKID')->toHtml(); ?>

Test for a product attribute:
<?php
	if ($this->product['attributename'] == "attributevalue") {
    // Add your stuff here
	}
?>

Test if in category:

<?php
	if ($this->getCurrentCategory()->getName() == "My Category") {
    //Add some stuff
	}
?>

Call an attribute on the product list page:

<?php
	if ($_product->getData('attributename')):
    $_staff_pick= $_product->getResource()->getAttribute ('attributename')->getFrontend()->getValue($_product);
    if ($_attributename == "Yes"){
        // attribute
    }
	endif;
?>

More functions:

$this->getCurrentCategory()->getImageUrl();
$this->getCurrentCategory()->getDescription();
$this->IsTopCategory();
$this->getProductListHtml();
$this->getCmsBlockHtml();

How to get product in stock quantity:

$_product =
 Mage::getModel('catalog/product')
 ->load($product_id);
$qty =
   Mage::getModel('cataloginventory/stock_item')
   ->loadByProduct($_product)
   ->getQty();

How to use magento "outside" magento:

require_once 'app/Mage.php';
Mage::app($yourStoreCode);
 
echo Mage::getStoreConfig('general/store_information/name');

How to "hijack" session outside magento:

require_once 'app/Mage.php';
 Mage::app($yourStoreCode);
 Mage::getSingleton('core/session'
     ,array('name'=>'frontend'))
  ->setSessionId($_COOKIE['frontend']);
 echo "cart_id="
 .Mage::helper('checkout/cart')
   ->getCart()
   ->getQuote()
   ->getId();

How to filter collection in magento 1.6 or get configurable products:

$configurable_products
  = Mage::getModel('catalog/product')
     ->getCollection()
     ->addAttributeToSelect('*')
     ->addAttributeToFiler('type_id',array('eq'=>'configurable'))
     ->load();

How to filter collection in magento 1.5 or get configurable products:

$configurable_products
 = Mage::getModel('catalog/product')
    ->getCollection()
    ->addFieldToSelect('*')
    ->addFieldToFiler('type_id',array('eq'=>'configurable'))
    ->load();

How to get configuration settings (store name as an example):

echo Mage::getStoreConfig('general/store_information/name');

How to round price:

echo Mage::getModel('sales/order')
        ->formatPricePrecision(
                 $_product->getFinalPrice()
              ,  3
          );

For use from within a Magento CMS page:

Call a static block from within a content area:

{{block type="cms/block" block_id="your_block_identifier" template="cms/content.phtml"}}

Image path from within CMS:

{{skin url="images/logo.gif"}}

Other product attributes:

Using $this->product['attributename'] syntax:
(Values for these attributes are here just as examples.)

[store_id] => 1
[entity_id] => 3
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => 2281
[category_ids] => 2,15,17,23,24
[created_at] => 2008-09-09 16:24:03
[updated_at] => 2008-09-15 22:47:51
[has_options] => 0
[name] => ProductName
[url_key] => productname
[gift_message_available] => 2
[meta_title] =>
[meta_description] =>
[image] => /p/r/product_productname.gif
[small_image] => /p/r/product_productname.gif
[thumbnail] => /p/r/product_productname.gif
[custom_design] => default/yourthemefolder
[options_container] => container2
[url_path] => productname.html
[status] => 1
[visibility] => 4
[tax_class_id] => 0
[weight] => 1.0000
[price] => 37.9900
[description] =>
[short_description] =>
[meta_keyword] =>
[custom_layout_update] =>
[media_gallery] => Array
[tier_price] => Array
[stock_item] =>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment