List products with quantity, sku and brand from all WooCommerce processing orders
I have this code that creates a page in WordPress admin showing the products that are in Processing orders.
function register_pending_order_summary_page() {
add_submenu_page( 'woocommerce', 'Pending Order Summary', 'Pending Order Summary', 'read', 'pending_order_summary_page', 'pending_order_summary_page_callback' );
}
function pending_order_summary_page_callback() { ?>
<div class="wrap">
<h1>Pending Order Summary</h1>
<p id="pendingordersubtitle">This is a list of all the products needed to complete all 'Processing' orders.<br /><hr /></p>
<?php
//Create an array to store order line items
$output_stack = array();
//Create an array to store order line item ids
$output_stack_ids = array();
//Array to display the results in numerical order
$output_diplay_array = array();
//SQL code
global $wpdb;
$order_line_items;
//Get the order ID of processing orders
$order_ids = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts where post_type = %s and post_status = %s;", 'shop_order', 'wc-processing' ));
//Collect the order IDs and add products to the pick list
foreach($order_ids as $an_order_id) {
//Collect all order line items matching IDs of pending orders
$order_line_items = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_order_items where order_id = %d and order_item_type = %s;", $an_order_id->ID, 'line_item'));
//Extract the order line item
foreach($order_line_items as $line_item) {
//Add it to the array
$pair = array($line_item->order_item_name, $line_item->order_item_id);
array_push($output_stack, $line_item->order_item_name); //Now we've got a list of line items
array_push($output_stack_ids, $pair); //Now we've got a list of line ids
}
}
//Count the number of occurances of each value
$occurences = (array_count_values($output_stack));
// Print output
echo '<table><tr><td style="width: 100px;"><strong>Quantity</strong></td><td><strong>Product</strong></td></tr>';
if(count($occurences)>0){
$cumulative_count = 0;
//Loop through each different product in our list
foreach ($occurences as $product_name => $quantity) {
//Go through the list of ALL line items from earlier
foreach($output_stack_ids as $the_line_item) {
//We only want to cumulatively add the quantity if the line item name matches the current product name in the list of unique products
if ($product_name == $the_line_item[0]) {
//Reset the quanitity to zero, because we don't need to know the number of instances if we're counting stock
$quantity = 0;
//For each unique line item, we want to retrieve the quantity
$order_line_item_quantity = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta where meta_key = %s and order_item_id=%d", "_qty", $the_line_item[1]));
//We've now got the quantity for each line item. Add them all together.
foreach($order_line_item_quantity as $item_quantity) {
//The total quantity is the combined _qty of each instance of a unique line item
$cumulative_count = $cumulative_count + ($item_quantity->meta_value );
}
}
}
$quantity = $cumulative_count;
$cumulative_count = 0;
$output_diplay_array[$product_name] = $quantity;
arsort($output_diplay_array);
}
//Output the results in a table
foreach ($output_diplay_array as $product_name => $quantity) {
echo '<tr><td>' . $quantity . '</td><td>' . $product_name.'</td></tr>';
}
echo '</table>';
//Some house cleaning
$wpdb->flush();
}
?>
<br /><a href="javascript:window.print()"><button type="button" class="orderprintbutton">Print</button>
</div>
<?php }
add_action('admin_menu', 'register_pending_order_summary_page',99);
//Function to apply print styling to the page
add_action('admin_head', 'dashboard_print_view');
function dashboard_print_view() {
echo '<style>
@media print {
#adminmenu{display: none!important;}
div#adminmenuback {display: none!important;}
#wpcontent {margin-left: 5%!important;}
#wpfooter {display: none!important;}
.orderprintbutton {display: none!important;"}
#screen-meta {display: none!important;}
}
</style>';
}
Can anyone please advice on how I could also show the SKU (or maybe even a custom attribute, like brand) for each product? Currently only Quantity and Product name is shown.
Comments
Post a Comment