1

For my WooCommerce store, I am writing a plugin that when an item is placed, creates a PDF with a picking list and a delivery label. It all works, except I can not seem to get the items in the order, I can get the order status and other parts of the order just not the items.

    // Getting an instance of the WC_Order object from a defined ORDER ID
    $order = wc_get_order( $order_id );

    // Iterating through each "line" items in the order
    foreach ($order->get_items() as $item_id => $item ) {

    // Get an instance of corresponding the WC_Product object
    $product        = $item->get_product();
    $product_name   = $item->get_name(); // Get the item name (product name)
    $item_quantity  = $item->get_quantity(); // Get the item quantity


    // Add item name and quantity to the PDF (Picking List)
    $pdf->Cell(0, 10, "Product name: '.$product_name.' | Quantity: '.$item_quantity.'", 0, 1);
}

I have tried pushing the items to the debugging log, but it isn't showing the details of the items, and so I think it is the way I am getting the items, rather than an error with writing them to the PDF.

1 Answer 1

1

First, you need to be sure that you are getting the right Order ID, as without that nothing works (just as you describe).

Try to enable WP_DEBUG as explained in How to debug in WooCommerce 3+ answer.

Then insert just before $order = wc_get_order( $order_id ); the following (temporary, for debug):

error_log( 'Order_id: ' . $order_id ); // Display the order ID in the debug.log file

if ( ! ( $order_id && $order_id > 0 ) ) {
    return; // Exit if not a numerical order ID greater than 0
}

Then when you are sure that you have the correct order ID related to the order, you can remove the debug code, and disable WP_DEBUG.


Now, there is a little mistake in the last code line (double quotes with single quotes string concatenation issue):

// Add item name and quantity to the PDF (Picking List)
  $pdf->Cell(0, 10, "Product name: '.$product_name.' | Quantity: '.$item_quantity.'", 0, 1);

Need to be replaced with:

// Add item name and quantity to the PDF (Picking List)
$pdf->Cell(0, 10, "Product name: {$product_name} | Quantity: {$item_quantity}", 0, 1);

or with:

// Add item name and quantity to the PDF (Picking List)
$pdf->Cell(0, 10, "Product name: ".$product_name." | Quantity: ".$item_quantity, 0, 1);

It should work.

Related:

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, so i know i am getting the correct order number, as i already display the order number , the order status etc. in the pdf just to show me that i am pulling the correct information. I have tried all of your suggestions, but nothing, works, i am also not getting any errors. I have tried striping back all of the plugins that aren't necessary, but still nothing.
The order number can be different from the order ID, so you need to be sure that you get the order ID.
Sorry, long day i meant the order, Id, i also get order status, customer id, date created, date modified and any other information, it just doesn't return anything to do with the items. I have removed plugins, changed the theme, it just doesn't provide the information. I have even tried it on two different servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.