【问题标题】:How to count the number of times a meta_value is used in a database如何计算元值在数据库中的使用次数
【发布时间】:2013-10-23 21:36:11
【问题描述】:

我正在尝试编写自己的插件来扩展我们在 Wordpress 上的 Woocommerce 商店的报告。简而言之,我的结帐页面上有几个自定义字段,我希望管理屏幕中的报告显示我们在结帐时使用的自定义下拉列表中使用了每个 * 选项的次数。我们下拉列表的一个例子是“你是怎么知道我们的?”当然还有选择选项,他们可以在其中选择他们是如何了解我们的。

在我的插件中,我成功进入了 woocommerce 的报告部分,并拥有了我想要完成的基本线框。报告屏幕显示了一个表格,在一列上有用户看到的可用选项,如“广播广告”、“从朋友那里听到”等。右侧我只想知道该选项的总次数被使用了。我能够连接到数据库以获取这些值,但我似乎无法弄清楚如何计算这些选项被使用了多少次?

这是我目前所拥有的,它向我展示了所有可用的选项,但没有显示每个选项的使用量,我不确定如何集成?

function custom_reporting() {

        global $start_date, $end_date, $woocommerce, $wpdb;

        $start_date = isset( $_POST['start_date'] ) ? $_POST['start_date'] : '';
        $end_date    = isset( $_POST['end_date'] ) ? $_POST['end_date'] : '';

        if ( ! $start_date )
            $start_date = date( 'Ymd', strtotime( date('Ym', current_time( 'timestamp' ) ) . '01' ) );
        if ( ! $end_date )
            $end_date = date( 'Ymd', current_time( 'timestamp' ) );

        $start_date = strtotime( $start_date );
        $end_date = strtotime( $end_date );

        $sql = "SELECT *
                FROM {$wpdb->prefix}woocommerce_order_items AS order_items

                LEFT JOIN {$wpdb->postmeta} AS country
                    ON order_items.order_id = country.post_id

                LEFT JOIN {$wpdb->posts} AS posts
                    ON order_items.order_id = posts.ID

                LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
                LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
                LEFT JOIN {$wpdb->terms} AS term USING( term_id )

                WHERE posts.post_type             = 'shop_order' 
                AND   posts.post_status           = 'publish'


                AND   country.meta_key            = 'reason_for_purchase'
                AND   term.slug IN ('completed','processing','on-hold')
                AND   post_date > '" . date('Y-m-d', $start_date ) . "'
                AND   post_date < '" . date('Y-m-d', strtotime('+1 day', $end_date ) ) . "'
                GROUP BY country.meta_value";

        $result       = $wpdb->get_results($sql, ARRAY_A);


    ?>
        <form method="post" action="">
            <p><label for="from"><?php _e('From:'); ?></label><input type="text" name="start_date" id="from" readonly="readonly" value="<?php echo esc_attr( date('Y-m-d', $start_date) ); ?>" /><label for="to"><?php _e('To:'); ?></label> <input type="text" name="end_date" id="to" readonly="readonly" value="<?php echo esc_attr( date('Y-m-d', $end_date) ); ?>" /><input type="submit" class="button" value="<?php _e('Show'); ?>" /></p>
        </form>

        <script type="text/javascript">
            jQuery(function(){
                <?php woocommerce_datepicker_js(); ?>
            });
        </script>

        <table class="widefat">
        <thead>
            <tr>
                <th>Reason for Purchase</th>
                <th>Total Entries</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Reason for Purchase</th>
                <th>Total Entries</th>
            </tr>
        </tfoot>
        <tbody>
    <?php

        foreach($result as $value) {
             ?>
            <tr>
                <td><?php echo $value['meta_value']; ?></td>
                <td><?php echo $value['sale_total']; ?></td>
            </tr>
    <?php

        }
    ?>
        </tbody>
        </table>

帮助,我快到了:D

【问题讨论】:

    标签: php mysql wordpress woocommerce


    【解决方案1】:

    您需要一个列来保存您的计数结果。这应该可以...

    $sql = "SELECT country.meta_value, count(*) as 'sale_total' ...
    

    【讨论】:

    • 嗯,条目总数仍然显示为空白。我几乎使用了上面粘贴的内容,在 SELECT * 之后添加了逗号并将 count(*) 添加为 c 部分,但仍然不显示条目总数?是我搞砸了还是理解有误?
    • 对不起,我不知道 woocommerce,我只是想从 mysql 的角度提供帮助。我已经编辑了答案,将“c”替换为“sale_total”。
    • 我认为您不需要选择“*”。为什么要求比你打算使用更多的列?更新了我的答案以反映这一点。
    • Thx.. 你知道只有在订单状态已完成、处理中或暂停时我才能得到结果吗?目前,即使订单被取消或放弃,这也会显示结果。为了使事情井井有条,我提出了一个新问题,但可以感谢了解更多关于连接和从数据库中检索的人stackoverflow.com/questions/19706529/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2011-02-21
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多