skip to Main Content

Example: Total amount in words: 104.99 One hundred four and ninety-nine only. enter image description here

I will be displayed on my woocommerce checkout and in my woocommerce Email generated invoice.

Thank you

2

Answers


  1. Since we are not a free coding platform, this is just a little approach. Goes into your functions.php file of your child theme. Tested and works. You still need to add the string translation for total in words and maybe a language check for the formatter output language.

    /**
     * Add cart totals in words to checkout
     */
    add_action( 'woocommerce_review_order_after_order_total', 'woocommerce_review_order_after_order_total_action' );
    function woocommerce_review_order_after_order_total_action() {
        $cart_totals      = WC()->cart->get_totals();
        $number_formatter = new NumberFormatter( "en", NumberFormatter::SPELLOUT );
        ?>
        <tr class="order-total-text">
            <th>total in words</th>
            <td><?= $number_formatter->format( $cart_totals['total'] ) ?></td>
        </tr>
        <?php
    }
    
    Login or Signup to reply.
  2. add_action( 'woocommerce_review_order_after_order_total', 'woocommerce_review_order_after_order_total_action' );
    function woocommerce_review_order_after_order_total_action() {
       $number = WC()->cart->get_totals();
       $no = floor($number);
       $point = round($number - $no, 2) * 100;
       $hundred = null;
       $digits_1 = strlen($no);
       $i = 0;
       $str = array();
       $words = array('0' => '', '1' => 'one', '2' => 'two',
        '3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six',
        '7' => 'seven', '8' => 'eight', '9' => 'nine',
        '10' => 'ten', '11' => 'eleven', '12' => 'twelve',
        '13' => 'thirteen', '14' => 'fourteen',
        '15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
        '18' => 'eighteen', '19' =>'nineteen', '20' => 'twenty',
        '30' => 'thirty', '40' => 'forty', '50' => 'fifty',
        '60' => 'sixty', '70' => 'seventy',
        '80' => 'eighty', '90' => 'ninety');
       $digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
       while ($i < $digits_1) {
         $divider = ($i == 2) ? 10 : 100;
         $number = floor($no % $divider);
         $no = floor($no / $divider);
         $i += ($divider == 10) ? 1 : 2;
         if ($number) {
            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
            $str [] = ($number < 21) ? $words[$number] .
                " " . $digits[$counter] . $plural . " " . $hundred
                :
                $words[floor($number / 10) * 10]
                . " " . $words[$number % 10] . " "
                . $digits[$counter] . $plural . " " . $hundred;
         } else $str[] = null;
      }
      $str = array_reverse($str);
      $result = implode('', $str);
      $points = ($point) ?
        "." . $words[$point / 10] . " " . 
              $words[$point = $point % 10] : ''
        ?>
        <tr class="order-total-text">
            <th>Total amounts in word</th>
            <td><?= $result . "Rupees  " . $points . " Paise"; ?></td>
        </tr>
        <?php
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search