【问题标题】:Display Woocommerce sales greater than $50显示 Woocommerce 销售额超过 50 美元
【发布时间】:2022-09-28 17:19:06
【问题描述】:

使用 Woocommerce 在网站上接受捐赠。想要在前端显示超过 50 美元的捐款(订单)。 似乎无法过滤掉 50 美元以下的订单(它们仍然显示)。

echo \'<div class=\"sales-admin\"><b>SALES:</b><ol>\';
    $orders = wc_get_orders( array(
            \'post_type\'   => \'shop_order\',
            \'post_status\' => \'wc-completed\',
            \'posts_per_page\'=> \'5\',
            \'meta_query\' =>
                array(
                    \'key\'       => \'total\',
                    \'value\'      => \'50\',
                    \'compare\'   => \'>\',
                    \'type\' => \'numeric\'
                ),
    ) );
    foreach ($orders as $order) {
     $order = new WC_Order( $order->ID );
     
     $items = $order->get_items();
     foreach( $items as $item ) {
      // $product_id = $item[\'product_id\'];
      // if ( $post->ID == $product_id ) { echo \'<li>\' . $order->get_billing_first_name() . \' \' . $order->get_billing_last_name() . \'</li>\'; }
        echo \'<li>\' . $order->get_billing_first_name() . \' \' . $order->get_billing_last_name() . \': \' . $order->total . \'</li>\';
     }            
    } 
    echo \'</ol></div>\';

感谢您对此的任何帮助。

    标签: wordpress woocommerce


    【解决方案1】:

    在按订单过滤时,您应该使用 _order_total 元键,total 键不存在

    查看数据库,找到shop_order 类型的订单,使用postID 检查post_meta 并找到密钥order_total


    更新

    在我的本地机器上,它与_order_total 一起工作。您还应该能够只传递 total 字段而不是元查询。

    $args = array(
        'total' => '>=' . ( 50)
    );
    $orders = wc_get_orders( $args );
    

    查看更多关于wc_get_ordershttps://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query

    【讨论】:

    • 这对我不起作用,但让我更好地理解。我做了一个 var dump 并在internal_meta_keys 下找到了order_total。但是order_total 是空白的。当我走到转储的顶部时,我确实看到了一个带有值的 total 键。我该如何查询?我试过:'total' =&gt; array( 'value' =&gt; 50, 'compare' =&gt; '&gt;=', ), 但没有用。
    • 查看更新的答案
    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多