WooCommerce:在所有订单中抢占产品的总收入

时间:2013-04-12 作者:Jarryd

我需要显示我的产品的所有订单的总收入,这取决于登录用户是否是产品的作者。

我不能使用Quantity字段作为乘数,因为Woocommerce的Gravity Forms附加组件将其处理为meta\\u键/值字段,这同样适用于订单的product ID引用。我用的是$wpdb 查询来获取我的订单数据,但我似乎无法正确地编写它,也不知道如何获取它,我的SQL技能并不是一流的。

到目前为止,我已经想到了这个:

SELECT * FROM wp_woocommerce_order_itemmeta AS order_meta
LEFT JOIN wp_woocommerce_order_items AS order_items
    ON order_meta.order_item_id = order_items.order_item_id
LEFT JOIN wp_term_relationships AS term_rels
    ON term_rels.object_id = order_items.order_id
WHERE term_taxonomy_id = 24 # 24 being the completed order status ID
这给了我以下信息:

MySQL Workbench output for my query

正如您所看到的,产品ID和数量:16/30等的元值在同一列中,如果可能的话,我几乎需要它们在同一行中。

在查询正确后,我可以将Qty(使用regex获得)乘以一个自定义字段值,并在网站上显示该值。

有人能给我指出正确的方向吗?:)

2 个回复
最合适的回答,由SO网友:Jarryd 整理而成

解决了。可能不是最有效的方法,但它确实有效!

SELECT product_id, meta_value FROM
    (
        SELECT order_meta.order_item_id, order_meta.meta_value AS \'product_id\'
        FROM wp_woocommerce_order_itemmeta AS order_meta
        LEFT JOIN wp_woocommerce_order_items AS order_items
            ON order_meta.order_item_id = order_items.order_item_id
        LEFT JOIN wp_term_relationships AS term_rels
            ON term_rels.object_id = order_items.order_id
        WHERE term_taxonomy_id = 24
        AND order_meta.meta_key = \'_product_id\'
    ) AS product_ID
    JOIN
    (
        SELECT order_meta2.meta_value, order_meta2.order_item_id
        FROM wp_woocommerce_order_itemmeta AS order_meta2
        LEFT JOIN wp_woocommerce_order_items AS order_items
            ON order_meta2.order_item_id = order_items.order_item_id
        LEFT JOIN wp_term_relationships AS term_rels
                ON term_rels.object_id = order_items.order_id
        WHERE term_taxonomy_id = 24
        AND (
            order_meta2.meta_key = \'Item Details\'
            OR order_meta2.meta_key = \'Product Name\'
        )
    ) AS product_qty
    ON product_qty.order_item_id = product_ID.order_item_id
    WHERE product_id = " . $post->ID

SO网友:salim

列出产品的所有客户

//now selects all data in the table wp_postmeta where the word billing is within the value 
$result = mysql_query("SELECT distinct post_id FROM wp_postmeta WHERE meta_key LIKE \'%billing%\' ");

while($row = mysql_fetch_array($result)) 
{ 
//prints the results in a basic table for each row that was retrieved 


//SELECT distinct b.order_item_name, a._billing_email, a., a., a. , a. FROM wp_postmeta a , wp_woocommerce_order_items b WHERE a.post_id=b.order_id and a.meta_key like \'%billing%\' 

//now selects all data in the table wp_postmeta where the word billing is within the value 


$result3 = mysql_query("SELECT * FROM  `wp_postmeta` WHERE post_id = ".$row[\'post_id\']  );
while($row3 = mysql_fetch_array($result3)) 
{ 
    if($row3[\'meta_key\']==\'_billing_email\')
        $email = $row3[\'meta_value\'];
    if($row3[\'meta_key\']==\'_billing_last_name\')
        $name = $row3[\'meta_value\'];
    if($row3[\'meta_key\']==\'_billing_first_name\')
        $firstname = $row3[\'meta_value\'];
    if($row3[\'meta_key\']==\'_billing_city\')
        $city = $row3[\'meta_value\'];
    if($row3[\'meta_key\']==\'_billing_postcode\')
        $cp = $row3[\'meta_value\'];
    if($row3[\'meta_key\']==\'_customer_user\')
        $user = $row3[\'meta_value\'];    
    if($user){
    $result4 = mysql_query("SELECT * FROM  wp_users WHERE ID = ".$user  );
    while($row4 = mysql_fetch_array($result4)) 
    {   
        $user_nicename = $row4[\'user_nicename\'];
        $user_email = $row4[\'user_email\'];

    }   
    }

}

$result2 = mysql_query("SELECT * FROM  `wp_woocommerce_order_items` WHERE order_id = ".$row[\'post_id\']  );
while($row2 = mysql_fetch_array($result2)) 
{ 
    $cours = $row2[\'order_item_name\'];
    echo $row[\'post_id\'].";".$user_nicename.";".$user_email.";".$cours .";".$email.";".$name.";".$firstname.";".$city.";".$cp."<br/>";
}
}

结束