I'l try to answer on your initial question - how to use directive with condition. For debug I use xDebug. And in my opinion its very hard to debug emails. In that case I show you an example with the same structure in another part of magento.
On sales/order/view there is same structure. The address output structure determined in Mage/Core/etc/config.xml
in default/customer/address_templates/text
.
Our task will be add any condition to show or not to show 'company name'.
Directives such as var, depend, if parse in Varien_Filter_Template
in method filter($value). In this method there is iteration of all $constructions (if/depend/var) and every $construction used in this call:
$replacedValue = call_user_func($callback, $construction);
For each if/depende/var there is its own method. Lets look at public function ifDirective($construction)
public function ifDirective($construction)
{
if (count($this->_templateVars) == 0) {
return $construction[0];
}
if($this->_getVariable($construction[1], '') == '') {
if (isset($construction[3]) && isset($construction[4])) {
return $construction[4];
}
return '';
} else {
return $construction[2];
}
}
Now lets look at protected method _getVariable. I'd like to draw your attention on one row:
} elseif (isset($stackVars[$i-1]['variable']) && $stackVars[$i-1]['variable'] instanceof Varien_Object) {
and look at the comment below:
// If object calling methods or getting properties
So to make condition our $stackVars should be an object and be an instace of Varien_Object (most of models extends from Mage_Core_Model_Abstract, which extends from Varien_Object). Lets climb a few steps above. We need to add to stackVars and object.
In my example in class Mage_Customer_Block_Address_Renderer_Default magento calls method filter($value), which I mentioned above.
Here I provide a solution. Warning! This solution is not very clean. It's purpose to show magento behavior. Please do all rewrites yourself.
public function render(Mage_Customer_Model_Address_Abstract $address, $format=null)
{
//parsing $data array
$formater->setVariables($data);
$format = !is_null($format) ? $format : $this->getFormat($address);
return $formater->filter($format);
}
In this method we will add an order object to $data array. According to my task lets add order object to this array. Do it before '$formater->setVariables($data);'
$data['order_object'] = $address->getOrder();
After this magento will parse this object and call any it's method. For example we add new method getPaymentOutput(). Put simple functionality into Order class:
public function isPrinted()
{
if ($this->getPayment()->getMethod() == 'checkmo') {
return true;
} else {
return false;
}
}
and only after that we make changes in xml directive with this:
{{if order_object.isPrinted()}}{{var smth}}<br />{{/if}}