Exibir produtos em destaque através de loop personalizado em WooCommerce na página de modelo
-
-
Adicione o resultado de `var_dump (get_meta_values ('_Featured','produto');` onde afunção `get_meta_values` é suportadapelafunçãopersonalizadaexplicadaem [esta resposta] (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 respostas
- votos
-
- 2015-09-15
mude seus argspara ser assim:
$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 );
Se vocêfor a WP-Conteúdo/Plugins/WooCommerce/Inclui/Class-WC-shortcodes.php (@ 595) Vocêpodeencontrar como éfeitopara códigos de códigos de 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.
-
Eles digitam anota é que '_Featured'não é armazenado como um valornumérico.É armazenado como uma corda "sim" ou "não".Tudo o restonapergunta op devefuncionar,trabalhouparamim.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
-
Apartir do WooCommerce 3.0,esta soluçãonãofuncionamais.Porfavor,vejaminha resposta atualizada abaixo.As of WooCommerce 3.0, this solution no longer works. Please see my updated answer below.
- 1
- 2018-06-12
- dpruth
-
- 2017-05-08
Issomudouno WooCommerce 3.0.Não é simplesmente ummeta_query,mas agorainclui umtax_query.Os argumentos são agora:
$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, );
Consulte WooCommerce/Inclui/Classe-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
-
Émesmo o queeuprocurava!Just what I was looking for!
- 1
- 2018-05-10
- joshkrz
-
Tambémpara o WooCommerce 3.0,eles recomendam usar `wc_placeholder_img_src`em vez 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
Loop deprodutosem destaqueem 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
De acordo com o WooCommerce Wiki :
.
Construindo WP_Queriespersonalizados ou consultas dobanco de dados [para recuperarprodutos] éprovável quebrar seu código em versõesfuturas de WooCommerce comomovimentos de dadosparatabelaspersonalizadasparamelhor desempenho.
woocommerce advogados usando
wc_get_products()
ouWC_Product_Query()
em vez deWP_Query()
ouget_posts()
.Euescrevi umpost com o código queeu costumava conseguir o que você quer aqui: 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/
-
Desculpe,sem ver algum códigoescrito,é difícilentender seu artigo.Vocêpodeincluir alguma codificação?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 opluginincorporadofoi quebrado;Está corrigido agorae vocêpode ver o código!@HOY the embed plugin was broken; it's fixed now and you can see the code!
- 0
- 2019-06-05
- cfx
-
Obrigado,aoprocurar soluções,cheguei aeste abaixo.Nãotenho certeza de comoisso difere do seu desde quenão consegui verificar o seu altamente,mas émuito curtoe me ajudou com um loop deprodutopersonalizado.https://www.kathyisawesome.com/woocommerce-modificar-produto-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
Eu sei queisso ébastante antigo,maseu acabei de compartilhar uma solução alternativa aqui e eu acho quepode ajudaraqueles que atingemessetópicotambém.
Em vez de usar
meta_query
outax_query
,vocêpode usar wc_get_featured_product_ids () também:$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 );
Espero que ajude!
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
Baseadoem: https://github.com/wooCommerce/wooocommerce/wiki/wc_get_products-and-wc_product_query
Eutentaria:
Loopexterno:
$args = array ( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, ); $products = wc_get_products( $args );
no 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();
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
Aspessoas devem começar a usar wc_get_productsporque o WooCommerce disse queesta é amaneirapadrão de recuperar oproduto. wc_get_productstem um argumentoem destaque,que sóprecisa ser definido como TRUE. Então o códigoestá simplesmente abaixo.
<?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>
verposte completo aqui: https://jameshwartlopez.com/plugin/get-destreed-produtos-de-categoria/
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
Se vocêfizer uma olhadanobanco de dadosem
wp_postmeta
tabela,você verámeta_key
será_featured
emeta_value
seráyes
ouno
em vez de valor0
ou1
gravaryes
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-->
-
Porfavor ** [editar] sua resposta **e adicione umaexplicação: ** Por que **poderia resolver oproblema?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2019-04-03
- fuxia
Eugostaria deexibir 6produtosem destaque daminha loja WooCommercenomeumodelo home-page.php. Depois de algunspesquisados,descobri que amaneira certa defazerissofoi através de um looppersonalizado,(não desejo usar códigos de acessoporquegostaria de adicionar classes adicionaisparaestiloetc.) Eutambém descobri que a chave que WooCommerce usapara o Produtosem destaque é '_Featured'. Eumontei o código abaixoparaexibir quaisquerprodutos queeuescolhi serprodutosem destaquenaminha loja,masnãofunciona ... Qualquer ajuda é apreciada.