should_load() ) { return; } add_action( 'woocommerce_after_single_product', array( $this, 'render_exclusive_products_section' ) ); add_filter( 'body_class', array( $this, 'body_classes' ) ); } /** * Check if the class should load. * * @return bool */ private function should_load() { return class_exists( 'WooCommerce' ); } /** * Render exclusive products section */ public function render_exclusive_products_section() { $products_category = get_theme_mod( 'neve_exclusive_products_category', '-' ); if ( $products_category === '-' ) { return; } $title = get_theme_mod( 'neve_exclusive_products_title' ); $query_args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => 10, ); if ( $products_category !== 'all' ) { $query_args['tax_query'] = array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', // This is optional, as it defaults to 'term_id' 'terms' => $products_category, 'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'. ), array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too 'operator' => 'NOT IN', ), ); } $loop = new \WP_Query( $query_args ); if ( ! $loop->have_posts() ) { return; } echo '
'; if ( ! empty( $title ) ) { echo '

' . wp_kses_post( $title ) . '

'; } echo ''; echo '
'; } /** * Add body classes contextually. * * @param array $classes the body classes. * * @return array */ public function body_classes( $classes ) { $products_category = get_theme_mod( 'neve_exclusive_products_category', '-' ); if ( $products_category === '-' || ! is_product() ) { return $classes; } $classes[] = 'nv-exclusive'; return $classes; } }