【问题标题】:WooCommerce List Top Selling Products & CategoriesWooCommerce 列出最畅销的产品和类别
【发布时间】:2017-04-25 10:22:11
【问题描述】:

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

项目 |数量

类别 |销售

我知道我们可以从 WooCommerce 的报告屏幕中检索此值,但我需要获取此值,以便我可以在 WordPress 管理面板的小部件中的其他位置显示它们。顺便说一句,我知道如何创建仪表板小部件。

有人知道如何实现吗?任何帮助表示赞赏。

谢谢。

【问题讨论】:

标签: wordpress woocommerce


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多