向Woocommerce管理员报告中的“自定义”列添加内容

时间:2018-08-01 09:57:27

标签: woocommerce

我想在Woocommerce报表中添加一个新列,按代码征税,其中包含每个税率的“净利润”,就像下面的图片一样:

new column taxes by code

我已成功在表格中添加列,但是如何获得每个税率的“净利润”?
有什么想法吗?

这是\ wp-content \ plugins \ woocommerce \ includes \ admin \ reports中修改后的class-wc-report-taxes-by-code.php的代码:

function class_wc_report_taxes_by_code() {

                if ( ! class_exists( 'New_WC_Report_Taxes_By_Code' ) ) {
                    include_once( WC_ABSPATH . 'includes/admin/reports/class-wc-report-taxes-by-code.php' );
                }
                class New_WC_Report_Taxes_By_Code extends WC_Report_Taxes_By_Code {

                    /**
                     * Get the main chart.
                     */
                    public function get_main_chart() {
                            global $wpdb;

                            $query_data = array(
                                    'order_item_name'     => array(
                                            'type'     => 'order_item',
                                            'function' => '',
                                            'name'     => 'tax_rate',
                                    ),
                                    'tax_amount'          => array(
                                            'type'            => 'order_item_meta',
                                            'order_item_type' => 'tax',
                                            'function'        => '',
                                            'name'            => 'tax_amount',
                                    ),
                                    'shipping_tax_amount' => array(
                                            'type'            => 'order_item_meta',
                                            'order_item_type' => 'tax',
                                            'function'        => '',
                                            'name'            => 'shipping_tax_amount',
                                    ),
                                    'rate_id'             => array(
                                            'type'            => 'order_item_meta',
                                            'order_item_type' => 'tax',
                                            'function'        => '',
                                            'name'            => 'rate_id',
                                    ),
                                    'ID'                  => array(
                                            'type'     => 'post_data',
                                            'function' => '',
                                            'name'     => 'post_id',
                                    ),
                            );

                            $query_where = array(
                                    array(
                                            'key'      => 'order_item_type',
                                            'value'    => 'tax',
                                            'operator' => '=',
                                    ),
                                    array(
                                            'key'      => 'order_item_name',
                                            'value'    => '',
                                            'operator' => '!=',
                                    ),
                            );

                            $tax_rows_orders = $this->get_order_report_data(
                                    array(
                                            'data'                => $query_data,
                                            'where'               => $query_where,
                                            'order_by'            => 'posts.post_date ASC',
                                            'query_type'          => 'get_results',
                                            'filter_range'        => true,
                                            'order_types'         => array_merge( wc_get_order_types( 'sales-reports' ), array( 'shop_order_refund' ) ),
                                            'order_status'        => array( 'completed', 'processing', 'on-hold' ),
                                            'parent_order_status' => array( 'completed', 'processing', 'on-hold' ), // Partial refunds inside refunded orders should be ignored
                                    )
                            );

                            // Merge
                            $tax_rows = array();

                            foreach ( $tax_rows_orders as $tax_row ) {
                                    $key              = $tax_row->rate_id;
                                    $tax_rows[ $key ] = isset( $tax_rows[ $key ] ) ? $tax_rows[ $key ] : (object) array(
                                            'tax_amount'          => 0,
                                            'shipping_tax_amount' => 0,
                                            'total_orders'        => 0,
                                    );

                                    if ( 'shop_order_refund' !== get_post_type( $tax_row->post_id ) ) {
                                            $tax_rows[ $key ]->total_orders += 1;
                                    }

                                    $tax_rows[ $key ]->tax_rate             = $tax_row->tax_rate;
                                    $tax_rows[ $key ]->tax_amount          += wc_round_tax_total( $tax_row->tax_amount );
                                    $tax_rows[ $key ]->shipping_tax_amount += wc_round_tax_total( $tax_row->shipping_tax_amount );
                            }
                            ?>


                            <table class="widefat">
                                    <thead>
                                            <tr>
                                                    <th><?php _e( 'Tax', 'woocommerce' ); ?></th>
                                                    <th><?php _e( 'Rate', 'woocommerce' ); ?></th>
                                                    <th class="total_row"><?php _e( 'Number of orders', 'woocommerce' ); ?></th>
                                                    <th class="total_row"><?php _e( 'Tax amount', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the sum of the "Tax rows" tax amount within your orders.', 'woocommerce' ) ); ?></th>
                                                    <th class="total_row"><?php _e( 'Shipping tax amount', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the sum of the "Tax rows" shipping tax amount within your orders.', 'woocommerce' ) ); ?></th>
                                                    <th class="total_row"><?php _e( 'Total tax', 'woocommerce' ); ?> <?php echo wc_help_tip( __( 'This is the total tax for the rate (shipping tax + product tax).', 'woocommerce' ) ); ?></th>
                                                    <th class="total_row"><?php _e( 'Net profit', 'woocommerce' ); ?> <?php echo wc_help_tip( __( "Total sales minus shipping and tax.", 'woocommerce' ) ); ?></th>
                                            </tr>
                                    </thead>
                                    <?php if ( ! empty( $tax_rows ) ) : ?>
                                            <tbody>
                                                    <?php
                                                    $t = array();
                                                    foreach ( $tax_rows as $rate_id => $tax_row ) {
                                                            $rate = $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d;", $rate_id ) );
                                                            $total_tax = $tax_row->tax_amount + $tax_row->shipping_tax_amount;
                                                            $percent = apply_filters( 'woocommerce_reports_taxes_rate', $rate, $rate_id, $tax_row );
                                                            if($percent > 0) {
                                                            $total = 100 * ($total_tax / $percent);
                                                            $t[] = 100 * ($total_tax / $percent);
                                                            }
                                                            ?>
                                                            <tr>
                                                                    <th scope="row"><?php echo apply_filters( 'woocommerce_reports_taxes_tax_rate', $tax_row->tax_rate, $rate_id, $tax_row ); ?></th>
                                                                    <td><?php echo apply_filters( 'woocommerce_reports_taxes_rate', $rate, $rate_id, $tax_row ); ?>%</td>
                                                                    <td class="total_row"><?php echo $tax_row->total_orders; ?></td>
                                                                    <td class="total_row"><?php echo wc_price( $tax_row->tax_amount ); ?></td>
                                                                    <td class="total_row"><?php echo wc_price( $tax_row->shipping_tax_amount ); ?></td>
                                                                    <td class="total_row"><?php echo wc_price( $tax_row->tax_amount + $tax_row->shipping_tax_amount ); ?></td>
                                                                    <td class="total_row"></td>
                                                            </tr>
                                                            <?php
                                                    }
                                                    ?>
                                            </tbody>
                                            <tfoot>
                                                    <tr>
                                                             <?php
                                                                     $total_tax = wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) + array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) );
                                                                     $total_order = wc_round_tax_total( array_sum( $t ) );
                                                             ?>
                                                            <th scope="row" colspan="3"><?php _e( 'Total', 'woocommerce' ); ?></th>
                                                            <th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) ) ); ?></th>
                                                            <th class="total_row"><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); ?></th>
                                                            <th class="total_row"><strong><?php echo wc_price( wc_round_tax_total( array_sum( wp_list_pluck( (array) $tax_rows, 'tax_amount' ) ) + array_sum( wp_list_pluck( (array) $tax_rows, 'shipping_tax_amount' ) ) ) ); ?></strong></th>
                                                            <th class="total_row"><strong></strong></th>
                                                    </tr>
                                            </tfoot>
                                    <?php else : ?>
                                            <tbody>
                                                    <tr>
                                                            <td><?php _e( 'No taxes found in this period', 'woocommerce' ); ?></td>
                                                    </tr>
                                            </tbody>
                                    <?php endif; ?>
                            </table>
                            <?php
                    }
                }
        }

0 个答案:

没有答案