【问题标题】:Woocommerce WP - Get _sku value from post_type = shop_order | SQL QUERYWoocommerce WP - 从 post_type = shop_order | 获取 _sku 值SQL查询
【发布时间】:2017-08-07 19:32:13
【问题描述】:

Woocommercewp_posts 中有两种 post_type,'product' & 'shop_order'。

shop_order ID,我在哪里可以获得 _skuwp_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_orderproduct 的原始页面 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

【问题讨论】:

  • 换句话说,我在哪里可以找到 post_type = 'product' 和 post_type = 'shop_order' 商品之间的关系。
  • 一个订单有订单商品。一些订单项目用于产品并具有产品 ID。您需要在 wp_posts、wp_woocommerce_order_items、wp_woocommerce_order_itemmeta、wp_posts 和 wp_postmeta 表上进行连接。

标签: php mysql wordpress woocommerce shop


【解决方案1】:

这是查找特定产品的 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]

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多