Answers.Body: <p>Replace my <code>if</code> condition by </p>
  
  <pre><code>if ( Craft::$app-&gt;request-&gt;getIsSiteRequest() || Craft::$app-&gt;getRequest()-&gt;getIsCpRequest() ) {
  </code></pre>
  
  <p>Thanks to Luke Holder for pushing in the right direction!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-06T15:14:16.733Z
Answers.LastActivityDate: 2019-02-06T15:14:16.733Z
Answers.UserDisplayName: Maxime Lafrenière
Answers.Body: <p>All you should need to do is remove this condition check:</p>
  
  <pre><code>if (Craft::$app-&gt;request-&gt;getIsSiteRequest()) {
  </code></pre>
  
  <p>You are only registering the twig extension for front end (site) requests, but emails can be triggered both on a cart complete front end (site) requests AND a back-end (CP) order status changes.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-04T13:49:02.507Z
Answers.Text: May I use a double condition check ? Like so :
  
  `if (Craft::$app->request->getIsSiteRequest() || Craft::$app->getRequest()->getIsCpRequest()) {`
  
  The email using `|pluralize` is sent from my other custom module, not from a Order Status Change.
Answers.UserDisplayName: Maxime Lafrenière
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-04T06:28:05.240Z
Answers.LastActivityDate: 2019-02-04T06:28:05.240Z
Answers.UserDisplayName: Luke Holder
Body: <p>I added <code>tomodomo/twig-pluralize-extension</code> to my project composer, loaded with a custom module like that :</p>
  
  <pre><code>if (Craft::$app-&gt;request-&gt;getIsSiteRequest()) {
      // Add in our Twig extension
      $pluralize = new \Tomodomo\Twig\Pluralize;
      Craft::$app-&gt;view-&gt;registerTwigExtension($pluralize);
  }
  </code></pre>
  
  <p>I use this to pluralize some string in my templates and email templates. When I check the url to my email template, everything work fine but the email didn’t send out because of "unknown function pluralize".</p>
  
  <p>I have another custom module (to handle something else in my emails) and this part work fine. </p>
  
  <p>Do I need to register/declare TwigExtensions with another method to get them work in emails ?</p>
  
  <p>Note: </p>
  
  <ol>
  <li><p>The only error related to my emails in the logs is about pluralize
  function missing.</p></li>
  <li><p>Everything work fine if I don’t use <code>|pluralize</code></p></li>
  </ol>
  
Tags: <craft3><plugin-craftcommerce2><modules><twigextension>
Title: TwigExtension added to Craft don’t work in emails
ThreadText: # TwigExtension added to Craft don’t work in emails
  
  (Asked by: username_0 on 2019-02-04)
  
  I added `tomodomo/twig-pluralize-extension` to my project composer, loaded with a custom module like that :
  
  ```
  if (Craft::$app->request->getIsSiteRequest()) {
      // Add in our Twig extension
      $pluralize = new \Tomodomo\Twig\Pluralize;
      Craft::$app->view->registerTwigExtension($pluralize);
  }
  
  ```
  
  I use this to pluralize some string in my templates and email templates. When I check the url to my email template, everything work fine but the email didn’t send out because of "unknown function pluralize".
  
  I have another custom module (to handle something else in my emails) and this part work fine.
  
  Do I need to register/declare TwigExtensions with another method to get them work in emails ?
  
  Note:
  
  1. The only error related to my emails in the logs is about pluralize
     function missing.
  2. Everything work fine if I don’t use `|pluralize`
  
  **Tags:** craft3, plugin-craftcommerce2, modules, twigextension
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2019-02-06. Score: 1 | Accepted answer
  
  Replace my `if` condition by
  
  ```
  if ( Craft::$app->request->getIsSiteRequest() || Craft::$app->getRequest()->getIsCpRequest() ) {
  
  ```
  
  Thanks to username_1 username_1 for pushing in the right direction!
  
  
  ---
  
  ### Answer by username_1 on 2019-02-04. Score: 1
  
  All you should need to do is remove this condition check:
  
  ```
  if (Craft::$app->request->getIsSiteRequest()) {
  
  ```
  
  You are only registering the twig extension for front end (site) requests, but emails can be triggered both on a cart complete front end (site) requests AND a back-end (CP) order status changes.
  
  > **Comments:**
  > **username_0**: May I use a double condition check ? Like so :  `if (Craft::$app->request->getIsSiteRequest() || Craft::$app->getRequest()->getIsCpRequest()) {`  The email using `|pluralize` is sent from my other custom module, not from a Order Status Change.  
  
  ---
  
  