【问题标题】:How to get all order details in wordpress/woocommerce? [duplicate]如何在 wordpress/woocommerce 中获取所有订单详细信息? [复制]
【发布时间】:2015-10-18 18:23:48
【问题描述】:

我正在使用 woo-commerce 并尝试从数据库中获取所有订单详细信息。我是新手,无法实现。我已经创建了 order_details.php 模板,我可以在其中获取所有订单数据。

谁能帮帮我?

【问题讨论】:

    标签: php wordpress woocommerce wordpress-theming


    【解决方案1】:

    试试这个代码。

    $args = array(
      'post_type' => 'shop_order',
     'posts_per_page' => '-1'
    );
    $my_query = new WP_Query($args);
    
    $orders = $my_query->posts;
    echo "</pre>";
    print_r($orders);
    echo "<pre>";
    

    按照您的要求自定义。

    【讨论】:

    • 感谢您的步骤。但不幸的是,它返回一个空白数组。让我向您解释一下,我在本地主机中安装了 woocommerce,其中有 2 个订单。我需要从数据库中获取这些订单详细信息.请您告诉我如何获取所有这些数据。 @Dev Danidhariya
    • '全球 $woocommerce;'在我的代码中添加 Top。然后检查。
    • 你好解决你的问题与否。
    • 还没有,兄弟。 :( 尝试修改插件以导出订单详细信息的 CSV。
    • 只需添加 'post_status' => 'all' 就会返回所有订单
    【解决方案2】:

    自从 Woocommerce 大型主要更新 3.0+ 以来,情况发生了很大变化:

    • WC_Order 属性不能像以前那样直接访问,会抛出一些错误。
    • 现在需要新的 WC_OrderWC_Abstract_Order getter 和 setter 方法。
    • 订单项目有一些新类:

      • WC_Order_Item 类。

      • WC_Order_Item_Product 类。

    因此,在 foreach 循环中也无法像以前一样访问 Order items 属性,您将不得不改用 this specific getter and setter methods

    // Get an instance of the WC_Order object (same as before)
    $order = wc_get_order( $order_id );
    
    // Get the order ID
    $order_id = $order->get_id();
    
    // Get the custumer ID
    $order_id = $order->get_user_id();
    

    获取和访问订单数据属性(在值数组中):

    $order = wc_get_order( $order_id );
    
    $order_data = $order->get_data(); // The Order data
    
    $order_id = $order_data['id'];
    $order_parent_id = $order_data['parent_id'];
    $order_status = $order_data['status'];
    $order_currency = $order_data['currency'];
    $order_version = $order_data['version'];
    $order_payment_method = $order_data['payment_method'];
    $order_payment_method_title = $order_data['payment_method_title'];
    $order_payment_method = $order_data['payment_method'];
    $order_payment_method = $order_data['payment_method'];
    
    ## Creation and modified WC_DateTime Object date string ##
    
    // Using a formated date ( with php date() function as method)
    $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s');
    $order_date_modified = $order_data['date_modified']->date('Y-m-d H:i:s');
    
    // Using a timestamp ( with php getTimestamp() function as method)
    $order_timestamp_created = $order_data['date_created']->getTimestamp();
    $order_timestamp_modified = $order_data['date_modified']->getTimestamp();
    
    
    $order_discount_total = $order_data['discount_total'];
    $order_discount_tax = $order_data['discount_tax'];
    $order_shipping_total = $order_data['shipping_total'];
    $order_shipping_tax = $order_data['shipping_tax'];
    $order_total = $order_data['cart_tax'];
    $order_total_tax = $order_data['total_tax'];
    $order_customer_id = $order_data['customer_id']; // ... and so on
    
    ## BILLING INFORMATION:
    
    $order_billing_first_name = $order_data['billing']['first_name'];
    $order_billing_last_name = $order_data['billing']['last_name'];
    $order_billing_company = $order_data['billing']['company'];
    $order_billing_address_1 = $order_data['billing']['address_1'];
    $order_billing_address_2 = $order_data['billing']['address_2'];
    $order_billing_city = $order_data['billing']['city'];
    $order_billing_state = $order_data['billing']['state'];
    $order_billing_postcode = $order_data['billing']['postcode'];
    $order_billing_country = $order_data['billing']['country'];
    $order_billing_email = $order_data['billing']['email'];
    $order_billing_phone = $order_data['billing']['phone'];
    
    ## SHIPPING INFORMATION:
    
    $order_shipping_first_name = $order_data['shipping']['first_name'];
    $order_shipping_last_name = $order_data['shipping']['last_name'];
    $order_shipping_company = $order_data['shipping']['company'];
    $order_shipping_address_1 = $order_data['shipping']['address_1'];
    $order_shipping_address_2 = $order_data['shipping']['address_2'];
    $order_shipping_city = $order_data['shipping']['city'];
    $order_shipping_state = $order_data['shipping']['state'];
    $order_shipping_postcode = $order_data['shipping']['postcode'];
    $order_shipping_country = $order_data['shipping']['country'];
    

    使用 WC_Order_Item_Product 和 WC_Order_Item 方法获取订单商品并访问数据:

    $order = wc_get_order($order_id);
    
    // Iterating through each WC_Order_Item_Product objects
    foreach ($order->get_items() as $item_key => $item_values):
    
        ## Using WC_Order_Item methods ##
    
        // Item ID is directly accessible from the $item_key in the foreach loop or
        $item_id = $item_values->get_id();
    
        ## Using WC_Order_Item_Product methods ##
    
        $item_name = $item_values->get_name(); // Name of the product
        $item_type = $item_values->get_type(); // Type of the order item ("line_item")
    
        $product_id = $item_values->get_product_id(); // the Product id
        $wc_product = $item_values->get_product(); // the WC_Product object
        ## Access Order Items data properties (in an array of values) ##
        $item_data = $item_values->get_data();
    
        $product_name = $item_data['name'];
        $product_id = $item_data['product_id'];
        $variation_id = $item_data['variation_id'];
        $quantity = $item_data['quantity'];
        $tax_class = $item_data['tax_class'];
        $line_subtotal = $item_data['subtotal'];
        $line_subtotal_tax = $item_data['subtotal_tax'];
        $line_total = $item_data['total'];
        $line_total_tax = $item_data['total_tax'];
    
    endforeach;
    

    【讨论】:

      【解决方案3】:

      $my_course_query = new WP_Query(array('post_type'=&gt;'shop_order','post_status'=&gt;'wc-completed'));

      将状态更改为 woocommerce 状态

      【讨论】:

      • 完美zzz!谢谢
      【解决方案4】:

      您可以使用下面的代码

      require(dirname(__FILE__) . '/wp-load.php');
      
      global $wpdb;
      
          $orders = get_posts( array(
              'post_type'   => 'shop_order',
              'posts_per_page' => '-1'
      ) );
      

      你需要包含wp-load.php文件

      require(dirname(__FILE__) . '/wp-load.php');
      

      并在此代码之前添加全局变量global $wpdb;

      【讨论】:

        猜你喜欢
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        • 2014-05-15
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        相关资源
        最近更新 更多