Skip to content

Instantly share code, notes, and snippets.

Created August 11, 2017 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3ddd86d2af6eb1139c02a32a3edade10 to your computer and use it in GitHub Desktop.
Save anonymous/3ddd86d2af6eb1139c02a32a3edade10 to your computer and use it in GitHub Desktop.
/**
* Carousel details.
*
* @since 1.0.0
*
* @param array $input Carousel details.
*/
function clean_commerce_get_carousel_details( $input ) {
$featured_carousel_type = clean_commerce_get_option( 'featured_carousel_type' );
$featured_carousel_number = clean_commerce_get_option( 'featured_carousel_number' );
switch ( $featured_carousel_type ) {
case 'featured-category':
$featured_carousel_category = clean_commerce_get_option( 'featured_carousel_category' );
$qargs = array(
'posts_per_page' => esc_attr( $featured_carousel_number ),
'no_found_rows' => true,
'post_type' => 'post',
'meta_query' => array(
array( 'key' => '_thumbnail_id' ),
),
);
if ( absint( $featured_carousel_category ) > 0 ) {
$qargs['category'] = esc_attr( $featured_carousel_category );
}
// Fetch posts.
$all_posts = get_posts( $qargs );
$carousels = array();
if ( ! empty( $all_posts ) ) {
$cnt = 0;
foreach ( $all_posts as $key => $post ) {
if ( has_post_thumbnail( $post->ID ) ) {
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'clean-commerce-carousel' );
$carousels[ $cnt ]['images'] = $image_array;
$carousels[ $cnt ]['title'] = $post->post_title;
$carousels[ $cnt ]['url'] = get_permalink( $post->ID );
$cnt++;
}
}
}
if ( ! empty( $carousels ) ) {
$input = $carousels;
}
break;
case 'featured-product-category':
$featured_carousel_product_category = clean_commerce_get_option( 'featured_carousel_product_category' );
$qargs = array(
'posts_per_page' => esc_attr( $featured_carousel_number ),
'no_found_rows' => true,
'post_type' => 'product',
'meta_query' => array(
array( 'key' => '_thumbnail_id' ),
),
);
if ( absint( $featured_carousel_product_category ) > 0 ) {
$qargs['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'terms' => esc_attr( $featured_carousel_product_category ),
// 'field' => 'slug',
),
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'exclude-from-catalog',
'operator' => 'NOT IN',
),
);
}
// Fetch posts.
$all_posts = get_posts( $qargs );
//die(print_r($all_posts));
$carousels = array();
if ( ! empty( $all_posts ) ) {
$cnt = 0;
foreach ( $all_posts as $key => $post ) {
if ( has_post_thumbnail( $post->ID ) ) {
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'clean-commerce-carousel' );
$carousels[ $cnt ]['images'] = $image_array;
$carousels[ $cnt ]['title'] = $post->post_title;
$carousels[ $cnt ]['url'] = get_permalink( $post->ID );
$cnt++;
}
}
}
if ( ! empty( $carousels ) ) {
$input = $carousels;
}
break;
default:
break;
}
return $input;
}
@quintorium
Copy link

In the Customizing panel under the settings section of Carousel Type, when there is no product category selected the carousel keeps displaying the hidden products. However this issue disappears when a product cat is picked. Consequently I suggest you also alter the query when $featured_carousel_product_category is empty or equal to 0. Please add the code below between lines 66 and 67:

					'tax_query'      => array(					
					    array(
							'taxonomy' => 'product_visibility',
					        'field'    => 'name',
					        'terms'    => 'exclude-from-catalog',
					        'operator' => 'NOT IN',
						),
					),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment