2

I get the following error on multiple order mails from Craft Commerce. The error points out to the lines where a macro is called:

Email template parse error for email “New order”. Order: “e35f244”. Template error: “An exception has been thrown during the rendering of a template ("Session does not exist in a console request.")

I've also seen this same error on other custom functions in the queue, but this got solved by performing a DB command instead of setting the field values directly on the element followed by the saveElement function, so the error does not only happen in this specific situation.

The mail template:

{% import "_macros/commerce" as macroCommerce %}

{% block content %}

    {% set language = "nl" %}
    
    {% set lineItemsList = {} %}
    {% for lineItem in order.lineItems %}
        {% set lineItemsListItem = {} %}
        {% set lineItemsListItem = lineItemsListItem|merge(["#{lineItem.qty} x #{lineItem.description}"]) %}
        {% if lineItem.purchasable.product.type.hasVariants %}
            {% set variantLabel = macroCommerce.getVariantLabel(lineItem.purchasable, true)|trim|striptags %}
            {% if variantLabel %}
                {% set lineItemsListItem = lineItemsListItem|merge([variantLabel]) %}
            {% endif %}
        {% endif %}

        {% set lineItemsList = lineItemsList|merge([lineItemsListItem|join(" - ")]) %}
    {% endfor %}

    {% set body = "#{"orders_new_products"|t("app", null, language = language)}
        #{lineItemsList}"
    %}
    
    {% include "_emails/template" %}

{% endblock %}

The getVariantLabel macro function in _macros/commerce.twig:

{% macro getVariantLabel(variant, showPrice = false) %}
    {% set variantLabel = {} %}
    {% set variantFields = variant.variantFields.one() %}

    {% if variantFields %}
        {% if variantFields.potSize %}
            {% set variantLabel = variantLabel|merge([variantFields.potSize]) %}
        {% endif %}

        {% if variantFields.color %}
            {% set variantLabel = variantLabel|merge([variantFields.color]) %}
        {% endif %}
    {% endif %}

    {% if showPrice %}
        {% set variantLabel = variantLabel|merge([variant.salePrice|commerceCurrency(craft.commerce.carts.cart.currency)]) %}
    {% endif %}

    {{ variantLabel|join(" - ") }}
{% endmacro %}

When the mails are sent manually from the order detailpage it does succeed. There is a cronjob set on the server that keeps the queue running automatically to keep all the functions running in the queue, so I guess that it has something to do with this.

Any solution or tips that could point me in the right direction are much appreciated!

1 Answer 1

1

This is a gotcha with the recent move to emails being sent by the queue and not during the actual checkout process.

Your problem is this line (and other lines you might have like it...):

variant.salePrice|commerceCurrency(craft.commerce.carts.cart.currency)

...when the queue runs, there is no current cart for the commerce carts service to retrieve, so therefore you also can't retrieve the currency from it.

If you change this to something direct, i.e. using the Craft currency filter, it will not raise this error.

https://docs.craftcms.com/v3/dev/filters.html#currency

variant.salePrice|currency('AUD')

...for example.

This is fine if your store only uses one currency.

If you are using multi currency, then I think you would need to retrieve the order.paymentCurrency in this case.

https://docs.craftcms.com/commerce/v3/payment-currencies.html#order-currency-fields

...in short, the one thing you can't do is call on the current cart....because there isn't one for console (queue) requests.

2
  • Thanks for your help, this solution solved the problem!
    – chlsydkysr
    Commented Aug 4, 2020 at 15:51
  • That is good to hear! Commented Aug 4, 2020 at 22:07

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.