【问题标题】:Woocommerce - php to get order informationWoocommerce - php 获取订单信息
【发布时间】:2013-08-12 23:29:30
【问题描述】:

我正在尝试获取与 woocommerce 插件 (wordpress) 上的订单相关联的数据。目前,我已经编写了自己的包含代码的插件:

<?php 
global $woocommerce;
$order = new WC_Order($order_id);
$order_shipping_total = $order->get_shipping();
echo $order_shipping_total;
?>

这只是为了测试它,我不相信这是有效的-但我真正需要的是获取具有特定订单状态的订单列表,然后能够访问这些字段(例如名字) 用于此列表中的每个订单。我该怎么做呢? 另外,我需要包含哪些文件才能完成这项工作? class-wc-order() 文件?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    最近我在以 XML 格式导出订单数据。

    $args = array(
      'post_type' => 'shop_order',
      'post_status' => 'publish',
      'meta_key' => '_customer_user',
      'posts_per_page' => '-1'
    );
    $my_query = new WP_Query($args);
    
    $customer_orders = $my_query->posts;
    
    foreach ($customer_orders as $customer_order) {
     $order = new WC_Order();
    
     $order->populate($customer_order);
     $orderdata = (array) $order;
    
     // $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
    }
    

    【讨论】:

    • 很好,工作正常,但我想获得一个特定的产品字段,比如买家电子邮件和时间,我如何才能显示特定产品 ID 的电子邮件和时间。谢谢
    • 你能看看这里吗?我已经使用了你的代码,但我想根据产品 ID here the question获取一些详细信息
    【解决方案2】:

    要过滤掉特定客户的订单,请使用附加参数 meta_value

    $user_id = get_current_user_id();
    $args = array(
      'post_type' => 'shop_order',
      'post_status' => 'publish',
      'meta_key' => '_customer_user',
      'meta_value' => $user_id,
      'numberposts' => -1, // -1 for all orders
      'posts_per_page' => '-1'
    );
    $my_query = new WP_Query($args);
    

    也是为特定客户加载订单的替代方式:

    $orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
        'numberposts' => 1, // -1 for all orders
        'meta_key'    => '_customer_user',
        'meta_value'  => $user_id,
        'post_type'   => 'shop_order',
        'post_status' => 'publish'
    ) ) );
    

    另见here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 2018-03-16
      • 2018-05-20
      相关资源
      最近更新 更多