Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Change code snippets to action hooks

  • Resolved Jimmy

    (@jayem82)


    Hello again!

    I have this snippet to show total order weight (with an additional safeweight added) below order data:

    <tr class="order-weight">
        <th>Total weight:</th>
        <td>
            <?php
            $items = $this->get_order_items();
            $extra_weight = 0.2;
            if( sizeof( $items ) > 0 ) {
                foreach( $items as $item ) {
                    $weight = $extra_weight += $item['weight'] * $item['quantity'];
                }
            }
            echo $weight . ' kg';
            ?>
        </td>
    </tr>

    I’m trying to change this into an action hook for wpo_wcpdf_after_order_data, is that possible?

    Cheers!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support kluver

    (@kluver)

    Hi @jayem82,

    That certainly is possible. 🙂

    Add the following to your functions.php:

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_show_order_weight', 10, 2 );
    function wpo_wcpdf_show_order_weight ($template_type, $order) { ?>
    	<tr class="order-weight">
    	    <th>Total weight:</th>
    	    <td>
    	    	<?php
    	        $items = $order->get_items();
    	        $total_weight = 0;
    	        $extra_weight = 0.2;
    			foreach ($items as $item_id => $item) {
    				$quantity = $item->get_quantity();
    			    if ( $product = $order->get_product_from_item( $item ) ) {
    			        $weight = $product->get_weight();
    			        $total_weight += ( $weight + $extra_weight ) * $quantity;
    			    }
    			}
    	        echo $total_weight . ' kg';
    	        ?>
    	    </td>
    	</tr>
    <?php }

    With kind regards,

    Michael

    Thank you Michael!

    I’ve also created snippet for total order items quantity. Thought I’d share the action hook for that one here as well. It seems to be working, could you just confirm that I’ve done it right?

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_order_total_quantity', 10, 2 );
    function wpo_wcpdf_order_total_quantity ($template_type, $order) { ?>
        <tr class="order-quantity">
            <th>Items:</th>
            <td>
                <?php
                $items = $order->get_items();
                if( sizeof( $items ) > 0 ) {
                    foreach( $items as $item ) {
                        $qty += 1 * $item['quantity'];
                    }
                }
                echo $qty;
                ?>
            </td>
        </tr>
    <?php }
    Plugin Author Ewout

    (@pomegranate)

    Looks good to me Jimmy, well done 🙂

    Ewout

    Thanks again for your support, cheers!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change code snippets to action hooks’ is closed to new replies.