Skip to content

Instantly share code, notes, and snippets.

@danichim
Created November 6, 2018 09:58
Show Gist options
  • Save danichim/09591c25c5d706fb9b9ce28aaf206fdc to your computer and use it in GitHub Desktop.
Save danichim/09591c25c5d706fb9b9ce28aaf206fdc to your computer and use it in GitHub Desktop.
Loop through WooCommerce order products
$filters = array(
'post_status' => 'any',
'post_type' => 'shop_order',
'posts_per_page' => 200,
'paged' => 1,
'orderby' => 'modified',
'order' => 'ASC'
);
$loop = new WP_Query($filters);
while ($loop->have_posts()) {
$loop->the_post();
$order = new WC_Order($loop->post->ID);
foreach ($order->get_items() as $key => $lineItem) {
//uncomment the following to see the full data
// echo '<pre>';
// print_r($lineItem);
// echo '</pre>';
echo '<br>' . 'Product Name : ' . $lineItem['name'] . '<br>';
echo 'Product ID : ' . $lineItem['product_id'] . '<br>';
if ($lineItem['variation_id']) {
echo 'Product Type : Variable Product' . '<br>';
} else {
echo 'Product Type : Simple Product' . '<br>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment