Email template

jrwhipjrwhip Member
in Help edited November 2014
I need to set up some simple emails to send to different people in order to fulfill an order. The emails just need basic order information such as Customer Name, address, email etc. Along with products, and some custom fields. How do I code this? Can you give me just a simple example like.
A new order arrived from { order.name }
They ordered { item.product }
Custom Field { item.age }
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @jrwhip,

    So for that you could make use of the category admin emails - which send an email receipt if any products from within that category as involved in a given transaction. Note that if products from multiple categories are within an order, an email per category would be sent.

    In terms of outputting information about the order - for that you'll use Twig. You can see an overview of all the information available to you here: https://wiki.foxycart.com/v/2.0/templates/view_data and to give you an example of how your simple example would look, that would look like:
    A new order from {{ shipping_address.first_name }} {{ shipping_address.last_name }}.
    They ordered:
    {% for item in items %}
      * {{ item.name }}
    {% endif %}
    
    Custom Fields:
    {% if custom_fields|length > 0 %}
    {% for key,custom_field in custom_fields %}
    {% if not custom_field.is_hidden %}
      * {{ key|replace({'_': ' '})|title }}: {{ custom_field.value }}
    {% endif %}
    {% endfor %}
    {% endif %}
    

    Note that the items and custom fields are both groups of items, so they need to be looped over to be accessed.

    You can also see some information about working with Twig on their website at http://twig.sensiolabs.org/doc/templates.html
  • I tried customizing the code, but I keep getting an error that will not let me save it. I even tried just pasting in the exact code you send and that didn't work either.

    The error is always something like this:

    Your template was not saved because there was an error in your Twig template syntax: Unexpected tag name "endif" (expecting closing tag for the "for" tag defined near line 4) in "TEMP_admin_email_template.twig" at line 5

    I am still very unclear about the syntax of this code and what items should be changed to mach the items in my cart.

    Can you send me an example of how I would do it with one product named: box and two custom fields the first named: age and the second named: gender

    Thanks again for all your help.
  • fc_adamfc_adam FoxyCart Team
    @jrwhip,

    Oh sorry - that's my fault. I mistyped a tag.

    What it should be is this:
    A new order from {{ shipping_address.first_name }} {{ shipping_address.last_name }}.
    They ordered:
    {% for item in items %}
      * {{ item.name }}
    {% endfor %}
     
    Custom Fields:
    {% if custom_fields|length > 0 %}
    {% for key,custom_field in custom_fields %}
    {% if not custom_field.is_hidden %}
      * {{ key|replace({'_': ' '})|title }}: {{ custom_field.value }}
    {% endif %}
    {% endfor %}
    {% endif %}
    

    That would output any product names that are in the cart, as well as any custom fields that are present in the cart. Did you need to restrict the output to only displaying those two specific custom fields, or would they be the only custom fields present for the transaction?
  • Actually outputting all custom fields would be great.

    I tried pasting the code you sent, and I was able to save it, but the email the system sent me on a test order did not include any custom fields. It did include the customers name and product ordered. I checked the transaction record and the custom fields are there. Is there something else I need to do?
  • fc_adamfc_adam FoxyCart Team
    @jrwhip,

    When you say custom field - do you mean a custom field you're adding on the checkout, or a custom attribute added to a product?
  • I am actually not sure of the difference. I add them to the cart like so:
    <input type="text" name="h:age">
    <input type="text" name="h:current_weight">
    
  • fc_adamfc_adam FoxyCart Team
    @jrwhip,

    Ah ok - so they're hidden session variables. With the Twig below, that's actually only display session variables that aren't set to be hidden, such as custom checkout fields:
    {% if custom_fields|length > 0 %}
    {% for key,custom_field in custom_fields %}
    {% if not custom_field.is_hidden %}
      * {{ key|replace({'_': ' '})|title }}: {{ custom_field.value }}
    {% endif %}
    {% endfor %}
    {% endif %}
    


    Give this a try:
    {% if custom_fields|length > 0 %}
    {% for key,custom_field in custom_fields %}
    {% if key in ["age", "current_weight"] %}
      * {{ key|replace({'_': ' '})|title }}: {{ custom_field.value }}
    {% endif %}
    {% endfor %}
    {% endif %}
    
  • I actually have several of these hidden session variables. Should I add them all to that array? or is there a way to list all hidden session variables no matter what they are?
  • fc_adamfc_adam FoxyCart Team
    @jrwhip,

    You could simply do it like this:
    {% if custom_fields|length > 0 %}
    {% for key,custom_field in custom_fields %}
      * {{ key|replace({'_': ' '})|title }}: {{ custom_field.value }}
    {% endfor %}
    {% endif %}
    

    Which would display all of them. If that makes it that some you don't want displayed start displaying though, you'll need to switch to the previous approach and add each name into the array.
  • That worked perfectly. Thanks!!!!!
Sign In or Register to comment.