• Resolved christianserrer

    (@christianserrer)


    Hello everyone,

    is there a possibility that I can display a note in the invoice, conditional on the products in the cart? (If one special product is in the cart there should be a note on the invoice about the taxation.)
    Would be glad if someone could help us.

    Many thanks,
    Christian

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor kluver

    (@kluver)

    Hi @christianserrer,

    That is certainly possible. You will need to add a code snippet that checks for certain (parent)product ids like this:

    add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_add_note_based_on_product_id', 10, 2 );
    function wpo_wcpdf_add_note_based_on_product_id ($template_type, $order) {
        if ($template_type == 'invoice' ) {
            $items = $order->get_items();
            $product_ids = array('123', '456');
            foreach ($items as $item_id => $item) {
                if ( $product = $order->get_product_from_item( $item ) ) {
                    ( $product_id = $product->get_parent_id() ) ? $product_id = $product->get_parent_id() : $product_id = $product->get_id();
                    if ( in_array( $product_id, $product_ids ) ) {
                        echo "<p>Your note goes here.</p>";
                    }
                }
            }
        }
    }

    You can add all the product ids that should trigger the message to the $product_ids array. This code snippet should be placed in the functions.php of your child theme. If you haven’t worked with code snippets/filters or functions.php before please read this: How to use filters

    • This reply was modified 6 years, 7 months ago by kluver.
    Thread Starter christianserrer

    (@christianserrer)

    Hi @kluver,
    thanks for the fast answer. The code works perfect. Is there a posibility to modify the code so that we can use different notes in the invoice for different products. I tried copying the code twice in the functions.php but that don′t worked.

    It would be great if you could help us.

    Many thanks,
    Christian

    Plugin Contributor Ewout

    (@pomegranate)

    Hello Christian,
    You can copy the function but you need to rename it then because you can’t have two functions by the same name. However, you can also do this within one function, which makes it more organized. Here’s a rewritten example:

    
    add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_add_note_based_on_product_id', 10, 2 );
    function wpo_wcpdf_add_note_based_on_product_id ($template_type, $order) {
        if ($template_type == 'invoice' ) {
            // set notes for products
            $product_notes = array(
                array(
                    'note'        => 'Note 1',
                    'product_ids' => array( 123, 456 ),
                ),
                array(
                    'note'        => 'Note 2',
                    'product_ids' => array( 234, 567 ),
                ),
            );
    
            // get list of product ids in the order
            $product_ids = array();
            $items = $order->get_items();
            foreach ($items as $item_id => $item) {
                $product_ids[] = (int) $item->get_product_id();
                $product_ids[] = (int) $item->get_variation_id();
            }
            $product_ids = array_unique( $product_ids );
    
            // show product notes when matches found
            foreach ($product_notes as $product_note) {
                if ( count( array_intersect( $product_ids, $product_note['product_ids'] ) ) > 0 ) {
                    printf('<p>%s</p>', $product_note['note']);
                }
            }
        }
    }
    

    If you need more help with this I recommend that you look for a developer, as this is quite advanced and somewhat beyond what we can offer as part of free support, I hope you understand!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional note’ is closed to new replies.