首页 文章

WooCommerce列出畅销产品和类别

提问于
浏览
1

我正在尝试编写PHP代码,以降序排列表格形式列出畅销商品(数量)和畅销类别(总销售额) .

项目|数量

类别|销售

我知道我们可以从WooCommerce的Reports屏幕中检索这个,但是我需要获取这些值,以便我可以在小部件的WordPress管理面板中显示它们 . 顺便说一句,我知道如何创建仪表板小部件 .

有谁知道如何实现这个?任何帮助表示赞赏 .

谢谢 .

1 回答

  • 1

    您应该查看woocommerce文件并查找 WC_Admin_Report . 您可以看到一些示例并按照您的需要运行 .

    我会给你这个我做的例子 .

    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();
    
        $data = $wc_report->get_order_report_data( array(
            'data' => array(
                '_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'
                ),
                '_product_id' => array(
                    'type' => 'order_item_meta',
                    'order_item_type' => 'line_item',
                    'function' => '',
                    'name' => 'product_id'
                ),
                'order_item_name' => array(
                    'type'     => 'order_item',
                    'function' => '',
                    'name'     => 'order_item_name',
                ),
            ),
    
            'group_by'     => 'product_id',
    
            'order_by'     => 'quantity DESC',
    
            'query_type' => 'get_results',
    
            'limit' => 20,
    
            'order_status' => array( 'completed', 'processing' ),
        ) );
    
        print_r($data);
    
    }
    

    样本输出 print_r($data);

    Array
    (
        [0] => stdClass Object
            (
                [quantity] => 4
                [gross] => 140
                [product_id] => 53
                [order_item_name] => Happy Ninja
            )
    
        [1] => stdClass Object
            (
                [quantity] => 3
                [gross] => 36
                [product_id] => 70
                [order_item_name] => Flying Ninja
            )
    
        [2] => stdClass Object
            (
                [quantity] => 1
                [gross] => 10
                [product_id] => 50
                [order_item_name] => Patient Ninja
            )
    
    )
    

    你有数据,这将使你启动并运行 Item | Quantity .

相关问题