我有这个代码,它在WordPress管理中创建一个页面,显示正在处理订单的产品.


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>';
}

有没有人可以建议我如何显示每个产品的SKU(或者甚至是一个定制属性,比如品牌)?目前只显示数量和产品名称.

代码作者:https://shanegowland.com/advanced-woocommerce/2021/list-the-products-needed-to-fulfill-all-pending-orders-in-woocommerce/

推荐答案

try 以下方法,使用与WooCommerce High-Performance Order Storage (HPOS)兼容的WC_Order_Query,并显示产品名称和自定义属性"Brand":

add_action('admin_menu', 'register_processing_order_summary_page', 99); 
function register_processing_order_summary_page() {
    add_submenu_page( 'woocommerce', 'Processing Order Summary', 'Processing Order Summary', 'read', 'processing_order_summary_page', 'processing_order_summary_page_callback' );
}


function processing_order_summary_page_callback() {  
    // Get all processing orders
    $orders = wc_get_orders([
        'limit'     => -1,
        'type'      => 'shop_order',
        'status'    => 'processing',
    ]);

    $data = array(); // Initialize variable

    // Loop through processing orders
    foreach( $orders as $order ) {
        // Loop through order items
        foreach( $order->get_items() as $item_id => $item ) {
            $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
            $product    = $item->get_product();
            $quantity   = $item->get_quantity();
            $parent     = wc_get_product( $item->get_product_id() );

            if ( isset($data[$product_id]) ) {
                $data[$product_id]['qty'] += $quantity;
            } else {
                $data[$product_id] = array(
                    'name'  => $product->get_name(),
                    'sku'   => $product->get_sku(),
                    'brand' => $parent->get_attribute('Brand'),
                    'qty'   => $quantity,
                );
            }
        }
    }
    ?>
    <div class="wrap">
        <h1><?php _e('Pending Order Summary'); ?></h1>
        <p id="processingordersubtitle"><?php 
        _e('This is a list of all the products needed to complete all "Processing" orders.'); 
        ?><br /><hr /></p>
        <table class="processing-order-items" cellspacing="3">
            <tr>
                <th><?php _e('Quantity'); ?></th>
                <th><?php _e('Product name'); ?></th>
                <th><?php _e('Brand'); ?></th>
                <th><?php _e('SKU'); ?></th>
            </tr>
    <?php if( count($data) > 0 ) :

    //Output the results in a table
    foreach ( $data as $product_id => $values ) : ?>
        <tr>
            <td><?php echo $values['qty']; ?></td>
            <td><?php echo $values['name']; ?></td>
            <td><?php echo $values['brand']; ?></td>
            <td><?php echo $values['sku']; ?></td>
        </tr>
    <?php
    endforeach; 
    endif;
    ?>
    </table><br>
    <a href="javascript:window.print()"><button type="button" class="orderprintbutton"><?php _e('Print'); ?></button></a>
    </div>
    <?php
}

 
// Function to apply print styling to the page
add_action('admin_head', 'dashboard_print_view');
function dashboard_print_view() {
    ?>
    <style>
    .processing-order-items th, 
    .processing-order-items td {text-align:left; min-width:100px; padding:2px 10px;}
    @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>
    <?php 
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

为什么注册SIGHUP的处理程序函数会阻止在PHP CLI中等待输入时点击X关闭Xterm窗口?""

PHP cUrl扩展与v8.2.12更新损坏

在AJAX请求后,Laravel重定向.(不允许使用方法)

从WooCommerce订单状态更改更新用户自定义元数据

如何在laravel中使用script type= text/html/script

使用与OpenSSL命令行兼容的AES-256-CTR为大型文件创建php代码OpenSSL加密

Codeigniter 4中的未定义变量$HOME

上传WordPress模板中的翻译文件(.mo和.po)

在带有livewire 3的laravel中使用规则方法时,无法进行实时验证

显示自定义 Laravel 9 公共属性的错误

Woocommerce API - 图像问题

Laravel 10 中的自定义类未找到

使用自定义规则进行 Livewire 验证不会显示错误

图像未在 Laravel Ajax Crud 中显示

PHP 合并/合并多维数组

Laravel Factory - 在单列中生成具有随机迭代量的假 json 数组数据

如何为多个表的id列设置不同的值

使用 PHP ImageMagick 库将 pdf 文件的前 n 页转换为单个 png 图像文件

根据数组中的文本文件编号显示星级

多排序子数组 - 包含范围数据的子数组