Monday, March 28, 2011

Magento Debugging Tips



Referenced from Inchoo

Snippet 1: Check if variable is object and of which class
<?php Zend_Debug::dump(get_class($this), 'get_class') ?>
<?php
/**
 * Once you do get_class you will get a class name.
 * With class name you can do something like
 * $this = new Mage_Page_Block_Html_Header();
 * then IDE will give you autocomplete on things like "$this->"
 *
 * Just remember to comment the out the
 * //$this = new Mage_Page_Block_Html_Header();
 * once you are done
 */
?>

Snippet 2: Do a basic dump of variable to see its "content"/value

<?php Zend_Debug::dump($this->debug(), 'debug') ?>

Snippet 3: Read the value of object properties/attributes

(Note that $_product var is just example, it can be any Magento/Varien object)

<?php Zend_Debug::dump($_product->getData('attribute_name')) ?> 

Snippet 4: Read the value of object properties/attributes

(Does the same thing as Snippet 3)

<?php Zend_Debug::dump($_product->getAttributeName()) ?>

Snippet 5: Compare the value of attribute/property to some other value

<?php if($_product->getSku() == 'sku-xxx-ppp-222'): ?>
    Ouput something only if product Sku is equal to 'sku-xxx-ppp-222'.
<?php endif; ?> 

Snippet 6: Loop trough Magento collection object

(Check if something is a collection, if we can iterate trough it)

<?php if($someVar instanceof Varien_Data_Collection): ?>
    
    <?php foreach($someVar as $k => $v): ?>
  • Some value: < ?php echo $v ?>

No comments:

Post a Comment