Skip to content

Instantly share code, notes, and snippets.

@lucasstark
Created December 20, 2023 12:58
Show Gist options
  • Save lucasstark/f768888d10e67108c4549c90f34985fe to your computer and use it in GitHub Desktop.
Save lucasstark/f768888d10e67108c4549c90f34985fe to your computer and use it in GitHub Desktop.
dump-woocommerce-order-information
<?php
function dump_wc_order(): void {
$order_id = null;
// Check if there's a $_GET parameter for the order ID
if (isset($_GET['debug_order_id'])) {
$order_id = intval($_GET['debug_order_id']);
} else {
return;
}
// Make sure we have an order ID
if (!$order_id) {
die('No order ID provided');
}
// Get the order
$order = wc_get_order($order_id);
if (!$order) {
die('Order not found');
}
// Get the items
$items = $order->get_items();
// Start output buffering
ob_start();
// Dump the order
echo '<h2>Order:</h2>';
// var_dump($order);
// Print some basic order info
echo '<p>Order ID: ' . $order->get_id() . '</p>';
// Dump the items
echo '<h2>Items:</h2>';
foreach ($items as $item) {
// Print some basic item info
echo '<h3>Item:</h3>';
echo '<p>Item ID: ' . $item->get_id() . '</p>';
echo '<p>Product ID: ' . $item->get_product_id() . '</p>';
echo '<p>Quantity: ' . $item->get_quantity() . '</p>';
// Dump the item meta data keys
echo '<h3>Item Meta Data Keys:</h3>';
echo '<ul>';
foreach ($item->get_meta_data() as $meta) {
echo '<li>' . $meta->key . '</li>';
}
echo '</ul>';
// Dump the item meta data with Key, Value, and Display
echo '<h3>Item Meta Data:</h3>';
echo '<ul>';
foreach ($item->get_meta_data() as $meta) {
echo '<li>';
echo '<strong>Key: ' . $meta->key . '</strong>';
echo '<br>';
if (is_array($meta->value)) {
echo '<strong>Value:</strong>';
echo '<pre>';
print_r($meta->value);
echo '</pre>';
} else {
echo 'Value: ' . $meta->value;
}
}
echo '</ul>';
}
// Get the output buffer contents and end output buffering
$output = ob_get_clean();
// Print the output and die
die($output);
}
add_action('init', 'dump_wc_order');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment