diff --git a/features/woocommerce/widget.php b/features/woocommerce/widget.php new file mode 100644 index 0000000000..08e00d8f0b --- /dev/null +++ b/features/woocommerce/widget.php @@ -0,0 +1,124 @@ + esc_html__( 'Show related products using ElasticPress. This widget will only appear on single product.', 'elasticpress' ) ); + parent::__construct( 'ep-related-products', esc_html__( 'ElasticPress - Related Products', 'elasticpress' ), $options ); + } + + /** + * Display widget + * + * @param array $args + * @param array $instance + */ + public function widget( $args, $instance ) { + + if ( ! is_singular( 'product' ) ) { + return; + } + + $related_products = get_transient( 'ep_related_products_' . get_the_ID() ); + + if ( false === $related_products ) { + $related_products = ep_wc_get_related_products( get_the_ID(), $instance['num_posts'] ); + + if ( empty( $related_products ) ) { + if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { + set_transient( 'ep_related_products_' . get_the_ID(), '', HOUR_IN_SECONDS ); // Let's not spam + } + + return; + } + + ob_start(); + + echo $args['before_widget']; + + if ( ! empty( $instance['title'] ) ) { + echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'] ); + } + ?> + +
+ + + +
+ ++ + + +
+ $product_id, + 'posts_per_page' => $limit, + 'ep_integrate' => true, + 'post__not_in' => $exclude_ids, + 'tax_query' => array( + 'relation' => 'OR', + array( + 'taxonomy' => 'product_cat', + 'field' => 'term_id', + 'terms' => $cats_array, + ), + array( + 'taxonomy' => 'product_tag', + 'field' => 'term_id', + 'terms' => $tags_array, + ), + ), + ); + + $query = new WP_Query( apply_filters( 'ep_wc_get_related_products_args', $args ) ); + + if ( ! $query->have_posts() ) { + return false; + } + return $query->posts; +} + /** * Setup all feature filters * @@ -648,6 +702,7 @@ function ep_wc_setup() { add_filter( 'ep_post_sync_args_post_prepare_meta', 'ep_wc_add_order_items_search', 20, 2 ); add_action( 'pre_get_posts', 'ep_wc_translate_args', 11, 1 ); add_action( 'parse_query', 'ep_wc_search_order', 11, 1 ); + add_action( 'widgets_init', 'ep_related_products_register_widget' ); } }