Sunday, August 2, 2015

Magento Cookbook - Teaching Old Dogs New Tricks, Easy Hacks to Make Magento Work Right

Magento Tricks and Hacks For Developers

In this posting, we will show you some cool and interesting hacks for developers.  These all require SSH access and R/W access to the Magento PHP code.  We have been developing custom templates and shopping carts and this is how we do it.   Magento pricing for standard carts is available here.

1) Dump Session Variables into your Magento 1column view (Magento 1.9 Trick) Put this into your 1-column phtml template file.
...
<div class="col-main">
      <pre> <?php var_dump($_SESSION); ?> </pre>
      <?php echo $this->getChildHtml('global_messages') ?>
      <?php echo $this->getChildHtml('content') ?>
</div>

...
...
<pre> <?php var_dump($_SESSION["checkout"]); ?> </pre>
<hr>
<pre> <?php var_dump($_SESSION["core"]["messages"]); ?> </pre>
<hr>
<pre> <?php var_dump($_SESSION); ?> </pre>
<hr>
<?php
   $session =  Mage::getSingleton("core/session"); 

   var_dump($session);
   Mage::register('messages_collection', 
     Mage::getSingleton('adminhtml/session')->getMessages(true));
    $_messages = Mage::registry('messages_collection');
    $_messages = $_messages->getItems();
    $_messagesData = array();
        foreach ($_messages as $_message) {
        $_messagesData[] = array(
          'type' => $_message->getType(),
          'text' => $_message->getText()
        );
        echo $_message->getText();
    }
?>


2) The following is a cheap work-around hack to fix Magento's broken user feedback mechanism.  Getting the checkout and shopping cart messages out and showing them. Use this with Magento 1.9 and it's broken messaging system.  getGroupedHTML() does not work at all, furthermore  $this->getChildHtml ('global_messages') does not work either.

<hr>
<?php echo $this->getMessagesBlock()->getGroupedHtml(); ?>
<hr>
<pre> <?php var_dump($_SESSION["checkout"]); ?> </pre>
<hr>
<pre> <?php var_dump($_SESSION["checkout"]["messages"]); ?> </pre>
<hr>
<?php
      $mess = new Mage_Core_Model_Message_Collection;
      $msgobj = new Mage_Core_Model_Message_Collection();
      $msgobj = $_SESSION["checkout"]["messages"];
      $lastmsg= $msgobj->getLastAddedMessage();
      echo var_dump($lastmsg);
      echo $lastmsg->getCode();
?>

Output looks like this, and is purely for debugging:
object(Mage_Core_Model_Message_Error)#90 (6) { ["_type":protected]=> string(5) "error" ["_code":protected]=> string(33) "Coupon code "ppppp" is not valid." ["_class":protected]=> string(0) "" ["_method":protected]=> string(0) "" ["_identifier":protected]=> NULL ["_isSticky":protected]=> bool(false) } Coupon code "ppppp" is not valid.