• Resolved outmaster

    (@outmaster)


    Hello!

    I am using Javascript to convert numbers to characters in the invoice template. It can Preview in HTML well, but when downloading the PDF, the converted characters cannot be displayed.

    Or do I have to put Javascript in any file or folder?

    Thanks for the advice

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Unfortunately you can’t use JavaScript in the PHP template, so you’ll need to find a PHP alternative to your method. What are you trying to achieve exactly? If you just need the total amount in words, here’s an example filter:

    
    add_action ( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_spell_out_total', 10, 2);
    function wpo_wcpdf_spell_out_total ($template_type, $order) {
    	if ( in_array( $template_type, array( 'invoice', 'credit-note' ) ) ) {
    		$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
    		$spelled_out_total = $f->format($order->get_total());
    		echo "<p><strong>Amount in words:</strong><br>" . $spelled_out_total . "</p>";
    	}
    }
    

    If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters

    Thread Starter outmaster

    (@outmaster)

    Thanks for the advice

    I put your example in the theme’s functions.php, It works

    But now I have created HTML fields for signing at the end of Invoice.

    That makes the total amount in words not in the desired position.

    If I am converting from your sample filter to PHP, put it in the invoice template, what do I do?

    Thread Starter outmaster

    (@outmaster)

    I tried this, now it works as I want.

    <td>
    <?php do_action( 'custom_amount_in_words', $this->type, $this->order ); ?>
    </td>

    and

    add_action ( 'custom_amount_in_words', 'wpo_wcpdf_spell_out_total', 10, 2);
    function wpo_wcpdf_spell_out_total ($template_type, $order) {
    	if ( in_array( $template_type, array( 'invoice', 'credit-note' ) ) ) {
    		$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
    		$spelled_out_total = $f->format($order->get_total());
    		echo "<p><strong>Amount in words:</strong><br>" . $spelled_out_total . "</p>";
    	}
    }

    Thank you so much

    Plugin Contributor Ewout

    (@pomegranate)

    Very glad to hear that! FWIW you could also call the function directly or even integrate the code in the function directly in the template. Whatever works for you 🙂

    If you can spare a minute, we always appreciate a plugin review here on wordpress.org.

    Thanks in advance and have a fantastic day!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Add Custom Javascript to invoice template’ is closed to new replies.