• Resolved panconpescao

    (@panconpescao)


    Hi, I need to put a variable text above the invoice that shows “bluexpress” with certain shipping instances id and “starken” with other shipping instances id, is there any way I could do that?

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

    (@yordansoares)

    Hi @panconpescao,

    Try the following code snippet:

    /**
    * PDF Invoices & Packing Slips:
    * Show a custom text after the document label for certaing shipping method IDs
    */
    add_action( 'wpo_wcpdf_after_document_label', function( $document_type, $order ) {
    if ( $document_type == 'invoice' ) {
    // Check shipping methods
    if ( ! empty( $shipping_methods = $order->get_shipping_methods() ) ) {
    $first_method = array_shift( $shipping_methods );
    $shipping_method_id = $first_method->get_method_id();
    }

    if ( $shipping_method_id == 'first_shipping_id' ) {
    $custom_text = 'bluexpress';
    } elseif ( $shipping_method_id == 'second_shipping_id' ) {
    $custom_text = 'starken';
    }
    if ( $custom_text ) {
    echo "<div style='margin: -10pt 0 10pt 0'>$custom_text</div>";
    }
    }
    }, 10, 2 );

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

    Please note that you have to replace first_shipping_id and second_shipping_id with the actual shipping method IDs.

    Thread Starter panconpescao

    (@panconpescao)

    Hi, i tried the snippet but it didn’t work. i have a question: how can i put multiple shipping IDs?

    For starken i need 14, 11, 12, 24
    For bluexpress i need 15, 13, 23

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @panconpescao,

    With the following code snippet, you should be able to set different IDs per case, as you want:

    /**
    * PDF Invoices & Packing Slips:
    * Show a custom text after the document label for certaing shipping method IDs
    */
    add_action( 'wpo_wcpdf_after_document_label', function( $document_type, $order ) {
    if ( $document_type == 'invoice' ) {
    // Check shipping methods
    if ( ! empty( $shipping_methods = $order->get_shipping_methods() ) ) {
    $first_method = array_shift( $shipping_methods );
    $shipping_method_id = $first_method->get_method_id();
    }

    if ( in_array( $shipping_method_id, [ 15, 13, 23 ] ) ) {
    $custom_text = 'bluexpress';
    } elseif ( in_array( $shipping_method_id, [ 14, 11, 12, 24 ] ) ) {
    $custom_text = 'starken';
    }
    if ( $custom_text ) {
    echo "<div style='margin: -10pt 0 10pt 0'>$custom_text</div>";
    }
    }
    }, 10, 2 );

    Please note that I have used the numbers you have provided, though in my experience, shipping IDs are usually formatted using slug names like flat_rate, local_pickup, free_shipping, etc. Therefore, please double-check that you update the code snippet using the actual shipping IDs.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.