【问题标题】:Generate order report by product name by WC_Admin_ReportWC_Admin_Report 按产品名称生成订单报告
【发布时间】:2017-04-18 06:40:55
【问题描述】:

我想生成一份订单报告,其中订购的产品包含特定名称。

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

我想使用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_meta 只查找产品名称在woocommerce_order_items 表中的woocommerce_order_itemmeta 表,那么我该如何通过woocommerce_order_items 进行搜索呢?像这样?

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

【问题讨论】:

    标签: php mysql woocommerce hook-woocommerce


    【解决方案1】:

    您可以为您的$where_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_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);    
    }
    

    别忘了'group_by'

        /* 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
                )
    
        )
    
        */  
    

    【讨论】:

    • 太棒了!这真的有效。非常感谢。对了,你知道这门课怎么做分页吗?
    • 很高兴您可以使用它,但是,我似乎没有看到一种简单的方法,只能使用您自己的方式。以任何可能的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多