Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add woocommerce related product widget #850

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions features/woocommerce/widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/**
* Related products widget
*
* @since 2.4
*/
class EP_Related_Products_Widget extends WP_Widget {

/**
* Initialize the widget
*/
public function __construct() {
$options = array( 'description' => 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'] );
}
?>

<ul>
<?php foreach ( $related_products as $post ) : ?>
<li>
<a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"><?php echo esc_html( get_the_title( $post->ID ) ); ?></a>
</li>
<?php endforeach; ?>
</ul>

<?php
wp_reset_postdata();

echo wp_kses_post( $args['after_widget'] );

$related_products = ob_get_clean();

if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
set_transient( 'ep_related_posts_' . get_the_ID(), $related_products, HOUR_IN_SECONDS );
}
}

echo $related_products;
}

/**
* Display widget settings form
*
* @param array $instance
*
*/
public function form( $instance ) {
$title = ( isset( $instance['title'] ) ) ? $instance['title'] : '';
$num_posts = ( isset( $instance['num_posts'] ) ) ? $instance['num_posts'] : 5;
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
<?php esc_html_e( 'Title:', 'elasticpress' ); ?>
</label>

<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
value="<?php echo esc_attr( $title ); ?>"/>
</p>

<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'num_posts' ) ); ?>">
<?php esc_html_e( 'Number of Posts to Show:', 'elasticpress' ); ?>
</label>

<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'num_posts' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'num_posts' ) ); ?>" type="text"
value="<?php echo absint( $num_posts ); ?>"/>
</p>
<?php
}

/**
* Update widget settings
*
* @param array $new_instance
* @param array $old_instance
*
* @since 2.2
* @return array
*/
public function update( $new_instance, $old_instance ) {

$instance = array();
$instance['title'] = isset( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '';
$instance['num_posts'] = isset( $new_instance['num_posts'] ) ? absint( $new_instance['num_posts'] ) : 5;

return $instance;
}
}
55 changes: 55 additions & 0 deletions features/woocommerce/woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,60 @@ function ep_wc_add_order_items_search( $post_args, $post_id ) {
return $post_args;
}

/**
* Register related products widget
*
* @since 2.4
*/
function ep_related_products_register_widget() {
require_once( dirname( __FILE__ ) . '/widget.php' );

register_widget( 'EP_Related_Products_Widget' );
}

/**
* Search Elasticsearch for related product
*
* @param int $product_id
* @param int $return
* @since 2.4
* @return array|bool
*/
function ep_wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array() ) {
$product_id = absint( $product_id );
$exclude_ids = array_merge( array( 0, $product_id ), $exclude_ids );

$cats_array = apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_cat_terms', wc_get_product_term_ids( $product_id, 'product_cat' ), $product_id ) : array();
$tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id ) : array();

$args = array(
'more_like' => $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
*
Expand All @@ -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' );
}
}

Expand Down