如何根据 category_id 使用 mysql -php 检索 woocommerce 产品详细信息
how to retrieve woocommerce product details based on their category_id using mysql -php
我必须从 woocommerce table.
检索产品详细信息,例如图片标题、描述、图片 link 和价格
我已经使用此查询检索了类别,
$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 获取产品详细信息,我是第一次使用它。
听起来您可以为此使用标准 WP_Query()
:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_cat' => $cat_id,
);
$result = new WP_Query( $args );
我终于搞定了
步骤如下:
---> 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 description
和product title
分别存储在tablewp_posts
字段post_title
和post_excerpt
中。
我已经分别执行了所有这些查询。这就是为什么我不在这个 answer.I 中添加它们的原因joins
完成后将编辑此答案
编码愉快..:)
试试这个,
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
或类似的内容。
我必须从 woocommerce table.
检索产品详细信息,例如图片标题、描述、图片 link 和价格我已经使用此查询检索了类别,
$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 获取产品详细信息,我是第一次使用它。
听起来您可以为此使用标准 WP_Query()
:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_cat' => $cat_id,
);
$result = new WP_Query( $args );
我终于搞定了
步骤如下:
---> 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 description
和product title
分别存储在tablewp_posts
字段post_title
和post_excerpt
中。
我已经分别执行了所有这些查询。这就是为什么我不在这个 answer.I 中添加它们的原因joins
编码愉快..:)
试试这个,
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
或类似的内容。