5

I have created a payment method and it has a _prepareSpecificInformation() that is extended under the Mage_Payment_Block_Info. Just need to detect where this is being called from with in the function as there are some points that I want to show everything and others, like an email, that I don't. This is what I have now.

protected function _prepareSpecificInformation($transport = null){
    $helper             = Mage::helper('centralprocessing');
    if ($this->_paymentSpecificInformation !== null) {
        return $this->_paymentSpecificInformation;
    }

    $info = $this->getInfo();

    $transport = new Varien_Object();
    $transport = parent::_prepareSpecificInformation($transport);

    $transData=array();

    $transData[Mage::helper('payment')->__('Card Type')]=$helper->getCardType($info->getCardType());
    $transData[Mage::helper('payment')->__('Masked CC Number')]='############'.$info->getMaskedCcNumber();

    $transData[Mage::helper('payment')->__('Response Return Code')]="".$info->getResponseReturnCode();
    $transData[Mage::helper('payment')->__('GUID')]=$info->getResponseGuid();
    $transData[Mage::helper('payment')->__('Approval Code')]=$info->getApprovalCode();

    $transport->addData($transData);

    return $transport;
}

So I wanted to add conditions on what is added to the $transData array depending if it's the admin or if it's in the email and such.

UPDATE The reason is that I need to put a link in with the data in the admin page only. Also turns out that

$transData[Mage::helper('payment')->__('GUID')]="<h1>HELLO</h1>";

Prints <h1>HELLO</h1> so the html is escaped first?

3 Answers 3

4
+50

Detecting when being rendered on admin pages

Here's an easy way to determine whether the block is rendered within the admin page:

$isAdminBlock = $this->getParentBlock() && $this->getParentBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Payment;

This works because the admin page will assign the payment block to be a child of Mage_Adminhtml_Block_Sales_Order_Payment. (PDFs and emails do not contain that parent block).

You can use this approach to modify your code like so:

protected function _prepareSpecificInformation($transport = null) {
    $helper = Mage::helper('centralprocessing');
    if ($this->_paymentSpecificInformation !== null) {
        return $this->_paymentSpecificInformation;
    }

    $info = $this->getInfo();

    // Here's where the detection occurs
    $isAdminBlock = $this->getParentBlock() && $this->getParentBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Payment;

    $transData = array();

    $transData[Mage::helper('payment')->__('Card Type')] = $helper->getCardType($info->getCardType());
    $transData[Mage::helper('payment')->__('Masked CC Number')] = '############'.$info->getMaskedCcNumber();

    // Here's an example of only showing certain info to admins
    if ($isAdminBlock) {
        $transData[Mage::helper('payment')->__('Response Return Code')]="".$info->getResponseReturnCode();
        $transData[Mage::helper('payment')->__('GUID')]=$info->getResponseGuid();
        $transData[Mage::helper('payment')->__('Approval Code')]=$info->getApprovalCode();
    }

    $transport = parent::_prepareSpecificInformation($transport);
    $transport->addData($transData);

    return $transport;
}

Detecting between PDFs and emails

I know this doesn't answer your specific question, but hopefully it'll help someone else with a similar issue.

It's also possible to detect whether the block is being added to a PDF. When this happens, Magento calls ->toPdf() before rendering the template, so you'd just override that method, set a flag, and check the flag later:

public function toPdf() {
    $this->setIsPdf(true);
    return parent::toPdf();
}

protected function _prepareSpecificInformation($transport = null) {
    ...

    if ($this->getIsPdf()) {
        $transData['whatever'] = 'This will only appear in PDFs (not emails)';
    }

    // You could even combine this with the admin block check:
    if (!$isAdminBlock && !$this->getIsPdf()) {
        // We're probably being rendered inside of an email
    }

    ...
}
4
  • wonderful, this makes perfect sense. I have one last part on this, that the orginal intent was to add html to $transData[Mage::helper('payment')->__('GUID')]=$info->getResponseGuid(); .. but if I go $transData[Mage::helper('payment')->__('GUID')]=$info->getResponseGuid().'<h1>HELLO</h1>'; (if the getResponseGuid() out put foo as a string) I end up with the out put of foo<h1>HELLO</h1> as an escaped string, not html, so I literally see that on the screen. Any thoughts on that?
    – Quantum
    Aug 9, 2014 at 19:25
  • The HTML is being escaped by the call to getValueAsArray() within the template. Changing the second parameter from true to false will prevent the escaping. Aug 9, 2014 at 19:48
  • An alternate approach could be modifying the template's foreach loop to check the $_label for your GUID key. If found, manually render the value, otherwise use the existing call to getValueAsArray(). Hope that helps! Aug 9, 2014 at 19:49
  • oh yes yes, should have recalled that. Thank you for the clear answer, I'll get to implementation and mark this as the solution when done.
    – Quantum
    Aug 9, 2014 at 20:06
1

The admin template that calls this function is app/design/adminhtml/default/default/template/payment/info/default.phtml but there is also a pdf that shows the payment information also, app/design/adminhtml/default/default/template/payment/info/pdf/default.phtml

A good way of finding the block you have is to turn on template hints and this will show you templates and blocks used on a page.

Looking at what you need to do with your requirements I would suggest one of the following.

  1. Rewrite the block Mage_Payment_Block_Info and check in the function if you are in the admin section via Mage::app()->getStore()->isAdmin() and then making your change,
  2. Listen to the event adminhtml_block_html_before, check for your block type and then perform the change to the html that you require.
2
  • 1
    I'm sorry I'm not sure how to apply this information to append a link only in the admin html, are you meaning that there is a global way to say I'm in this block?
    – Quantum
    Aug 8, 2014 at 5:36
  • Yes, there is a number of tools, like "template hints" (inbuilt in some Mage versions this one), that provide you with info about the block/template generating a certain <markup> in your stores/ adminhtml. Aug 8, 2014 at 14:33
-1
/app/code/core/Mage/Payment/Block/Info/Cc.php -> line 95
/app/code/core/Mage/Paypal/Block/Payment/Info.php -> line 53
/app/code/core/Mage/Payment/Block/Info.php -> line 105
/app/code/core/Mage/Sales/Block/Payment/Info/Billing/Agreement.php -> line 49
/app/code/core/Mage/Payment/Block/Info/Ccsave.php -> line 43

get this easy : http://doc-magento.com/ . just search your variable,function,calss from the right corner "search"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.