Php WooCommerce:在回路内部显示产品变化
如何在一个循环中显示每个产品的产品变体,如商店页面上的产品变体?是否有任何功能可以添加到内容产品模板中,用于检索变体列表并显示它们?最好的方法是更改循环。这样做会有帮助:Php WooCommerce:在回路内部显示产品变化,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,如何在一个循环中显示每个产品的产品变体,如商店页面上的产品变体?是否有任何功能可以添加到内容产品模板中,用于检索变体列表并显示它们?最好的方法是更改循环。这样做会有帮助: function filter_wc_query($query_args){ if(is_archive()){ //here you can use any conditional function to show variations on $query_args[] = ['post_
function filter_wc_query($query_args){
if(is_archive()){ //here you can use any conditional function to show variations on
$query_args[] = ['post_parent'=> '*'];
}
return $query_args;
}
add_filter('woocommerce_get_query_vars','filter_wc_query');
或者,如果要在视图级别执行此操作:
在此解决方案图像中,您可以通过标准WP功能抓取:
get_post_thumbnail($product->ID,'shop_list');
但请记住,变体链接会将用户带到父产品,并迫使用户再次选择变体。最好的方法是改变循环。这样做会有帮助:
function filter_wc_query($query_args){
if(is_archive()){ //here you can use any conditional function to show variations on
$query_args[] = ['post_parent'=> '*'];
}
return $query_args;
}
add_filter('woocommerce_get_query_vars','filter_wc_query');
或者,如果要在视图级别执行此操作:
在此解决方案图像中,您可以通过标准WP功能抓取:
get_post_thumbnail($product->ID,'shop_list');
但是请记住,变体链接会将用户带到父产品,并迫使用户再次选择变体。您可以使用此代码将产品变体添加到商店/产品类别循环中
// Load our function when hook is set
add_action( 'pre_get_posts', 'custom_modify_query_get_posts_by_date' );
// Modify the current query
function custom_modify_query_get_posts_by_date( $query ) {
// Check if on frontend and main query is modified and if its shop or product category loop
if( (! is_admin() && $query->is_main_query()) && ( is_shop() || is_product_category() ) ) {
$query->set( 'order', 'ASC' );
add_filter( 'posts_where', 'rc_filter_where' );
}
return $query;
}
// Add products variation post type to the loop
function rc_filter_where( $where = '' ) {
$type = 'product_variation';
$where .= " OR post_type = '$type'";
return $where;
}
您可以使用此代码将产品变体添加到商店/产品类别循环中
// Load our function when hook is set
add_action( 'pre_get_posts', 'custom_modify_query_get_posts_by_date' );
// Modify the current query
function custom_modify_query_get_posts_by_date( $query ) {
// Check if on frontend and main query is modified and if its shop or product category loop
if( (! is_admin() && $query->is_main_query()) && ( is_shop() || is_product_category() ) ) {
$query->set( 'order', 'ASC' );
add_filter( 'posts_where', 'rc_filter_where' );
}
return $query;
}
// Add products variation post type to the loop
function rc_filter_where( $where = '' ) {
$type = 'product_variation';
$where .= " OR post_type = '$type'";
return $where;
}