【问题标题】:how to retrieve woocommerce product details based on their category_id using mysql -php如何使用 mysql -php 根据其 category_id 检索 woocommerce 产品详细信息
【发布时间】:2015-07-23 13:16:53
【问题描述】:

我必须从 woocommerce 表中检索产品详细信息,例如图片标题、描述、图片链接和价格。

我已使用此查询检索到类别,

$result = mysql_query("SELECT term_id,name,slug FROM wp_terms WHERE term_id 
    IN (SELECT term_id FROM wp_term_taxonomy WHERE parent='0' 
    AND taxonomy='product_cat') ORDER BY name ASC");

基于此类别 ID,我正在检索这样的子类别:

  $result = mysql_query("SELECT term_id,name,slug FROM wp_terms WHERE term_id
     IN (SELECT term_id FROM wp_term_taxonomy WHERE parent='$cat_id'
     AND taxonomy='product_cat') ORDER BY name ASC");

我已尝试查询以获取产品详细信息,

 $result = mysql_query("SELECT  `ID`,`post_title`,`post_content`,`guid` FROM wp_posts WHERE
post_type='product' and post_status='publish' and ID IN(SELECT object_id
FROM wp_term_relationships WHERE term_taxonomy_id IN('$cat_id') and 
term_taxonomy_id IN(SELECT term_taxonomy_id FROM wp_term_taxonomy 
where taxonomy='product_cat'))");

当我在phpmyadmin 中执行它时,这给了我标题和描述,但是当我在 php 中给出相同的代码并通过json 发送响应时,它给了我一个空响应。

我认为我的查询不正确。

请建议我如何从 woocommerce 获取产品详细信息,我是第一次使用它。

【问题讨论】:

  • 这不会返回多个产品吗?你到底想做什么?你不应该为此需要任何 SQL...
  • 是的。我需要一份产品清单。连同它的详细信息。我通过json 将响应发送到我的 android 应用程序并在移动设备中显示这些项目

标签: php mysql wordpress woocommerce


【解决方案1】:

听起来您可以为此使用标准的WP_Query()

$args = array( 
    'post_type' => 'product', 
    'posts_per_page' => -1, 
    'product_cat' => $cat_id, 
);
$result = new WP_Query( $args );

【讨论】:

  • 非常感谢您的宝贵时间。我可以在 php 中包含 wordpress 功能吗?
  • 是的,这是一种选择。另一种选择是使用WP REST API plugin。您可以执行相同的查询,它会自动返回 JSON。
  • 我想它会变得复杂@rnevius ..用mysql查询是不是可能的。
【解决方案2】:

我终于完成了

步骤如下:

    --->  get `id` of product from `wp_posts`
    --->  pass it as a `post_id` to `wp_postmeta` and get the `meta_value` where `meta_key`='_thumbnail_id'
    --->  now send this `meta_value` as `id` of the product to `wp_posts `
    --->  in the field `guid` we have the link of image of the product

要获得产品的价格:

---> Send `id` as a `post_id` to `wp_postmeta` and get the `meta_value` where `meta_key`='_price'

Product descriptionproduct title 分别存储在wp_posts 的字段post_titlepost_excerpt 中。

我已经分别执行了所有这些查询。这就是为什么我没有在这个答案中添加它们。一旦完成joins

,我将编辑这个答案

编码愉快.. :)

【讨论】:

    【解决方案3】:

    试试这个,

    global $wpdb;
    $products = $wpdb->get_results("SELECT *
                                    FROM wp_posts
                                    LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
                                    LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
                                     WHERE wp_term_taxonomy.term_id = 60
                                     ROUP BY wp_posts.post_title");
    

    您必须根据您的类别编号更改WHERE wp_term_taxonomy.term_id = 60,例如WHERE wp_term_taxonomy.term_id = $cat_no 或类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      相关资源
      最近更新 更多