0

I am trying to write my own plugin to extend the reports of our Woocommerce shop on Wordpress. In a nutshell I have a couple of custom fields on my checkout page and I would like the reports in the admin screen to show how many times each *option was used from the custom dropdowns we are using on our checkout. An example of our dropdown would be "How did you hear about us?" with of course select options where they can choose how they heard about us.

In my plugin I successfully tapped into the reports section of woocommerce and have the basic wireframe of what I want to accomplish. The report screen is showing a table, on one column it has the available options the user sees, like "radio advertisement", "heard from a friend", etc. The right side I would simply like a total of how many times that option was used. I am able to connect to the database to get those values but what I cant seem to figure out is how to count how many times those options were used?

Here is what I have so far which shows me all available options, just nothing showing for how much each option was used, which I am not sure how to integrate?

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>

Help, Im almost there :D

4

1 回答 1

1

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

$sql = "SELECT country.meta_value, count(*) as 'sale_total' ...
于 2013-10-23T22:28:54.370 回答