Custom email subjects for different categories

Hello,

I know we can edit the email receipt template here https://admin.foxycart.com/admin.php?ThisAction=EditTemplate&template=email

My question is: can we have a custom email subject for different categories?

Thanks
Comments
  • fc_adamfc_adam FoxyCart Team
    @alextorres,

    It is possible to customise the email subject based on the transaction, by using some Twig in the template. We have some information about those on our wiki here for your reference: https://wiki.foxycart.com/v/2.0/emails#email_receipt_placeholders

    As an example of how you could approach it:
    {% set custom_subject = "" %}
    {% for item in items %}
    {% if item.category == "Category1" %}
    {% set custom_subject = 'Custom subject for Category 1' %}
    {% elseif item.category == "Category2" %}
    {% set custom_subject = 'Custom subject for Category 1' %}
    {% endif %}
    {% endfor %}

    {% if custom_subject != "" %}
    {{ custom_subject|fc_output_data('email_subject') }}
    {% endif %}
    That code will loop through the cart items, and set a custom subject depending on what products are in the cart. If multiple products are present, then it'll set it to whatever the last item's category is. You could modify that to set variables for what products are present in the cart, and then decide on the category based on the different combinations of categories. Then finally, if the custom_subject variable isn't empty, then it sets the subject accordingly. If it is still blank though, it doesn't set the subject and so the default subject will be used.
  • Thank you @fc_adam , I'll give it a go
  • alextorresalextorres Member
    edited November 24
    Hello @fc_adam

    I'm having trouble concatenating a variable to an string to build the subject

    This is what I'm trying to do but it's giving me an error when I save the template and I'm not sure what's wrong

    {% set custom_subject = "PPX-" ~ {{ order_id }} ~ " " ~ {{ store_name }} ~ "Order " %}

    And this is the error
    Error:
    Your template was not saved because there was an error in your Twig template syntax: A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in "TEMP_email_template.twig" at line 4
    Your settings have not been saved.


    EDIT: nevermind, this


    {% set custom_subject = "PPX-" ~ order_id ~ " " ~ store_name ~ "Order " %}


    is not giving me an error anymore ^_^'
  • fc_adamfc_adam FoxyCart Team
    @alextorres,

    Glad you got it working! Yeah, as the set command is already a Twig command, you can just reference Twig variables directly within it like you've discovered.
Sign In or Register to comment.