【问题标题】:Count number of orders for a variation in Woocommerce计算 Woocommerce 变体的订单数量
【发布时间】:2018-05-11 05:18:26
【问题描述】:

我想出了一种方法来做到这一点,但我发现我的查询花费的时间太长了,而且 Woocmmerce 中的订单越多,我们添加的变体越多,查询花费的时间就越长...

我希望在 WC 或 WP 中有一种方法可以仅查询订单的变体 ID,但可惜我还没有找到。我需要按变化进行销售报告。

//get number of orders per variation_id
function getOrdersfromVariation($variation_id){
    $numberOfOrders = 0;
    ip_write_log("getOrdersfromVariation varid: $variation_id");

    // rewrite with wc_get_orders
    $args = array(
      'status' => array(  'processing', 'completed'),
        'limit' => -1,
    );
    $orders = wc_get_orders( $args );
    if(isset($orders)){
    //TODO: Get order count - $total_orders = $orders->total;

       foreach ($orders as $order){
           foreach ($order->get_items() as $key => $lineItem) {
               $item_data = $lineItem->get_data();

               if ($item_data['variation_id'] == $variation_id) {
                  $numberOfOrders++;

               }
           }
        }
        if(isset($numberOfOrders)){
           return $numberOfOrders;
        }
     }
  return;
}

【问题讨论】:

    标签: php sql wordpress woocommerce orders


    【解决方案1】:

    您可以使用以下函数中嵌入的这个非常简单的 SQL 查询,从变体 ID 中获取特定订单状态的订单计数:

    function count_orders_from_variation($variation_id){
        global $wpdb;
    
        // DEFINE below your orders statuses
        $statuses = array('wc-completed', 'wc-processing');
    
        $statuses = implode("','", $statuses);
    
        return $wpdb->get_var("
            SELECT count(p.ID) FROM {$wpdb->prefix}woocommerce_order_items AS woi
            JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
            JOIN {$wpdb->prefix}posts AS p ON woi.order_id = p.ID
            WHERE p.post_type = 'shop_order' AND p.post_status IN ('$statuses')
            AND woim.meta_key LIKE '_variation_id' AND woim.meta_value = $variation_id
        ");
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。

    用法示例(显示变体 ID 41 的订单计数):

    echo '<p>Orders count: ' . count_orders_from_variation(41) . '</p>';
    

    【讨论】:

    • 哇,这让我的页面加载时间从 25 秒缩短到了 3 秒。非常感谢!
    【解决方案2】:

    您可以在活动子主题(或主题)的 function.php 文件中或任何插件文件中使用以下代码。

    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    
    }
    

    以下代码将显示此变体 ID 的订单商品 ID 列表以及一些数据。

    $items_ids = get_all_orders_items_from_a_product_variation( 41 );
    
    
    foreach( $items_ids as $item_id ){
    
    
        $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>';
    }
    

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      相关资源
      最近更新 更多