【问题标题】:Get all parent products with "instock" stock status in Woocommerce在 Woocommerce 中获取所有具有“instock”库存状态的父产品
【发布时间】:2018-06-09 22:11:34
【问题描述】:

在 WooCommerce 中,我有以下 SQL 查询来选择父产品。我想只选择“有货”的产品。

这是我的实际 SQL 查询:

$query=$db->query("select * from wp_posts 
  where post_parent='0' and post_type='product'  and post_status='publish' 
  group by ID 
  limit 10");

如何仅选择“有货”父产品(但不是全部)

【问题讨论】:

  • 更新您的问题添加适当的数据样本和预期结果
  • @scaisEdge 已按要求更新
  • 我没有看到任何明显的 .. 数据样本 ..

标签: php sql wordpress woocommerce product


【解决方案1】:

仅获取具有“instock”等库存状态的父产品的正确方法: :

global $wpdb;

// The SQL query
$results = $wpdb->get_results( "
    SELECT p.*, pm.meta_value as stock_status
    FROM {$wpdb->prefix}posts as p
    INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
    WHERE p.post_type LIKE 'product'
    AND p.post_status LIKE 'publish'
    AND p.post_parent = '0'
    AND pm.meta_key LIKE '_stock_status'
    AND pm.meta_value LIKE 'instock' 
    GROUP BY p.ID
" );

// Testing output (objects array)
echo '<pre>'; print_r($results); echo '</pre>';

经过测试并且有效。


仅获取产品 ID:

global $wpdb;

// The SQL query
$results = $wpdb->get_col( "
    SELECT p.ID
    FROM {$wpdb->prefix}posts as p
    INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
    WHERE p.post_type LIKE 'product'
    AND p.post_status LIKE 'publish'
    AND p.post_parent = '0'
    AND pm.meta_key LIKE '_stock_status'
    AND pm.meta_value LIKE 'instock' 
" );

// Testing output (array of IDs)
echo '<pre>'; print_r($results); echo '</pre>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2019-09-03
    • 2017-10-09
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多