Jump to content

Edit History

Hello @Eutanasio,

To display the total paid WT in Words on PDF Invoices in Prestashop 1.7 please follow below steps:
Step 1: Create a Custom Function to Convert Numbers to Words
Add the custom function in a file that can be included easily. One suitable place is in a custom helper file.

File Path: override/classes/helper/Helper.php
Code:

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}


class Helper extends HelperCore
{
    public static function convert_number_to_words($num = false, $currency = 'dollars', $subunit = 'cents')
    {
        if (!is_numeric($num)) {
            return false;
        }

        $num = number_format($num, 2, '.', '');
        list($integerPart, $fractionPart) = explode('.', $num);

        $words = self::convert_integer_to_words($integerPart);
        $words .= ' ' . $currency;

        if ($fractionPart > 0) {
            $words .= ' and ' . self::convert_integer_to_words($fractionPart) . ' ' . $subunit;
        }

        return ucfirst(trim($words)) . '.';
    }

    private static function convert_integer_to_words($num)
    {
        $num = (int) $num;
        $list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
            'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
        );
        $list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
        $list3 = array('', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
            'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
            'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
        );

        $num_length = strlen($num);
        $levels = (int) (($num_length + 2) / 3);
        $max_length = $levels * 3;
        $num = substr('00' . $num, -$max_length);
        $num_levels = str_split($num, 3);
        $words = array();

        for ($i = 0; $i < count($num_levels); $i++) {
            $levels--;
            $hundreds = (int) ($num_levels[$i] / 100);
            $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred ' : '');
            $tens = (int) ($num_levels[$i] % 100);
            $singles = '';

            if ($tens < 20) {
                $tens = ($tens ? ' and ' . $list1[$tens] . ' ' : '');
            } elseif ($tens >= 20) {
                $tens = (int)($tens / 10);
                $tens = ' and ' . $list2[$tens] . ' ';
                $singles = (int) ($num_levels[$i] % 10);
                $singles = ' ' . $list1[$singles] . ' ';
            }

            $words[] = $hundreds . $tens . $singles . (($levels && (int)($num_levels[$i])) ? ' ' . $list3[$levels] . ' ' : '');
        }

        $words = implode(' ', $words);
        $words = preg_replace('/^\s*\band\b\s*/', '', $words);

        return trim($words);
    }
}

Step 2: Override the PDF Generation Logic
Override the PDF.php file to include the total paid in words. Create an override file if it doesn't exist.

File Path: override/classes/pdf/HTMLTemplateInvoice.php
Code:

<?php
class HTMLTemplateInvoice extends HTMLTemplateInvoiceCore
{
    public function getContent()
    {
        $order = new Order((int)$this->order->id);
        $total_paid = $order->total_paid;
        $currency = new Currency($order->id_currency);
        
        // Define currency names
        $currencyNames = array(
            'USD' => array('dollars', 'cents'),
            'EUR' => array('euros', 'cents'),
            'GBP' => array('pounds', 'pence'),
            // Add other currencies as needed
        );
        
        $currencyName = isset($currencyNames[$currency->iso_code]) ? $currencyNames[$currency->iso_code][0] : 'dollars';
        $subunitName = isset($currencyNames[$currency->iso_code]) ? $currencyNames[$currency->iso_code][1] : 'cents';
        
        $total_paid_in_words = Helper::convert_number_to_words($total_paid, $currencyName, $subunitName);
        
        $this->smarty->assign(array(
            'total_paid_in_words' => $total_paid_in_words,
        ));
        
        return parent::getContent();
    }
}



 

Hello @Eutanasio,

the To display the total paid WT in Words on PDF Invoices in Prestashop 1.7 please follow below steps:
Step 1: Create a Custom Function to Convert Numbers to Words
Add the custom function in a file that can be included easily. One suitable place is in a custom helper file.

File Path: override/classes/helper/Helper.php
Code:

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}


class Helper extends HelperCore
{
    public static function convert_number_to_words($num = false, $currency = 'dollars', $subunit = 'cents')
    {
        if (!is_numeric($num)) {
            return false;
        }

        $num = number_format($num, 2, '.', '');
        list($integerPart, $fractionPart) = explode('.', $num);

        $words = self::convert_integer_to_words($integerPart);
        $words .= ' ' . $currency;

        if ($fractionPart > 0) {
            $words .= ' and ' . self::convert_integer_to_words($fractionPart) . ' ' . $subunit;
        }

        return ucfirst(trim($words)) . '.';
    }

    private static function convert_integer_to_words($num)
    {
        $num = (int) $num;
        $list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
            'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
        );
        $list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
        $list3 = array('', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
            'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
            'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
        );

        $num_length = strlen($num);
        $levels = (int) (($num_length + 2) / 3);
        $max_length = $levels * 3;
        $num = substr('00' . $num, -$max_length);
        $num_levels = str_split($num, 3);
        $words = array();

        for ($i = 0; $i < count($num_levels); $i++) {
            $levels--;
            $hundreds = (int) ($num_levels[$i] / 100);
            $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred ' : '');
            $tens = (int) ($num_levels[$i] % 100);
            $singles = '';

            if ($tens < 20) {
                $tens = ($tens ? ' and ' . $list1[$tens] . ' ' : '');
            } elseif ($tens >= 20) {
                $tens = (int)($tens / 10);
                $tens = ' and ' . $list2[$tens] . ' ';
                $singles = (int) ($num_levels[$i] % 10);
                $singles = ' ' . $list1[$singles] . ' ';
            }

            $words[] = $hundreds . $tens . $singles . (($levels && (int)($num_levels[$i])) ? ' ' . $list3[$levels] . ' ' : '');
        }

        $words = implode(' ', $words);
        $words = preg_replace('/^\s*\band\b\s*/', '', $words);

        return trim($words);
    }
}

Step 2: Override the PDF Generation Logic
Override the PDF.php file to include the total paid in words. Create an override file if it doesn't exist.

File Path: override/classes/pdf/HTMLTemplateInvoice.php
Code:

<?php
class HTMLTemplateInvoice extends HTMLTemplateInvoiceCore
{
    public function getContent()
    {
        $order = new Order((int)$this->order->id);
        $total_paid = $order->total_paid;
        $currency = new Currency($order->id_currency);
        
        // Define currency names
        $currencyNames = array(
            'USD' => array('dollars', 'cents'),
            'EUR' => array('euros', 'cents'),
            'GBP' => array('pounds', 'pence'),
            // Add other currencies as needed
        );
        
        $currencyName = isset($currencyNames[$currency->iso_code]) ? $currencyNames[$currency->iso_code][0] : 'dollars';
        $subunitName = isset($currencyNames[$currency->iso_code]) ? $currencyNames[$currency->iso_code][1] : 'cents';
        
        $total_paid_in_words = Helper::convert_number_to_words($total_paid, $currencyName, $subunitName);
        
        $this->smarty->assign(array(
            'total_paid_in_words' => $total_paid_in_words,
        ));
        
        return parent::getContent();
    }
}


Step 3: Update the Template
Update the invoice template to display the total paid in words.

File Path: themes/<your-theme>/pdf/invoice.total-tab.tpl
Screenshot: https://prnt.sc/_CjxzH6dOjHr
Code:
 

<tr class="bold">
    <td class="grey">
        {l s='Total (in words)' d='Shop.Pdf' pdf='true'}
    </td>
    <td class="white">
        {$total_paid_in_words}
    </td>
</tr>

Step 4: Clear Cache

After making these changes, clear the cache to ensure the updates take effect. You can do this from the back office:
Go to Advanced Parameters > Performance.
Click on Clear Cache.

Preview: https://prnt.sc/BqdUEbSO2_cT
This should ensure that the total paid in words appears on your PDF invoices in Prestashop 1.7.
 

×
×
  • Create New...