3

I need to customise the contact form of our magento project, so that there are 2 different forms, one is a basic contact form, the other one is tailored to specific clients, so they both have really different fields. Since both forms will share the same email template (not sure why magento is built like that, you should be able to set new templates in the admin contacts preferences) i want to use some conditionals. I managed to use DEPENDS to only show the relevant fields, but then all white space from the code would appear in the email, which looks really strange. So i decided to code around using IF, thinking this will take care of the blank lines (still have to prove that though).

Please have a look at the below code and let me know if you can see something strange. If i take the conditionals out and use DEPEND on each field, it comes to my inbox just fine, but this will not work:

<!--@subject Submitted Form@--> 
<!--@vars {
    "var data.name":"Sender Name", 
    "var data.email":"Sender Email", 
    "var data.telephone":"Sender Telephone", 
    "var data.comment":"Comment"
} @-->

{{if data.type}}
   Type: {{var data.type}}
   Name: {{var data.name2}}
   Job Title: {{var data.jobTitle}} 
   Company: {{var data.company}} 
   Address: {{var data.address}}
   Contact Number: {{var data.contactNumber}} 
   E-mail: {{var data.email2}} 
   Telephone: {{var data.telephone}} 
   Enquiries: {{var data.enquiries}}
{{else}}
   Name: {{var data.name}}
   E-mail: {{var data.email}}
   Comment: {{var data.comment}}
{{/if}}

Can you spot something weird? I can get the email in the ELSE branch, so it looks like the data.type is not getting through? The data.type should work, as it comes for a select in the form:

<select name="type">
    <option value="">Select...</option>
    <option value="Hotelier">I am a Hotelier</option>
    <option value="Retail Customer">I am a Retail Customer</option>
</select>

Any ideas?

2
  • Try to send data.type in the else email to see if it has anything. I dont quite understand what are you doing, but i think the tpe field is not being received by the action
    – mbalparda
    Commented Jul 31, 2014 at 9:15
  • See Varien_Filter_Template. It does the template filtering/var replacing. The if statement isn't what you'd expect. It doesn't work exactly as in Php. Debug/log the data object values and you'll know. If you modify the code care security (no use of eval or other security risks). On some Magento versions it translates the labels of options, on other it doesn't, others sends the value. I do not know why this class wasn't improved by now. Commented Jul 31, 2014 at 9:21

1 Answer 1

3

Eh maybe I am going mad because it is late when I answered this but it looks like there could be a error here with Magento's standard regex for email template processing.

The regex is defined in Varien_Filter_Template as follows.

/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si

But to my eyes it looks like there is one too many slashes in the closing if statement. I have copied this regex into a few online regex tools and the error is highlighted. If you change this error in the online tools and then give it the text you have provided then the regex matches and all seems to work.

This is really odd but could be a magento error that needs working on.

For a temporary fix what you can do is create a new module, rewrite the class Mage_Core_Model_Email_Template_Filter and then update the constant CONSTRUCTION_IF_PATTERN

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.