Afficher les produits phares via une boucle personnalisée dans woocommerce sur la page modèle
-
-
Ajoutez le résultat de `var_dump (get_meta_values ('_featured','product');` où lafonction `get_meta_values`estpriseen chargepar lafonctionpersonnaliséeexpliquée dans [cette réponse] (http://wordpress.stackexchange.com/a/9451/31545)Add the result from `var_dump( get_meta_values( '_featured', 'product' );` where the function `get_meta_values` is supported by the custom function explained in [this answer](http://wordpress.stackexchange.com/a/9451/31545)
- 0
- 2015-07-24
- Pieter Goosen
-
9 réponses
- votes
-
- 2015-09-15
Modifiez vos argumentspour qu'ils ressemblent à ceci:
$meta_query = WC()->query->get_meta_query(); $meta_query[] = array( 'key' => '_featured', 'value' => 'yes' ); $args = array( 'post_type' => 'product', 'stock' => 1, 'showposts' => 6, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => $meta_query );
Si vous accédez à wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php (@ 595),vouspouveztrouver comment celafonctionnepour les shortcodes WC.
Change your args to be like this:
$meta_query = WC()->query->get_meta_query(); $meta_query[] = array( 'key' => '_featured', 'value' => 'yes' ); $args = array( 'post_type' => 'product', 'stock' => 1, 'showposts' => 6, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => $meta_query );
If you go to wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php (@595) you can find how it's done for WC shortcodes.
-
Ilestimportant denoter que «_featured»n'estpas stocké sousforme de valeurnumérique.Ilest stocké sous laforme d'une chaîne «oui» ou «non».Tout le reste dans la question OP devraitfonctionner,afonctionnépourmoi.They key to note is that '_featured' is not stored as a numeric value. It is stored as a string 'yes' or 'no'. Everything else in the OP question should work, worked for me.
- 3
- 2016-07-03
- i_a
-
Depuis WooCommerce 3.0,cette solutionne fonctionneplus.Veuillez voirma réponsemise àjour ci-dessous.As of WooCommerce 3.0, this solution no longer works. Please see my updated answer below.
- 1
- 2018-06-12
- dpruth
-
- 2017-05-08
Cela a changé dans WooCommerce 3.0.Cen'estpas simplement unemeta_query,maisinclut désormais unetax_query.Les arguments sontmaintenant:
$meta_query = WC()->query->get_meta_query(); $tax_query = WC()->query->get_tax_query(); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', 'operator' => 'IN', ); $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => $atts['per_page'], 'orderby' => $atts['orderby'], 'order' => $atts['order'], 'meta_query' => $meta_query, 'tax_query' => $tax_query, );
Voir woocommerce/includes/class-wc-shortcodes.php
This has changed in WooCommerce 3.0. It's not simply a meta_query, but now includes a tax_query. The arguments are now:
$meta_query = WC()->query->get_meta_query(); $tax_query = WC()->query->get_tax_query(); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', 'operator' => 'IN', ); $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => $atts['per_page'], 'orderby' => $atts['orderby'], 'order' => $atts['order'], 'meta_query' => $meta_query, 'tax_query' => $tax_query, );
See woocommerce/includes/class-wc-shortcodes.php
-
Exactement ce queje cherchais!Just what I was looking for!
- 1
- 2018-05-10
- joshkrz
-
Aussipour Woocommerce 3.0,ils recommandent d'utiliser `wc_placeholder_img_src` au lieu de` woocommerce_placeholder_img_src`.Also for Woocommerce 3.0, they recommend using `wc_placeholder_img_src` instead of `woocommerce_placeholder_img_src`.
- 0
- 2018-05-25
- Robotnicka
-
- 2017-12-24
Boucle deproduitsen vedette dans WooCommerce 3
<ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'tax_query' => array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', ), ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?>
Featured Products Loop in WooCommerce 3
<ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'tax_query' => array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', ), ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?>
-
- 2018-05-24
Selon le Wiki WooCommerce :
La création de WP_Queries ou de requêtes debase de donnéespersonnalisées [pour récupérer desproduits]est susceptible de casser votre code dans lesfutures versions de WooCommerce àmesure que les données se déplacent vers destablespersonnaliséespour demeilleuresperformances.
WooCommercepréconise l'utilisation de
wc_get_products()
ouWC_Product_Query()
au lieu deWP_Query()
ouget_posts()
.J'ai écrit un article avec le code quej'ai utilisépour réaliser ce que vous voulezici: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
According to the WooCommerce Wiki:
Building custom WP_Queries or database queries [to retrieve products] is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.
WooCommerce advocates using
wc_get_products()
orWC_Product_Query()
instead ofWP_Query()
orget_posts()
.I've written a post with the code I used to achieve what you want here: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
-
désolé,sans voir du code écrit,ilest difficile de comprendre votre article.Pouvez-vous s'il vousplaîtinclure du codage?sorry, without seeing some written code, it is hard to understand your article. Can you please include some coding ?
- 0
- 2019-06-05
- HOY
-
@HOY leplugin d'intégration a été cassé;c'est corrigémaintenantet vouspouvez voir le code!@HOY the embed plugin was broken; it's fixed now and you can see the code!
- 0
- 2019-06-05
- cfx
-
merci,en cherchant des solutions,j'aiproposé celle-ci ci-dessous.Jene saispasen quoiil diffère du vôtre carje n'aipaspu vérifier le vôtreen profondeur,maisilesttrès courtet m'a aidé avec uneboucle deproduitpersonnalisée.https://www.kathyisawesome.com/woocommerce-modifying-product-query/thank you, while looking for solutions, I came up with this one below. I am not sure how it differs from yours since I was not able to check yours throughly but it is very short and helped me with a custom product loop. https://www.kathyisawesome.com/woocommerce-modifying-product-query/
- 0
- 2019-06-05
- HOY
-
- 2018-04-28
Je sais que c'est assez ancien,maisje viens departager une solution alternative ici et jepense que celapeut aiderceux quitouchent également à ce sujet.
Au lieu d'utiliser
meta_query
outax_query
,vouspouvez utiliser wc_get_featured_product_ids () aussi:$args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'orderby' => 'date', 'order' => 'DESC', 'post__in' => wc_get_featured_product_ids(), ); $query = new WP_Query( $args );
J'espère que cela aide!
I know this is quite old, but I've just shared an alternative solution here and I think it can help those reaching this topic too.
Instead of using
meta_query
ortax_query
, you can use wc_get_featured_product_ids() too:$args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'orderby' => 'date', 'order' => 'DESC', 'post__in' => wc_get_featured_product_ids(), ); $query = new WP_Query( $args );
I hope it helps!
-
- 2018-09-26
Basé sur: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
J'essaierais:
boucleextérieure:
$args = array ( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, ); $products = wc_get_products( $args );
dans laboucle:
$query = new WC_Product_Query( array( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, 'return' => 'ids', ) ); $products = $query->get_products();
Based on: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
I would try:
outside loop:
$args = array ( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, ); $products = wc_get_products( $args );
in the loop:
$query = new WC_Product_Query( array( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, 'return' => 'ids', ) ); $products = $query->get_products();
-
- 2020-07-12
Lesgens devraient commencer à utiliser wc_get_products car woocommerce a déclaré que c'était laméthode standard de récupération desproduits. wc_get_products a un argumentprésenté qui doitjuste être défini surtrue. Le codeest donc simplement ci-dessous.
<?php // Display featured products by category. on this case its "shirts" which is the slug of the category. $query_args = array( 'featured' => true, 'category' => array( 'shirts' ), ); $products = wc_get_products( $query_args ); global $post; $columns = wc_get_loop_prop( 'columns' ); ?> <div class="woocommerce columns-<?php echo esc_attr( $columns ); ?>"> <?php woocommerce_product_loop_start(); foreach ($products as $product) { $post = get_post($product->get_id()); setup_postdata($post); wc_get_template_part('content', 'product'); } wp_reset_postdata(); woocommerce_product_loop_end(); ?> </div>
Voir l'article completici: https://jameshwartlopez.com/plugin/get-features-products-of-a-category/
People should start using wc_get_products because woocommerce said this is the standard way of retrieving product. wc_get_products has an argument featured which just needs to be set to true. So the code is simply below.
<?php // Display featured products by category. on this case its "shirts" which is the slug of the category. $query_args = array( 'featured' => true, 'category' => array( 'shirts' ), ); $products = wc_get_products( $query_args ); global $post; $columns = wc_get_loop_prop( 'columns' ); ?> <div class="woocommerce columns-<?php echo esc_attr( $columns ); ?>"> <?php woocommerce_product_loop_start(); foreach ($products as $product) { $post = get_post($product->get_id()); setup_postdata($post); wc_get_template_part('content', 'product'); } wp_reset_postdata(); woocommerce_product_loop_end(); ?> </div>
See full post here: https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/
-
- 2016-07-11
si vous regardez dans labase de données dans latable
wp_postmeta
,vous verrez quemeta_key
sera_featured
etmeta_value
serayes
ouno
donc au lieu de la valeur0
ou1
écrivezyes
ouno
<?php $q = new WP_Query([ 'post_type' => 'product', 'stock' => 1, 'showposts' => 3, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => [ ['key' => '_featured', 'value' => 'yes' ] ] ]); if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); // display product info endwhile; wp_reset_query(); endif; ?>
if you take a look in the database in
wp_postmeta
table you will seemeta_key
will be_featured
andmeta_value
will beyes
orno
so instead of value0
or1
writeyes
orno
<?php $q = new WP_Query([ 'post_type' => 'product', 'stock' => 1, 'showposts' => 3, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => [ ['key' => '_featured', 'value' => 'yes' ] ] ]); if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); // display product info endwhile; wp_reset_query(); endif; ?>
-
- 2019-04-03
<ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'tax_query' => array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', ), ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); echo '<p>'.get_the_title().'</p>'; endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?> </ul><!--/.products-->
<ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'tax_query' => array( array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', ), ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); echo '<p>'.get_the_title().'</p>'; endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?> </ul><!--/.products-->
-
Veuillez ** [modifier] votre réponse **,et ajouter uneexplication: **pourquoi ** celapourrait-il résoudre leproblème?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2019-04-03
- fuxia
Je souhaite afficher 6produitsphares demaboutique woocommerce surmontemplate home-page.php. Après quelques recherches,j'aitrouvé que labonnefaçon de lefaire était via unebouclepersonnalisée,(jene souhaitepas utiliser de shortcodes carje voudrais ajouter des classes supplémentairespour le style,etc.) J'ai également constaté que la clé que woocommerce utilisepour leproduitsen vedetteest «_featured». J'ai rassemblé le code ci-dessouspour affichertous lesproduits quej'ai choisis commeproduitsen vedette dansmaboutique,mais celane fonctionnepas ... Toute aideest appréciée.