Muestre los productos destacados a través de un bucle personalizado en woocommerce en la página de la plantilla
-
-
Agregueel resultado de `var_dump (get_meta_values ('_featured','product');` donde lafunción `get_meta_values`es compatible con lafunciónpersonalizadaexplicadaen [esta respuesta] (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 respuestas
- votos
-
- 2015-09-15
Cambie sus argumentospara que sean así:
$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 va a wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php (@ 595),puedeencontrar cómo se hacepara los códigos cortos 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.
-
La clave ateneren cuentaes que '_featured'no se almacena como un valornumérico.Se almacena como una cadena 'sí' o 'no'.Todo lo demásen lapregunta de OP deberíafuncionar,funcionóparamí.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 de WooCommerce 3.0,esta solución yanofunciona.Consultemi respuesta actualizada a continuación.As of WooCommerce 3.0, this solution no longer works. Please see my updated answer below.
- 1
- 2018-06-12
- dpruth
-
- 2017-05-08
Esto ha cambiadoen WooCommerce 3.0.Noes simplemente unameta_query,sino que ahoraincluye unatax_query.Los argumentos ahora son:
$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/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
-
¡Justo lo queestababuscando!Just what I was looking for!
- 1
- 2018-05-10
- joshkrz
-
Tambiénpara Woocommerce 3.0,recomiendan usar `wc_placeholder_img_src`en lugar 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
Bucle deproductos destacadosen 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 acuerdo con WooCommerce Wiki :
Esprobable que la creación de consultas debase de datos o WP_Queriespersonalizadas [para recuperarproductos] rompa su código en futuras versiones de WooCommerce amedida que los datos semueven haciatablaspersonalizadaspara unmejor rendimiento.
WooCommerce defiendeel uso de
wc_get_products()
oWC_Product_Query()
en lugar deWP_Query()
oget_posts()
.Heescrito unapublicación conel código que usépara lograr lo que quieres aquí: 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/
-
lo siento,sin ver algún códigoescrito,es difícilentender su artículo.¿Puedesincluir algo de codificación?sorry, without seeing some written code, it is hard to understand your article. Can you please include some coding ?
- 0
- 2019-06-05
- HOY
-
@HOYel complemento deinserciónestaba roto;¡está arreglado ahora ypuedes verel código!@HOY the embed plugin was broken; it's fixed now and you can see the code!
- 0
- 2019-06-05
- cfx
-
gracias,mientrasbuscaba soluciones,seme ocurrióesta a continuación.Noestoy seguro deen qué se diferencia del suyo,ya quenopude revisarel suyo afondo,peroesmuy corto yme ayudó con un ciclo deproductopersonalizado.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
Sé queestoesbastante antiguo,pero acabo de compartir una solución alternativa aquí y creo quepuede ayudarlos que llegan aestetematambién.
En lugar de utilizar
meta_query
otax_query
,puede utilizar wc_get_featured_product_ids () también:$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 quete ayude!
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
Basadoen: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
Intentaría:
bucleexterior:
$args = array ( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, ); $products = wc_get_products( $args );
en elbucle:
$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
Lagente debería comenzar a usar wc_get_productsporque woocommerce dijo queestaes laformaestándar de recuperarproductos. wc_get_productstiene un argumento que solo debeestablecerseen verdadero. Entoncesel códigoestá simplemente debajo.
<?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>
Vea lapublicación completa aquí: https://jameshwartlopez.com/plugin/obtener-productos-destacados-de-una-categoría/
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
siecha un vistazo a labase de datosen latabla
wp_postmeta
verámeta_key
será_featured
ymeta_value
seráyes
ono
así queen lugar del valor0
o1
escribayes
ono
<?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-->
-
** [edite] su respuesta ** y agregue unaexplicación: ** ¿por qué **estopodría resolverelproblema?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2019-04-03
- fuxia
Megustaríamostrar 6productos destacados demi tienda woocommerceen miplantilla home-page.php. Después deinvestigar unpoco,descubrí que laforma correcta de hacerestoera através de unbuclepersonalizado (no deseo usar códigos cortosporqueme gustaría agregar clases adicionalesparaelestilo,etc.) Tambiénencontré que la clave que usa woocommerceparael Losproductos destacados son '_featured'. Reuníel código a continuaciónparamostrar losproductos queelegí comoproductos destacadosen mitienda,peronofunciona ... Se agradece cualquier ayuda.