【问题标题】:Get WooCommerce orders Items by variation ID按变体 ID 获取 WooCommerce 订单项目
【发布时间】:2017-07-20 12:14:18
【问题描述】:

如何根据特定产品变体列出订单商品?

给定一个变体 ID,我想生成包含该特定变体的所有订单商品的列表。

在我的例子中,我使用变体来表示日期,并且产品本身是一个经常性事件,所以这样做的目的是列出参加该特定日期的人。

如果这可以通过get_posts 使用meta_keys 或类似的东西来完成,那将是惊人的,但否则我猜自定义查询将是一种方式。

我似乎无法弄清楚在这种情况下表是如何关联的,或者是否以可搜索的方式存储。

非常感谢任何帮助。

谢谢!

【问题讨论】:

    标签: php sql wordpress woocommerce orders


    【解决方案1】:

    有很多方法可以做到这一点。在这里,我在一个自定义函数中使用了一个 SQL 查询和一个 $variation_id (要设置的变体 ID) 作为其中的参数:

    function get_all_orders_items_from_a_product_variation( $variation_id ){
    
        global $wpdb;
    
        // Getting all Order Items with that variation ID
        $item_ids_arr = $wpdb->get_col( $wpdb->prepare( "
            SELECT `order_item_id` 
            FROM {$wpdb->prefix}woocommerce_order_itemmeta 
            WHERE meta_key LIKE '_variation_id' 
            AND meta_value = %s
        ", $variation_id ) );
    
        return $item_ids_arr; // return the array of orders items ids
    
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    USAGE (此处以变体 ID 41 为例):

    这将显示此变体 ID 的订单商品 ID 列表以及一些数据(例如)。

    $items_ids = get_all_orders_items_from_a_product_variation( 41 );
    
    // Iterating through each order item
    foreach( $items_ids as $item_id ){
    
        // Getting some data (the color value here)
        $item_color = wc_get_order_item_meta( $item_id, 'pa_color', true );
    
        // Displaying some data related to the current order item
        echo 'Item ID: '. $item_id . ' with color "' . $item_color .'"<br>';
    }
    

    此代码已经过测试并且可以工作。


    获取订单 ID (已更新)

    现在,如果您需要获取所有订单 ID,您可以通过这种方式在该函数中使用另一个查询:

    function get_all_orders_that_have_a_product_variation( $variation_id ){
    
        global $wpdb;
    
        // Getting all Order IDs with that variation ID
        $order_ids_arr = $wpdb->get_col( $wpdb->prepare( "
            SELECT DISTINCT items.order_id
            FROM {$wpdb->prefix}woocommerce_order_items AS items
            LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON items.order_item_id = itemmeta.order_item_id
            WHERE meta_key LIKE '_variation_id'
            AND meta_value = %s
        ", $variation_id ) );
    
        return $order_ids_arr; // return the array of orders ids
    
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    USAGE (这里总是以变体 ID 41 为例):

    这将显示此变体 ID 的订单 ID 列表及其状态(例如)。

    $orders_ids = get_all_orders_that_have_a_product_variation( 41 );
    
    // Iterating through each order item
    foreach( $orders_ids as $order_id ){
    
        // Getting an instance of the order object
        $order = wc_get_order($order_id);
    
        // Displaying some data related to the current order
        echo 'Order #'. $order_id . ' has status "' . $order->get_status() .'"<br>';
    }
    

    此代码已经过测试并且可以工作。

    您还可以通过这种方式将订单 ID 与其相关的商品 ID 组合成一个更复杂的数组:

    function get_all_orders_and_item_ids_that_have_a_product_variation( $variation_id ){
    
        global $wpdb;
    
        // Getting all Order IDs and item ids with that variation ID
        $results = $wpdb->get_results( $wpdb->prepare( "
            SELECT items.order_id, items.order_item_id AS item_id
            FROM {$wpdb->prefix}woocommerce_order_items AS items
            LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON items.order_item_id = itemmeta.order_item_id
            WHERE meta_key LIKE '_variation_id'
            AND meta_value = %s
        ", $variation_id ), ARRAY_A );
    
        return $results; // return a multi-dimensional array of orders Ids / items Ids
    
    }
    

    【讨论】:

    • 非常感谢您的详尽回答!我最终使用了您的第二个函数和 WC 函数的组合来充实数据,但将调查我是否可以优化但从查询中获取所有内容。
    • 不适合我,可能是版本问题?仅获取空白数组。
    • 我尝试添加获取订单 ID,仅使用了带有自定义前缀的第二个函数,但没有运气。它只是显示空白数组。甚至尝试检查函数然后发现item_ids_arr 显示空白数组。
    • 我应该用我的代码创建一个新问题吗?我有两个变体,即“S”和“L”。我只想要两者的订单 ID,以便我可以计算特定变体的订单数量。
    • @BilalHussain 我终于在第二个函数 (sorry) 中找到了问题。因此代码会更新并在唯一的 SQL 查询中工作(而不是 2)。它会完美运行。
    【解决方案2】:

    我尝试做同样的事情,在查看 db 后,我在 '_variation_id' 前面得到了 0。但是使用我的自定义黑客为正在寻找相同问题的未来人获取订单项目。我们需要使用product_name、variaion_name(例如 Size 等)进行比较。和variation_value LIKE 'S'、'L' 等。考虑这个函数:

    function get_all_orders_that_have_a_product_variation( $product_name, $variation_name, $variation_value ){
    
    global $wpdb;
    // Getting all Order IDs with that variation ID
    
    $order_ids_arr = $wpdb->get_col($wpdb->prepare ( "
            SELECT * FROM wpcc_woocommerce_order_items items 
            LEFT JOIN wpcc_woocommerce_order_itemmeta itemmeta ON items.order_item_id = itemmeta.order_item_id 
            WHERE items.order_item_name LIKE %s 
            AND itemmeta.meta_key LIKE %s 
            AND itemmeta.meta_value = %s", $product_name, $variation_name, $variation_value ));
    
    
     return $order_ids_arr; // return the array of orders ids
    
    }
    

    现在调用它,假设我们的产品名称是Raleigh sport,变体名称是size,值是s。简单来说,我们想获取名称为Raleigh sport 的产品的订单商品id,并且我们只想获取size 为S 的产品

    简单的函数调用:

    print_r(get_all_orders_that_have_a_product_variation( "Raleigh Sport", "Size", "S" ));

    谢谢。

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2019-09-08
      • 1970-01-01
      • 2018-03-16
      • 2018-03-17
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多