Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function register_pending_order_summary_page() {
- add_submenu_page( 'woocommerce', 'Warehouse Picking List', 'Warehouse Picking List', '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 occurrences of each value
- $occurrences = (array_count_values($output_stack));
- // Print output
- echo '<table><tr><td><strong>SKU</strong></td><td><strong>Thumbnail</strong></td><td style="width: 100px;"><strong>Quantity</strong></td><td><strong>Product</strong></td></tr>';
- if(count($occurrences) > 0){
- $cumulative_count = 0;
- // Loop through each different product in our list
- foreach ($occurrences as $product_name => $quantity) {
- // Reset cumulative_count for each product
- $cumulative_count = 0;
- // Initialize product_id, sku, and thumbnail for the current product
- $product_id = null;
- $sku = null;
- $thumbnail = null;
- // 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]) {
- // Fetch product_id from wp_wc_order_product_lookup using order_item_id
- $product_id = $wpdb->get_var($wpdb->prepare("SELECT product_id FROM {$wpdb->prefix}wc_order_product_lookup WHERE order_item_id = %d", $the_line_item[1]));
- // Fetch sku from wp_postmeta using product_id
- $sku = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE post_id = %d AND meta_key = '_sku'", $product_id));
- // Fetch thumbnail URL from wp_postmeta using product_id
- $thumbnail_id = get_post_thumbnail_id($product_id);
- $thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail');
- $thumbnail = $thumbnail_url ? $thumbnail_url[0] : '';
- // Reset the quantity 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);
- }
- }
- }
- // Store the data in the output_diplay_array
- $output_diplay_array[] = array(
- 'order_item_id' => $the_line_item[1],
- 'product_id' => $product_id,
- 'sku' => $sku,
- 'thumbnail' => $thumbnail,
- 'quantity' => $cumulative_count,
- 'product_name' => $product_name
- );
- }
- // Output the results in a table
- foreach ($output_diplay_array as $data) {
- echo '<tr><td style="border-bottom: 1px solid #ccc; padding: 10px;">' . $data['sku'] . '</td><td style="border-bottom: 1px solid #ccc; padding: 10px;"><img src="' . $data['thumbnail'] . '" width="50" height="50"></td><td style="border-bottom: 1px solid #ccc; padding: 10px;">x <b>' . $data['quantity'] . '</b></td><td style="border-bottom: 1px solid #ccc; padding: 10px;">' . $data['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>';
- }
Advertisement
Comments
-
- Nice work, thank you.
- I had to change the code like this so it could work:
- //Get the order ID of processing orders
- $order_ids = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM {$wpdb->prefix}wc_orders where status = %s or status = %s;", 'wc-on-hold', 'wc-processing' ));
- Could you add Variation SKU? At the moment, only Single Product SKUs are shown.
- And actual stock level would be nice, also.
Add Comment
Please, Sign In to add comment
Advertisement