mohamed adel
mohamed adel

Reputation: 715

Detect if WooCommerce Reports Tab is Active

is there is a function to know if i am in woocommerce reports page?

i want the function to be executed when the user click this tab only

enter image description here

to be specific in this tab

http://localhost/zumra/wordpress/wp-admin/admin.php?page=wc-reports&tab=reports

how to add this code to be executed when i am in the page `add_filter(

'woocommerce_admin_reports', 'add_report_tab' );
function add_report_tab( $reports ) {
    $orders = wc_get_orders( array('limit' => -1, 'type' => 'shop_order') );
    // Get Orders With Discount on Them
    foreach( $orders as $order ){

        if (sizeof($order->get_used_coupons()) > 0) {
        $order_data = $order->get_data(); // The Order data
        $discounted_orders .= 
        '<tr><td>' . $order->get_order_number() . '</td>' .
        '<td>' . $order->get_status() . '</td>' .
        '<td>' . $order->get_date_created()->date('Y-m-d H:i:s') . '</td>' .
        '<td>' . $order->get_total() . '</td>' .
        '<td>' . $order->get_billing_first_name() . '</td>' .
        '<td>' . $order->get_billing_email() . '</td>' .
        '<td>' . $order->get_billing_phone() . '</td>' .
        '<td>' . $order->get_total_discount() . '</td>' .
        '<td>' . $order_payment_method = $order_data['payment_method_title'] . '</td></tr>';
        }
    }

$reports['reports'] = array(
                'title'  => __( 'Zumra', 'woocommerce' ),
                'reports' => array(
                    "sales_by_code" => array(
                        'title'       => __( 'Discounted Orders', 'woocommerce' ),
                        'description' =>  '<table id="hm_sbp_table" style="width:100%; text-align: center;">
                        <tr>
                          <th>#</th>
                          <th>Status</th> 
                          <th>Creation Date</th>
                          <th>Total</th>
                          <th>Customer Username</th> 
                          <th>Customer E-Mail</th>
                          <th>Customer Phone</th> 
                          <th>Total Discount</th>
                          <th>Payment Method</th>
                        </tr>' . $discounted_orders . '<button type="submit" id="button-a" class="button-primary" name="hm_sbp_download" value="1"  return true;">Download Report</button>' , 
                        'hide_title'  => false,
                        'callback'    => '',
                    )

return $reports;
}`

Upvotes: 0

Views: 181

Answers (1)

MrEbabi
MrEbabi

Reputation: 740

Simply, you may try this (tested: enter image description here):

//define the wc_admin_reports_path callback 
function filter_wc_admin_reports_path( $reports_class_wc_report_name_php, $name, $class ) { 
    //run your code here
    return $reports_class_wc_report_name_php; 
}; 

//add the filter 
add_filter( 'wc_admin_reports_path', 'filter_wc_admin_reports_path', 10, 3 ); 

Better filter:

// define the woocommerce_reports_charts callback 
function filter_woocommerce_reports_charts( $reports ) { 
    echo "test<br>";
    echo "test  1<br>";
    echo "test  2<br>";
    echo "test  3<br>";

    return $reports;
};

// add the filter 
add_filter( 'woocommerce_reports_charts', 'filter_woocommerce_reports_charts', 10, 1 ); 

I hope this helps. Have a good day.

Upvotes: 1

Related Questions