Advertisement
Guest User

Untitled

a guest
Jan 13th, 2024
86
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.69 KB | Source Code | 1 0
  1. function register_pending_order_summary_page() {
  2.     add_submenu_page( 'woocommerce', 'Warehouse Picking List', 'Warehouse Picking List', 'read', 'pending_order_summary_page', 'pending_order_summary_page_callback' );
  3. }
  4.  
  5. function pending_order_summary_page_callback() {  ?>
  6.  
  7.     <div class="wrap">
  8.  
  9.         <h1>Pending Order Summary</h1>
  10.         <p id="pendingordersubtitle">This is a list of all the products needed to complete all 'Processing' orders.<br /><hr /></p>
  11.  
  12.         <?php
  13.  
  14.         // Create an array to store order line items
  15.         $output_stack = array();
  16.  
  17.         // Create an array to store order line item ids
  18.         $output_stack_ids = array();
  19.      
  20.         // Array to display the results in numerical order
  21.         $output_diplay_array = array();
  22.      
  23.         // SQL code
  24.         global $wpdb;
  25.         $order_line_items;
  26.      
  27.         // Get the order ID of processing orders
  28.         $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' ));
  29.  
  30.         // Collect the order IDs and add products to the pick list
  31.         foreach($order_ids as $an_order_id) {
  32.  
  33.             // Collect all order line items matching IDs of pending orders
  34.             $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'));
  35.          
  36.             // Extract the order line item
  37.             foreach($order_line_items as $line_item) {
  38.  
  39.                 // Add it to the array
  40.                 $pair = array($line_item->order_item_name, $line_item->order_item_id);
  41.                 array_push($output_stack, $line_item->order_item_name); // Now we've got a list of line items
  42.                 array_push($output_stack_ids, $pair); // Now we've got a list of line ids
  43.  
  44.             }
  45.         }
  46.  
  47.         // Count the number of occurrences of each value
  48.         $occurrences = (array_count_values($output_stack));
  49.  
  50.         // Print output
  51.         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>';
  52.      
  53.         if(count($occurrences) > 0){
  54.          
  55.             $cumulative_count = 0;
  56.          
  57.             // Loop through each different product in our list
  58.             foreach ($occurrences as $product_name => $quantity) {
  59.              
  60.                 // Reset cumulative_count for each product
  61.                 $cumulative_count = 0;
  62.  
  63.                 // Initialize product_id, sku, and thumbnail for the current product
  64.                 $product_id = null;
  65.                 $sku = null;
  66.                 $thumbnail = null;
  67.  
  68.                 // Go through the list of ALL line items from earlier
  69.                 foreach ($output_stack_ids as $the_line_item) {
  70.  
  71.                     // We only want to cumulatively add the quantity if the line item name matches the current product name in the list of unique products
  72.                     if ($product_name == $the_line_item[0]) {
  73.  
  74.                         // Fetch product_id from wp_wc_order_product_lookup using order_item_id
  75.                         $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]));
  76.  
  77.                         // Fetch sku from wp_postmeta using product_id
  78.                         $sku = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE post_id = %d AND meta_key = '_sku'", $product_id));
  79.  
  80.                         // Fetch thumbnail URL from wp_postmeta using product_id
  81.                         $thumbnail_id = get_post_thumbnail_id($product_id);
  82.                         $thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail');
  83.                         $thumbnail = $thumbnail_url ? $thumbnail_url[0] : '';
  84.  
  85.                         // Reset the quantity to zero, because we don't need to know the number of instances if we're counting stock
  86.                         $quantity = 0;
  87.  
  88.                         // For each unique line item, we want to retrieve the quantity
  89.                         $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]));
  90.  
  91.                         // We've now got the quantity for each line item. Add them all together.
  92.                         foreach ($order_line_item_quantity as $item_quantity) {
  93.                             // The total quantity is the combined _qty of each instance of a unique line item
  94.                             $cumulative_count = $cumulative_count + ($item_quantity->meta_value);
  95.                         }
  96.  
  97.                     }
  98.                 }
  99.  
  100.                 // Store the data in the output_diplay_array
  101.                 $output_diplay_array[] = array(
  102.                     'order_item_id' => $the_line_item[1],
  103.                     'product_id' => $product_id,
  104.                     'sku' => $sku,
  105.                     'thumbnail' => $thumbnail,
  106.                     'quantity' => $cumulative_count,
  107.                     'product_name' => $product_name
  108.                 );
  109.             }
  110.      
  111.             // Output the results in a table
  112.             foreach ($output_diplay_array as $data) {
  113.                 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>';
  114.             }
  115.      
  116.             echo '</table>';
  117.      
  118.             // Some house cleaning
  119.             $wpdb->flush();
  120.         }
  121.  
  122.     ?>
  123.  
  124.     <br /><a href="javascript:window.print()"><button type="button" class="orderprintbutton">Print</button>
  125.      
  126. </div>
  127.  
  128. <?php }
  129. add_action('admin_menu', 'register_pending_order_summary_page', 99);
  130.  
  131. // Function to apply print styling to the page
  132. add_action('admin_head', 'dashboard_print_view');
  133.  
  134. function dashboard_print_view() {
  135.     echo '<style>
  136.   @media print {
  137.    #adminmenu{display: none!important;}
  138.    div#adminmenuback {display: none!important;}
  139.    #wpcontent {margin-left: 5%!important;}
  140.    #wpfooter {display: none!important;}
  141.    .orderprintbutton {display: none!important;"}
  142.    #screen-meta {display: none!important;}
  143. }
  144.  </style>';
  145. }
  146.  
Advertisement
Comments
  • Chris_at_work
    79 days
    # text 0.40 KB | 1 0
    1. Nice work, thank you.
    2. I had to change the code like this so it could work:
    3. //Get the order ID of processing orders
    4. $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' ));
    5.  
    6. Could you add Variation SKU? At the moment, only Single Product SKUs are shown.
    7. And actual stock level would be nice, also.
Add Comment
Please, Sign In to add comment
Advertisement