แทนที่ฟังก์ชันการทำงานในส่วนรายงาน Woocommerce

ฉันต้องปรับแต่งรายงานใน WooCommerce ดังนั้นฉันจึงต้องแก้ไขไฟล์หลัก class-wc-report-sales-by-date.php ไฟล์ดังกล่าวไม่ได้ใช้ hook ใด ๆ

ตรวจสอบสิ่งนี้ในซอร์สโค้ด WC: includes/admin/reports/class-wc-report-sales-by-date.php

ฉันต้องแก้ไขบรรทัด 393 ตัวแปรนี้: $this->report_data->total_sales ฉันต้องปรับแต่งจำนวน ยอดขายรวม เพื่อเพิ่มค่าอื่น

ฉันจะแทนที่ WooCommerce ไฟล์หลักนี้ได้อย่างไร


person Andrés Posada Llano    schedule 23.11.2017    source แหล่งที่มา


คำตอบ (1)


อย่าแทนที่ไฟล์หลัก… มีวิธีอื่นที่ทำเพื่อสิ่งนั้น หากคุณดูที่ line 411 คุณจะมี woocommerce_admin_report_data hook ตัวกรองเพื่อทำการเปลี่ยนแปลง ด้วยวิธีนี้ (ตัวอย่าง):

add_filter( 'woocommerce_admin_report_data', 'custom_admin_report_data', 10, 1 );
function custom_admin_report_data( $report_data ){
    // HERE you make your calculations and changes
    // New amout to set (example)
    $new_calculated_amount = 100;

    // Set the new amounts for "total_sales" key
    $report_data->total_sales = $new_calculated_amount;

    // Raw data output just for testing, to get the keys and the structure of the data
    // to be removed
    echo '<pre>'; print_r($report_data); echo '</pre>';

    // Return the changed data object
    return $report_data;
}

โค้ดไปอยู่ในไฟล์ function.php ของธีมลูกที่ใช้งานอยู่ (หรือธีม) หรือในไฟล์ปลั๊กอินใดๆ

ทดสอบแล้วใช้งานได้

ฉันได้รวมบรรทัดโค้ดที่แสดงข้อมูลดิบที่คุณควรลบออก... เพียงเพื่อดูโครงสร้างข้อมูลและการเปลี่ยนแปลงที่ทำโดยฟังก์ชันบนค่า "total_sales"...


ผลลัพธ์ของข้อมูลดิบจะมีลักษณะดังนี้ (ที่ให้โครงสร้างข้อมูลเพื่อทำการเปลี่ยนแปลงได้ดีขึ้น):

stdClass Object
(
    [order_counts] => Array
        (
            [0] => stdClass Object
                (
                    [count] => 1
                    [post_date] => 2017-11-21 16:45:43
                )
        )
    [coupons] => Array
        (
        )
    [order_items] => Array
        (
            [0] => stdClass Object
                (
                    [order_item_count] => 1
                    [post_date] => 2017-11-21 16:45:43
                )
        )
    [refunded_order_items] => 0
    [orders] => Array
        (
            [0] => stdClass Object
                (
                    [total_sales] => 48
                    [total_shipping] => 15
                    [total_tax] => 5
                    [total_shipping_tax] => 3
                    [post_date] => 2017-11-21 16:45:43
                )
        )
    [full_refunds] => Array
        (
        )
    [partial_refunds] => Array
        (
        )
    [refund_lines] => Array
        (
        )
    [total_tax_refunded] => 0
    [total_shipping_refunded] => 0
    [total_shipping_tax_refunded] => 0
    [total_refunds] => 0
    [total_tax] => 5.00
    [total_shipping] => 15.00
    [total_shipping_tax] => 3.00
    [total_sales] => 48.00
    [net_sales] => 25.00
    [average_sales] => 3.57
    [average_total_sales] => 6.86
    [total_coupons] => 0.00
    [total_refunded_orders] => 0
    [total_orders] => 1
    [total_items] => 1
)

อย่างที่คุณเห็น คุณจะต้องทำการเปลี่ยนแปลงข้อมูลออบเจ็กต์คำสั่งซื้อด้วย เนื่องจากคุณมีปุ่ม "total_sales" ด้วย...

person LoicTheAztec    schedule 23.11.2017
comment
ขอบคุณ. นั่นคือสิ่งที่ฉันต้องการ ฉันขอขอบคุณเวลาและความช่วยเหลือของคุณจริงๆ :) - person Andrés Posada Llano; 24.11.2017
comment
สิ่งนี้เป็นไปได้หรือไม่ที่จะจัดการข้อมูลหรือเพิ่มรายการเพิ่มเติมลงในแถบด้านข้าง/คำอธิบายแผนภูมิใน 'ยอดขายตามผลิตภัณฑ์' ตัวกรองนี้มีผลกับ 'ยอดขายตามวันที่' เท่านั้น - person no.; 19.02.2020