我正在添加一个插件,在管理woocommerce产品详细信息页面上添加一个自定义元框。它列出了包含该产品的已完成订单。我能够成功地将数据添加到该框中。但是,我想添加一个带有两个日期选择器的过滤器按钮来过滤数据。我一直在看meta box示例,它们将数据保存到记录中。但在我的情况下,我并没有保存数据,只是返回存在的数据。我将如何实现这一点?
Update: 以下是我目前拥有的代码:
function wporg_add_custom_box()
{
$screens = [\'product\'];
foreach ($screens as $screen) {
add_meta_box(
\'wporg_box_id\', // Unique ID
\'Orders that purchased this product\', // Box title
\'wporg_custom_box_html\', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action(\'add_meta_boxes\', \'wporg_add_custom_box\');
function wporg_custom_box_html($post)
{
?>
<table>
<tr>
<td>Order ID</td>
<td>Name</td>
<td>Email</td>
<td>Item Name</td>
<td>Date Created</td>
</tr>
<?php
// Access WordPress database
global $wpdb;
// Select Product ID
$product_id = $post->ID;
// Get the orders that bought the product
// Only get those that are paid
$statuses = array_map( \'esc_sql\', wc_get_is_paid_statuses() );
$orders = $wpdb->get_col("
SELECT DISTINCT p.ID FROM {$wpdb->posts} AS p
INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id
WHERE p.post_status IN ( \'wc-" . implode( "\',\'wc-", $statuses ) . "\' )
AND im.meta_key IN ( \'_product_id\')
AND im.meta_value = $product_id
");
$i = 0;
$arrayLength = count($orders);
if ($arrayLength<=0)
{
echo \'<tr><td colspan="5">No one bought this yet.</td></tr>\';
}
else
{
while ($i < $arrayLength)
{
//echo "<tr><td>". $orders[$i] ."</td></tr>";
$order = wc_get_order($orders[$i]);
foreach ($order->get_items() as $item_key => $item ){
//the variable stores it as string whereas the object returns it as integer
if ($item->get_product_id() === (int)$product_id){
echo "<tr><td>".$orders[$i]."</td><td>". $order->get_billing_first_name() . " " . $order->get_billing_last_name() ."</td><td>". $order->get_billing_email() ."</td><td>". $item->get_name() ."</td><td>". $order->get_date_created()->date(\'m-d-Y H:i:s\') ."</td></tr>";
}
}
$i++;
}
}
?>
<table>
<?php
}
?>