php Woocommerce 获取产品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21028830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Woocommerce get products
提问by Tester
I used the following code to get the list of product categories form WooCommerce in my WordPress website:
我使用以下代码在我的 WordPress 网站中从 WooCommerce 获取产品类别列表:
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 0; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<?php $all_categories = get_categories( $args );
//print_r($all_categories);
foreach ($all_categories as $cat) {
//print_r($cat);
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
?>
<?php
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; ?>
<?php
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
} ?>
<?php }
}
?>
This works fine and returns the list of product categories. I have been trying now to get a list of products for a particular category.
这工作正常并返回产品类别列表。我现在一直在尝试获取特定类别的产品列表。
Example: get all the products for with cat_id=34.
示例:获取 with 的所有产品cat_id=34。
I know products are stored as posts, and have been trying to get this done but can't seem to.
我知道产品存储为帖子,并且一直试图完成此操作,但似乎无法完成。
How do I get the list of products for a particular category?
如何获取特定类别的产品列表?
回答by Suman.hassan95
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'hoodies'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;
wp_reset_query();
?>
This will list all product thumbnails and names along with their links to product page. change the category name and posts_per_page as per your requirement.
这将列出所有产品缩略图和名称以及它们到产品页面的链接。根据您的要求更改类别名称和posts_per_page。
回答by Christian Lescuyer
Do not use WP_Query()or get_posts(). From the WooCommerce doc:
不要使用WP_Query()或get_posts()。来自 WooCommerce 文档:
wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.
wc_get_products 和 WC_Product_Query 提供了一种检索产品的标准方法,该方法可以安全使用并且不会因未来 WooCommerce 版本中的数据库更改而中断。构建自定义 WP_Queries 或数据库查询可能会在 WooCommerce 的未来版本中破坏您的代码,因为数据会移向自定义表以获得更好的性能。
You can retrieve the products you want like this:
您可以像这样检索您想要的产品:
$args = array(
'category' => array( 34 ),
'orderby' => 'name',
);
$products = wc_get_products( $args );
回答by Hunter WebDev
<?php
$args = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args );
?>
This should grab all the products you want, I may have the post type wrong though I can't quite remember what woo-commerce uses for the post type. It will return an array of products
这应该包含你想要的所有产品,我可能有错误的帖子类型,虽然我不太记得 woo-commerce 用于帖子类型的内容。它将返回一系列产品
回答by Hritik Pandey
Always use tax_query to get posts/products from a particular category or any other taxonomy. You can also get the products using ID/slug of particular taxonomy...
始终使用 tax_query 从特定类别或任何其他分类法中获取帖子/产品。您还可以使用特定分类法的 ID/slug 获取产品...
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'accessories',
)
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post();
the_title(); echo "<br>";
endwhile;
wp_reset_postdata();

