Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算业务变化的订单数量_Php_Sql_Wordpress_Woocommerce_Orders - Fatal编程技术网

Php 计算业务变化的订单数量

Php 计算业务变化的订单数量,php,sql,wordpress,woocommerce,orders,Php,Sql,Wordpress,Woocommerce,Orders,我已经找到了一种方法,但是我发现我的查询时间太长了,Woocmerce中的订单越多,我们添加的变体越多,查询时间就越长 我希望在WC或WP中有一种方法可以查询订单的变体ID,但遗憾的是,我还没有找到它。我需要做销售报告的变化 //get number of orders per variation_id function getOrdersfromVariation($variation_id){ $numberOfOrders = 0; ip_write_log("getOrd

我已经找到了一种方法,但是我发现我的查询时间太长了,Woocmerce中的订单越多,我们添加的变体越多,查询时间就越长

我希望在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;
}

您可以在活动子主题(或主题)的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>';
}
$items\u id=get\u all\u orders\u items\u from\u a\u product\u variation(41);
foreach($items\u id作为$item\u id){
$item\u color=wc\u get\u order\u item\u meta($item\u id,'pa\u color',true);
//显示与当前订单项目相关的某些数据
回显“项目ID:”.$Item_ID.。颜色为“.”.$Item_color.“
”; }

希望这对您有用。

您可以使用嵌入以下函数的非常简单的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'订单计数:'。从变更(41)中计算订单

",;
Wow,这使我的页面加载时间从25秒减少到3秒。非常感谢你!
echo '<p>Orders count: ' . count_orders_from_variation(41) . '</p>';