首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何过滤自定义元狒狒中的结果集?

如何过滤自定义元狒狒中的结果集?
EN

WordPress Development用户
提问于 2020-01-27 15:53:28
回答 1查看 95关注 0票数 0

我正在添加一个插件,添加一个自定义元框在管理woocommerce产品详细信息页面。它列出了包含该产品的已完成订单。我成功地用数据添加了那个框。但是,我想添加一个带有两个日期选择器的filter按钮来过滤数据。我一直在查看元框示例,它们将数据保存到记录中。不过,在我的情况下,我不是在保存数据,而是返回存在的数据。我该怎么做才能做到这一点?

更新:下面是我当前的代码:

代码语言:javascript
复制
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') ."
EN

回答 1

WordPress Development用户

发布于 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调用的示例。

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/357281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档