Skip to content

Instantly share code, notes, and snippets.

@corsonr
Last active July 4, 2023 08:14
Show Gist options
  • Save corsonr/a6f28280b4ad7b9e562a6b06b24a73ed to your computer and use it in GitHub Desktop.
Save corsonr/a6f28280b4ad7b9e562a6b06b24a73ed to your computer and use it in GitHub Desktop.
Loop through WooCommerce order products
<?php
$count = 1;
foreach( $order->get_items() as $item_id => $line_item ){
$item_data = $line_item->get_data();
$product = $line_item->get_product();
$product_name = $product->get_name();
$item_quantity = $line_item->get_quantity();
$item_total = $line_item->get_total();
$metadata['Line Item '.$count] = 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
$count += 1;
}
return $metadata;
@matthewjrallen
Copy link

How would I get the value of attributes selected on the item?

@danichim
Copy link

danichim commented Nov 6, 2018

`
$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