我正在添加一个插件,添加一个自定义元框在管理woocommerce产品详细信息页面。它列出了包含该产品的已完成订单。我成功地用数据添加了那个框。但是,我想添加一个带有两个日期选择器的filter按钮来过滤数据。我一直在查看元框示例,它们将数据保存到记录中。不过,在我的情况下,我不是在保存数据,而是返回存在的数据。我该怎么做才能做到这一点?
更新:下面是我当前的代码:
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)
{
?>
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 '';
}
else
{
while ($i < $arrayLength)
{
//echo "";
$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 "";
}
}
$i++;
}
}
?>
Order ID
Name
Email
Item Name
Date Created
No one bought this yet.". $orders[$i] ."".$orders[$i]."". $order->get_billing_first_name() . " " . $order->get_billing_last_name() ."". $order->get_billing_email() ."". $item->get_name() ."". $order->get_date_created()->date('m-d-Y H:i:s') ."发布于 2020-01-29 15:57:13
要向代码中添加日期选择器,您应该加载包含在WordPress中的set JS日期选择器,以便加载到产品管理页面中,并编写一些自定义JS代码,以便根据选定的日期过滤产品。
您可以加载所有产品,然后使用代码过滤它们,或者只在特定日期之间通过Ajax加载产品。如果您有很多产品,那么Ajax方法将更好地防止长时间的加载。
在这里,您可以找到如何加载jQuery UI日期选择器:https://stackoverflow.com/a/27463687/2011434
在这里,您可以找到在产品页面我如何在某些/wp-管理页面上嵌入样式/脚本?中对脚本进行排队的几种方法
这里可以看到如何对admin https://wordpress.stackexchange.com/a/112717/62909执行Ajax调用的示例。
https://wordpress.stackexchange.com/questions/357281
复制相似问题