Adding Custom Product Features to PrestaShop New Order Emails: A Developer's Guide

Customizing PrestaShop New Order Emails with Product Features

This insight addresses a common requirement for PrestaShop store owners: modifying the new order email template to include custom product features. The original forum post highlights a user, kepak, seeking to add a 'Company Owned' feature to the new order email in PrestaShop 9.0.2.

The user's initial approach involved directly editing the prestashop/mails/themes/classic/modules/ps_emailalerts/new_order.html.twig file. The challenge lies in accessing and displaying the value of the custom product feature within the email template.

The Problem: Accessing Product Features in Email Templates

The default PrestaShop email templates display basic product information like reference, name, unit price, quantity, and total price. However, accessing custom product features requires additional code to retrieve and format the data for display.

Solutions and Suggestions from the Forum

The first reply suggests modifying the code and adding a variable to the template. This is a high-level indication that the solution involves both PHP code changes and template modifications.

The second reply, from PrestaHeroes.com, suggests using ChatGPT to detail the changes needed and provides a PDF guide. This highlights the complexity of the task and the potential need for custom module development or overrides.

Steps to Implement the Solution (Based on General PrestaShop Practices)

While the provided forum thread lacks a complete solution, here's a general approach to achieve this:

  1. Identify the Module: The ps_emailalerts module is responsible for sending order-related emails.
  2. Override or Extend: To avoid modifying core files, create a custom module or override the ps_emailalerts module.
  3. Modify the PHP Logic: In the module's PHP file (likely in the hookNewOrder or similar hook), retrieve the desired product feature using PrestaShop's API. For example:
    
        $product = new Product((int)$product_id, false, $context->language->id, $context->shop->id);
        $featureValue = Product::getFeatureValueByProductId((int)$product_id, (int)$feature_id);
        $extra_vars['{company_owned}'] = $featureValue;
        
  4. Pass the Variable to the Template: Assign the retrieved feature value to a variable that can be used in the email template. This is often done by adding the variable to the $extra_vars array that is passed to the Mail::Send() function.
  5. Modify the Twig Template: Edit the new_order.html.twig file (either in the module's directory or in your theme's override directory) to display the new variable:
    
        
          {{ 'Company Owned'|trans({}, 'Emails.Body', locale)|raw }}
          {{ company_owned|raw }}
        
        

Important Considerations

  • Feature ID: Ensure you have the correct ID of the product feature you want to display.
  • Translations: Use PrestaShop's translation system to ensure your custom text is properly translated.
  • Testing: Thoroughly test your changes to ensure the email is formatted correctly and the data is accurate.

By following these steps, PrestaShop merchants and developers can successfully add custom product features to their new order email templates, providing customers with more detailed order information.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools