2

My following code is under static block and i am trying to achieve following functionality.

I print invoice using the following code:

{{if order_date == '03/06/2017'}}
    <td style='width: 15%;'>
        <span style='font-weight:bold; padding:5px; display:block; background:#e5e5e5;'>Tax(VAT/CST)</span>
    </td>
{{else}}
    <td style='width: 15%;'>
        <span style='font-weight:bold; padding:5px; display:block; background:#e5e5e5;'>Tax(GST)</span>
    </td>
{{/if}}

Update:-

My order date is 03/06/2017 and every time it is going into else statement and printing else statement instead of printing if statement.

2
  • and what is the problem?
    – Marius
    Commented Jun 16, 2017 at 14:42
  • @Marius i had updated the question with actual issue. Commented Jun 16, 2017 at 14:45

2 Answers 2

2

Pradeep, I don't believe that the template filter system and the {{if}} directive supports operators (==). If you consult the class, Varien_Filter_Template, you would find that the ifDirective parser does not look for expression evaluation in the way you would expect.

Specifically, line 248 calls $this->_getVariable, which according to your directive is receiving order_date = '03/06/2017' as the $value argument, which cannot match any values stored in the filter processor.

The _getVariable method supports only a few types of values/operations:

  • Direct variable name; eg: order_data
  • Varien_Object instances; eg: getOrderData()
  • Other objects with a corresponding method name; eg: order_data()

Another way to solve this problem is to supply a model/object in your conditional, like the order model, with a custom extending method, which you can call to to evaluate the expression outside of the filter processor, like this:

class Your_Module_Model_Order extends Mage_Sales_Model_Order
{
    public function checkOrderDate($input)
    {
        return $this->getData('order_date') == $input;
    }
}

And then in your content, rewrite the directive like so:

{{if order.checkOrderDate('03/06/2017') }}
...

Here's a complete working test case:

require_once '/vagrant/public_html/app/Mage.php';
Mage::app();

class MockOrderClass extends Varien_Object
{
    public $order_date = '03/06/2017';

    public function checkOrderDate($input)
    {
        return $this->order_date == $input;
    }
}

$filter = new Varien_Filter_Template();
$filter->setVariables(array('order' => new MockOrderClass()));

$input = <<<EOF
{{if order.checkOrderDate('03/06/2017') }}
    <td style='width: 15%;'>
        <span style='font-weight:bold; padding:5px; display:block; background:#e5e5e5;'>Tax(VAT/CST)</span>
    </td>
{{else}}
    <td style='width: 15%;'>
        <span style='font-weight:bold; padding:5px; display:block; background:#e5e5e5;'>Tax(GST)</span>
    </td>
{{/if}}
EOF;

echo '<pre>' . (htmlentities($filter->filter($input))) . '</pre>';
1
  • Actually i want to put condition like this {{if order.getShippingMethod(1).getMethod() == "pickupatstore_1"}} <div class="name"> some text </div>{{else}} <div class="name">{{var formattedShippingAddress|raw}}</div> {{/if}} but its not working in cms block magento 2. Commented Nov 15, 2018 at 13:46
0

Why you dont do your conditions with a pretty php tag in a phtml and the rendred will be displayed in your static block like this:

1) Create a phtml in :

app/design/frontend/{YourPackage}/{YourTheme}/template/cms/phtmlname.phtml

2) Put this piece of code in your static block:

{{block type="core/template" template="cms/phtmlname.phtml"}}

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.