• Resolved Donzzzzz

    (@donzzzzz)


    I have used the below hook to match strings in the item name and change the color it works perfectly in the html view but doesnt affect the pdf view.

    add_action( 'wpo_wcpdf_after_item_meta', 'change_text_color', 10, 3 );
    function change_text_color( $template_type, $item_id, $item ) {
        ?>
    	<script src="https://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>
        <script>
            jQuery(document).ready(function($) {
                var divClass = $('.item-name');
                var text = divClass.text();
                var newText = text.replace(/(Gluten)/g, '<span style="color: red;">$1</span>');
                newText = newText.replace(/(Free)/g, '<span style="color: green;">$1</span>');
                newText = newText.replace(/(string3)/g, '<span style="color: blue;">$1</span>');
                divClass.html(newText);
            });
        </script>
    	
        <?php
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @donzzzzz,

    You can not apply scripts within the PDF documents, but you can achieve the same with this code snippet, that I just wrote for you:

    /**
     * PDF Invoices & Packing Slips for WooCommerce:
     * Highlight specific words in the item names
     */
    add_filter( 'wpo_wcpdf_html_filters', function( $filters ) {
    	$filters[] = array( 'woocommerce_order_item_name', 'wpo_wcpdf_hihglight_item_name', 999, 3 );
    	return $filters;
    } );
    function wpo_wcpdf_hihglight_item_name( $item_name, $item, $is_visible ) {
        $item_names_to_highlight = array(
            /* Set the word(s) to search and the color to apply, one per line */
            '(Gluten)' => 'red',
            '(Free)' => 'green',
            '(string3)' => 'blue',
        );
        foreach ( $item_names_to_highlight as $target => $color) {
            if ( strpos( $item_name, $target ) !== false ) {
                $item_name = str_replace( $target,  "<span style=\"color: {$color};\">{$target}</span>", $item_name );
            }
        }
    	return $item_name;
    }

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

    Thread Starter Donzzzzz

    (@donzzzzz)

    Hi,

    Thanks for getting back, i tried adding the above code but it doesnt work on the HTML and also the pdf.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Where are you adding the code snippet?

    I have tested it and it is working as expected. Please note that the code is case-sensitive, that is, you need to write the word(s) exactly as it appears in the item name.

    E.g. if the part you want to highlight is “Free”, you have to enter “Free” with the “F” capitalized. If you enter “free” instead, the word will not be highlighted. The same applies to the punctuation marks.

    Thread Starter Donzzzzz

    (@donzzzzz)

    Hi,
    I had not removed the (). its works like a charm. Thanks

    • This reply was modified 1 year, 11 months ago by Donzzzzz.
    Plugin Contributor Yordan Soares

    (@yordansoares)

    I’m glad to hear that it is working now, @donzzzzz!

    If you don’t mind and have the time, do you think you could leave us a review?

    Thanks in advance and all the best with your store!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Color Strings in Item name’ is closed to new replies.