請幫助完成此操作。我需要CSV格式下載woocommerce的訂單報告,對於我提出以下查詢:Sql查詢下載woocommerce的訂單報告
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
fputcsv($output, array('ID', 'Date', 'Status','Name'));
$rows = mysql_query('SELECT ID,post_date,post_status,post_name FROM wp_posts WHERE post_date LIKE "%2016-03-30%"');
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
而這僅僅是一個線索,在這裏我只取後,從表中的數據。
但我需要連接到postmeta和其他表,以便我可以獲得有關訂單的所有信息。
通過在互聯網上搜索我得到下面的代碼,但我不知道如何與我的代碼整合。
看到查詢的人,讓所有的訂單細節:
select
p.ID as order_id,
p.post_date,
max(CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END) as billing_email,
max(CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END) as _billing_first_name,
max(CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END) as _billing_last_name,
max(CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END) as _billing_address_1,
max(CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END) as _billing_address_2,
max(CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END) as _billing_city,
max(CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END) as _billing_state,
max(CASE WHEN pm.meta_key = '_billing_postcode' and p.ID = pm.post_id THEN pm.meta_value END) as _billing_postcode,
max(CASE WHEN pm.meta_key = '_shipping_first_name' and p.ID = pm.post_id THEN pm.meta_value END) as _shipping_first_name,
max(CASE WHEN pm.meta_key = '_shipping_last_name' and p.ID = pm.post_id THEN pm.meta_value END) as _shipping_last_name,
max(CASE WHEN pm.meta_key = '_shipping_address_1' and p.ID = pm.post_id THEN pm.meta_value END) as _shipping_address_1,
max(CASE WHEN pm.meta_key = '_shipping_address_2' and p.ID = pm.post_id THEN pm.meta_value END) as _shipping_address_2,
max(CASE WHEN pm.meta_key = '_shipping_city' and p.ID = pm.post_id THEN pm.meta_value END) as _shipping_city,
max(CASE WHEN pm.meta_key = '_shipping_state' and p.ID = pm.post_id THEN pm.meta_value END) as _shipping_state,
max(CASE WHEN pm.meta_key = '_shipping_postcode' and p.ID = pm.post_id THEN pm.meta_value END) as _shipping_postcode,
max(CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END) as order_total,
max(CASE WHEN pm.meta_key = '_order_tax' and p.ID = pm.post_id THEN pm.meta_value END) as order_tax,
max(CASE WHEN pm.meta_key = '_paid_date' and p.ID = pm.post_id THEN pm.meta_value END) as paid_date,
(select group_concat(order_item_name separator '|') from wp_woocommerce_order_items where order_id = p.ID) as order_items
from
wp_posts as p,
wp_postmeta as pm
where
post_type = 'shop_order' and
p.ID = pm.post_id and
post_date BETWEEN '2015-01-01' AND '2015-07-08'
and post_status = 'wc-completed'
group by
p.ID
請幫忙完成這一點,或者有沒有什麼好的方法,而不是這個?
我不想使用插件。
目前我有一個插件,但這是工作緩慢,這就是爲什麼我使這個頁面。
請幫忙解決這個問題。
嘿@Manik,你能提供更多關於你卡住的部分的描述嗎?不要相信針對您的問題的完整解決方案可以在一篇文章中解決,但也許您可以準確顯示斷開連接的位置。我看到你有一個SQL語句,它目前是否給出了正確的結果,並且你只需要在自定義WordPress查詢中使用這個語句? – Xtremefaith
您可以給wp_query提取數據,請告訴我如何使用csv進行下載。這對我真的很有幫助。 – Manik
謝謝你的回覆:)。 – Manik