Woocommerce - php to get order information

后端 未结 2 1277
后悔当初
后悔当初 2021-02-08 04:28

I\'m trying to get the data associated with an order on the woocommerce plugin (wordpress). Currently, I have written my own plugin that contains the code:



        
2条回答
  •  半阙折子戏
    2021-02-08 05:07

    To filter out orders for a particular customer use additional argument 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);
    

    Also alternative way for loading orders for a particular customer:

    $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'
    ) ) );
    

    See also here.

提交回复
热议问题