Php 按产品名称按WC\U管理报告生成订单报告

Php 按产品名称按WC\U管理报告生成订单报告,php,mysql,woocommerce,hook-woocommerce,Php,Mysql,Woocommerce,Hook Woocommerce,我想生成一个订单报告,该订单产品包含特定名称 例如,我想要标题为“玩具”的产品的销售报告,该产品包括“汽车玩具”、“工具玩具”、“自行车玩具”等 我想使用Woocommerce WC_Admin_Report类来生成报告,但无法使其正常工作 include_once($woocommerce->plugin_path().'/includes/admin/reports/class-wc-admin-report.php'); $wc_report = new WC_Admin_Repor

我想生成一个订单报告,该订单产品包含特定名称

例如,我想要标题为“玩具”的产品的销售报告,该产品包括“汽车玩具”、“工具玩具”、“自行车玩具”等

我想使用
Woocommerce WC_Admin_Report
类来生成报告,但无法使其正常工作

include_once($woocommerce->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
$wc_report = new WC_Admin_Report();

$where_meta[] = array(
            'type' => 'order_item_meta',
            'meta_key' => '_product_name',
            'operator' => 'LIKE',
            'meta_value' => 'shirt'
        );

$sold_products = $wc_report->get_order_report_data(array(
        'data' => array(
            '_product_id' => array(
                'type' => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => '',
                'name' => 'product_id'
            ),
            '_qty' => array(
                'type' => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => 'SUM',
                'name' => 'quantity'
            ),
            '_line_subtotal' => array(
                'type' => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => 'SUM',
                'name' => 'gross'
            ),
            '_line_total' => array(
                'type' => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => 'SUM',
                'name' => 'gross_after_discount'
            )
        ),
        'query_type' => 'get_results',

        'where_meta' => $where_meta,

        'limit' => 20,

        'order_status' => array( 'completed', 'processing')
    ));
where\u-meta
仅查找产品名称位于
woocommerce\u-order\u-items
表中的
woocommerce\u-order\u-items
表,因此如何在
woocommerce\u-order\u-items
中进行搜索?像这样的

 $where_meta[] = array(
                'type' => 'order_item',
                'meta_key' => 'order_item_name',
                'operator' => 'LIKE',
                'meta_value' => 'toy'
            );

您可以对
$where\u meta
这样做

$products = new WP_Query( array( 
    's'             => 'shirt',
    'post_type'     => 'product',
    'fields'        => 'ids'
) );

$product_ids = $products->posts;

$where_meta = array (
    'relation' => 'OR',
    array(
        'type'       => 'order_item_meta',
        'meta_key'   => array( '_product_id', '_variation_id' ),
        'meta_value' => $product_ids,
        'operator'   => 'IN',
    ),
);
另外,你也可以这样做。更方便。
使用
where
而不是
where\u meta

$where => array(
    array(
        'key'      => 'order_items.order_item_name',
        'value'    => '%shirt%',
        'operator' => 'LIKE',
    ),
),
您还需要在
init
上调用此函数。那么它应该是这样的:

add_action('init', 'reports');

function reports() {


    include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
    $wc_report = new WC_Admin_Report();

    $sold_products = $wc_report->get_order_report_data( array(
        'data' => array(
            '_product_id' => array(
                'type' => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => '',
                'name' => 'product_id'
            ),
            '_qty' => array(
                'type' => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => 'SUM',
                'name' => 'quantity'
            ),
            '_line_subtotal' => array(
                'type' => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => 'SUM',
                'name' => 'gross'
            ),
            '_line_total' => array(
                'type' => 'order_item_meta',
                'order_item_type' => 'line_item',
                'function' => 'SUM',
                'name' => 'gross_after_discount'
            )
        ),
        'where' => array(
            array(
                'key'      => 'order_items.order_item_name',
                'value'    => '%shirt%',
                'operator' => 'LIKE',
            ),
        ),

        'group_by'     => 'order_item_name',

        'query_type' => 'get_results',

        'limit' => 20,

        'order_status' => array( 'completed', 'processing' ),
    ) );

    print_r($sold_products);    
}
不要忘记“分组依据”

    /* sample output of print_r($sold_products);
    Array
    (
        [0] => stdClass Object
            (
                [product_id] => 70
                [quantity] => 3
                [gross] => 36
                [gross_after_discount] => 36
            )

        [1] => stdClass Object
            (
                [product_id] => 53
                [quantity] => 4
                [gross] => 140
                [gross_after_discount] => 140
            )

        [2] => stdClass Object
            (
                [product_id] => 50
                [quantity] => 1
                [gross] => 10
                [gross_after_discount] => 10
            )

    )

    */  

令人惊叹的!这确实有效。谢谢。顺便问一下,你也知道如何在这门课上进行分页吗?很高兴你能利用它,但是,除了用你自己的方式,我似乎看不到一种简单的方法。以任何可能的方式。