我有一个非常简单的问题。但它困扰了我很多。
任何人都可以告诉我,从WordPress woo-commerce网站下订单后,订单及其中的所有内容都存储在数据库中。更准确的是,任何人都可以告诉我如何在不使用WC_Order类等woocommerce类的情况下获取我的送货地址?我的意思是我需要通过自定义数据库查询手动获取数据,但我无法在数据库中找到订单及其相关的所有内容?我知道订单是作为wp-posts表中的帖子存储在数据库中的,但是其余部分,即送货地址账单地址等等?我希望我不要混淆任何人。
最诚挚的问候,
MAK
答案 0 :(得分:26)
订单是自定义帖子类型(CPT),因此它们存储在wp_posts表中。如果在post_type字段中搜索“shop_order”,SQL将检索所有订单。
然后,您必须在wp_postmeta表中搜索post_id与订单帖子的ID匹配的所有记录。您将在wp_postmeta表中找到的字段中包含整个送货地址和帐单地址。
答案 1 :(得分:7)
此外,订单数据将存储在woocommerce_order_items和woocommerce_order_itemmeta表格中(对于WooCommerce> 2.5我相信)这些表格包含与客户购买的实际产品相关的内容。
shop_order帖子条目的post_id与woocommerce_order_items中的order_id相匹配。 woocommerce_order_items中的order_item_id与woocommerce.order_itemmeta中的order_item_id相匹配。
答案 2 :(得分:4)
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 p
join wp_postmeta pm on p.ID = pm.post_id
join wp_woocommerce_order_items oi on p.ID = oi.order_id
where
post_type = 'shop_order' and
post_date BETWEEN '2015-01-01' AND '2015-07-08' and
post_status = 'wc-completed' and
oi.order_item_name = 'Product Name'
group by
p.ID
来源here。
答案 3 :(得分:2)
此查询应该可以帮助您。您只需要更改自己的数据库的数据库前缀:
SELECT *
FROM
adolfoma_comoconq_wp470.wpkn_postmeta
INNER JOIN
adolfoma_comoconq_wp470.wpkn_posts
ON adolfoma_comoconq_wp470.wpkn_posts.ID=adolfoma_comoconq_wp470.wpkn_postmeta.post_id
where adolfoma_comoconq_wp470.wpkn_posts.post_type ="shop_order";
我刚刚写了它,我可以看到电子邮件,帐单地址,姓名,购买金额等。我没有看到确切的项目和金额,可能需要额外的查询。
答案 4 :(得分:1)
我可以确认woocommerce订单是自定义帖子类型,因此它们存储在wp_posts中。
WooCommerce订单是"自定义帖子"它们存储在" wp_posts"在" post_type" - > "" shop_order"
如果您想选择带有SQL查询的车间订单,您可以执行以下操作。
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order'", ARRAY_A );