Woocommerce 在 wp_posts 中有两种类型的 post_type,“产品”和“< em>shop_order'。
根据shop_order ID,我可以在哪里获取_sku? wp_postmeta 具有 _sku,它直接连接到 post_type = 'product'
而不是 post_type = 'shop_order'
。到目前为止有这个:
SELECT
oi.order_item_id,
oi.order_item_name,
oi.order_item_type,
oi.order_id,
max( CASE WHEN oim.meta_key = '_line_subtotal' and oim.order_item_id = oi.order_item_id THEN oim.meta_value END ) as _line_subtotal,
max( CASE WHEN oim.meta_key = '_line_total' and oim.order_item_id = oi.order_item_id THEN oim.meta_value END ) as _line_total,
max( CASE WHEN oim.meta_key = '_product_id' and oim.order_item_id = oi.order_item_id THEN oim.meta_value END ) as _product_id,
max( CASE WHEN oim.meta_key = '_variation_id' and oim.order_item_id = oi.order_item_id THEN oim.meta_value END ) as _variation_id,
max( CASE WHEN oim.meta_key = '_qty' and oim.order_item_id = oi.order_item_id THEN oim.meta_value END ) as _quantaty
FROM
wp_woocommerce_order_items oi INNER JOIN
wp_woocommerce_order_itemmeta oim on oi.order_item_id = oim.order_item_id
where oi.order_id=:order_item
group by oi.order_item_id
");
$sth2->bindParam(':order_item', $_GET["order"], PDO::PARAM_STR);
$sth2->execute();
如何在此 SQL 查询中打印 _sku 值?
shop_order 到 product 的原始页面 ID 的连接在哪里 post_type = 'product '
_sku 值位于 wp_postmeta 中:
post_id = where wp_post.ID (which is the ID of the product, not the shop_order)
meta_key = _sku
meta_value = my_sku_value_here
最佳答案
以下是查找特定产品的 woocommerce 订单的查询:
select ord.ID
from wp_posts prd
join wp_woocommerce_order_itemmeta om on om.meta_key = '_product_id' and om.meta_value = prd.ID
join wp_woocommerce_order_items o on o.order_item_id = om.order_item_id
join wp_posts ord on ord.ID = o.order_id
where prd.ID = [Specific product ID]
关于php - Woocommerce WP - 从 post_type = shop_order | 获取 _sku 值SQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45554423/