Created
September 25, 2015 04:05
-
-
Save growdev/28dc19a5c00a58a6e9b3 to your computer and use it in GitHub Desktop.
Search WooCommerce orders for line items with qty > 1, and multiple 'pa_color' assignments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "START\n"; | |
// Setup arguments for WP_Query | |
// set post type to WooCommerce orders | |
// set post status to published | |
// set fields to return just the post IDs | |
// set posts per page to -1 so all records are returned | |
$args = array( | |
'post_type' => 'shop_order', | |
'post_status' => 'publish', | |
'fields' => 'ids', | |
'posts_per_page'=> '-1', | |
); | |
// do the query | |
$query = new WP_Query( $args ); | |
// get an array of Order post IDs | |
$orders = $query->posts; | |
$count = 0; | |
$total = 0; | |
$qty = 0; | |
foreach ( $orders as $order_id ) { | |
// load a WC_Order object from the ID | |
$order = wc_get_order( $order_id ); | |
// Get line items for this order | |
$items = $order->get_items(); | |
if ( count( $items ) > 2 ) { | |
//echo "ID: " . $order_id . "\n"; | |
//print_r( $items ); | |
foreach ( $items as $item ) { | |
if ( $item['qty'] > 1 && $item['name'] == 'Coolest Cooler' ) { | |
// check for more than one pa_color | |
if ( 1 < count( $item['item_meta']['pa_color'] )) { | |
print_r( $item['item_meta']['pa_color'] ); | |
echo "ID (" . $order_id . ") Color: " . $item['pa_color'] . "\n"; | |
$qty ++; | |
} | |
} | |
$count ++; | |
} | |
} | |
$total++; | |
} | |
echo "Orders: " . $total . "\n"; | |
echo "Orders with more than one line item: " . $count . "\n"; | |
echo "Orders with multiple coolers: " . $qty. "\n"; | |
echo "END\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment