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!