Mostra i prodotti in vetrina attraverso il loop personalizzato in woocommerce nella pagina dei modelli
-
-
Aggiungiil risultato da `var_dump (get_meta_values ('_featured','product');` dove lafunzione `get_meta_values` è supportata dallafunzionepersonalizzata spiegatain [questa risposta] (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 risposta
- voti
-
- 2015-09-15
Cambiai tuoi argomentiin questomodo:
$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 vai a wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php (@ 595)puoi scoprire come sifapergli shortcode 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 chiave danotare è che "_featured"non vienememorizzato come valorenumerico.Vienememorizzato come una stringa "sì" o "no".Tuttoil restonella domanda OP dovrebbefunzionare,hafunzionatoperme.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
-
Apartire da WooCommerce 3.0,questa soluzionenonfunzionapiù.Siprega di vedere lamia risposta aggiornata di seguito.As of WooCommerce 3.0, this solution no longer works. Please see my updated answer below.
- 1
- 2018-06-12
- dpruth
-
- 2017-05-08
Questo è cambiatoin WooCommerce 3.0.Non è semplicemente unameta_query,ma orainclude unatax_query.Gli argomenti sono ora:
$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, );
Vedi 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
-
Proprio quello che stavo cercando!Just what I was looking for!
- 1
- 2018-05-10
- joshkrz
-
Ancheper Woocommerce 3.0,raccomandano di usare `wc_placeholder_img_src`invece di` 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
Ciclo diprodottiin vetrinain 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
Secondoil WooCommerce Wiki :
La creazione di WP_Queries o query di databasepersonalizzate [per recuperarei prodotti] rischia di danneggiareil codice nelle versionifuture di WooCommercemanmano chei dati si spostano versotabellepersonalizzateperprestazionimigliori.
WooCommerce sostiene l'utilizzo di
wc_get_products()
oWC_Product_Query()
invece diWP_Query()
oget_posts()
.Ho scritto unpost conil codice che ho usatoper ottenere ciò che desideri qui: 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/
-
scusa,senza vedere del codice scritto,è difficile capireiltuo articolo.Puoiincludere unpo 'di codice?sorry, without seeing some written code, it is hard to understand your article. Can you please include some coding ?
- 0
- 2019-06-05
- HOY
-
@HOYilplug-in diincorporamentononfunzionava;ora è stato risoltoe puoi vedereil codice!@HOY the embed plugin was broken; it's fixed now and you can see the code!
- 0
- 2019-06-05
- cfx
-
grazie,mentre cercavo soluzioni,hopensato a questa di seguito.Non sono sicuro di come sia diverso daltuopoichénon sono statoin grado di controllareiltuo afondo,ma èmoltobrevee mi ha aiutato con un ciclo diprodottipersonalizzati.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
So che èpiuttosto vecchio,ma ho appena condiviso una soluzione alternativa qui e penso chepossa aiutareanche quelli che affrontano questo argomento.
Invece di utilizzare
meta_query
otax_query
,puoi utilizzare anche wc_get_featured_product_ids () :$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 );
Spero che aiuti!
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
Basato su: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
Proverei:
loopesterno:
$args = array ( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, ); $products = wc_get_products( $args );
nel ciclo:
$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
Lepersone dovrebberoiniziare a utilizzare wc_get_productsperché woocommerce ha affermato che questo èilmodo standardper recuperareilprodotto. wc_get_products ha un argomentoin primopiano che deveessereimpostato sutrue. Quindiil codice è semplicemente sotto.
<?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>
Leggiilpost completo qui: https://jameshwartlopez.com/plugin/get-Featured-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
se dai un'occhiata al databasenellatabella
wp_postmeta
vedrai chemeta_key
sarà_featured
emeta_value
saràyes
ono
quindi alposto del valore0
o1
scriviyes
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-->
-
Perfavore ** [modifica] latua risposta **e aggiungi una spiegazione: **perché ** questopotrebbe risolvereilproblema?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2019-04-03
- fuxia
Vorreimostrare 6prodottiin vetrina dalmionegozio woocommerce sulmiotemplate home-page.php. Dopo alcune ricerche ho scoperto cheilmodogiustoperfarloera attraverso un ciclopersonalizzato,(non desidero utilizzare codicibreviperché vorrei aggiungere classi aggiuntiveper lo styling,ecc.) Ho anche scoperto che la chiave che woocommerce usaperili prodottipresentati è "_featured". Homessoinsiemeil codice qui sottoper visualizzaretuttii prodotti che ho scelto dipresentareprodottinelmionegozio,manonfunziona ... Qualsiasi aiuto è apprezzato.