Variable Substitution

Hello,
Please i need assistance using the Variable Substitution for email template notification

The precise area i need it is for the invoice validation capture which should contain the following

Product Name
Product Price
Product Qty
Total

Using the email template, the content didn’t have the variable listed for Product Name, Product Price and Product Quantity.

How can i get these variables so i can set the template?

Secondly is, we have more than one business Sales location and we want to get notification for every invoice validated to be sure of the pricing used. How do i make the notification to be location based so that the notification comes only from one particular location?

Thank you

hi,
look at that .

Hi @Grelf , thank you for the assistance.

However, i have already gone this route, it doesn’t contain Variable for products, pricing or quantity

Any way to add or include and from where ?

Step 1: Create a Centralized Substitution File

  1. Create a centralized substitution file in the core/substitutions/ directory. For example, name it functions_custom_substitution.lib.php.

[code=php]<?php

/**

  • Add custom substitution variables for quotes and supplier orders.

  • @param string $string The original string with placeholders.

  • @param CommonObject $object The object (proposal, supplier order, etc.) related to the template.

  • @param Translate $outputlangs The output language object.

  • @return string The string with custom substitutions.
    */
    function custom_substitution($string, $object, $outputlangs)
    {
    global $conf, $langs, $db;

    // Fetch products from $object (should be a proposal or supplier order)
    if ($object->element == ‘propal’ || $object->element == ‘commande_fournisseur’) {
    $products = ;
    foreach ($object->lines as $line) {
    $products = [
    ‘name’ => $line->product_label,
    ‘price’ => price($line->subprice),
    ‘qty’ => $line->qty,
    ‘total’ => price($line->subprice * $line->qty)
    ];
    }

     // Replace custom placeholders with actual values
     $string = str_replace('__PRODUCT_NAME__', implode(", ", array_column($products, 'name')), $string);
     $string = str_replace('__PRODUCT_PRICE__', implode(", ", array_column($products, 'price')), $string);
     $string = str_replace('__PRODUCT_QTY__', implode(", ", array_column($products, 'qty')), $string);
     $string = str_replace('__PRODUCT_TOTAL__', implode(", ", array_column($products, 'total')), $string);
    

    }

    return $string;
    }

// Register the custom substitution function
$substitutionarray[‘custom’] = ‘custom_substitution’;
[/code]

Step 2: Include the Substitution File in Existing Modules

  1. Modify the description files of the relevant modules to include this substitution file. For example, for the proposal and supplier order modules, edit moddesc.propal.class.php and moddesc.commande_fournisseur.class.php.

For Proposals:

// In htdocs/core/modules/propal/moddesc.propal.class.php
include_once DOL_DOCUMENT_ROOT.'/core/substitutions/functions_custom_substitution.lib.php';

// Register the function
$substitutionarray['custom'] = 'custom_substitution';

For Supplier Orders:

// In htdocs/core/modules/fournisseur/moddesc.commande_fournisseur.class.php
include_once DOL_DOCUMENT_ROOT.'/core/substitutions/functions_custom_substitution.lib.php';

// Register the function
$substitutionarray['custom'] = 'custom_substitution';

Step 3: Enable Custom Substitutions

  1. Ensure that the relevant modules are enabled and configured to use custom substitutions.

Step 4: Use the Substitution Variables in Email Templates

  1. Use the substitution variables in your email templates as previously explained:

Example email template:

[code]Dear Customer,

Thank you for your order. Here are the details of your order:

Product Name: PRODUCT_NAME
Product Price: PRODUCT_PRICE
Product Quantity: PRODUCT_QTY
Total: PRODUCT_TOTAL

Best regards,
Your Company[/code]

Notes

  • Centralizing the substitution file reduces code duplication and makes maintenance easier.
  • Make sure to test your modifications in a development environment before applying them to production.
  • Monitor Dolibarr updates and adapt your files accordingly to avoid conflicts.

Hi @Grelf ,

What else can i say? this is a great work, deeply appreciated.
I am not a tech or coding person and my greatest fear in all this is putting this things in the wrong line, place and everything stops working.

I would appreciate it more (of course i want to learn) if its done simply the way i should go about it. Like the first thing to do and the line where it should be inserted, sequentially like that…

Like for number 1, i dont know what to copy, there is an image in between and so i dont get confused.

Pls help align.

Then what i need is the customer invoice validation object - The product Name, The price and Qty, why is it from proposal and supplier order? Can you explain?

Thank you