Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!function_exists('mp_related_products')) :
- /**
- * Displays related products for the passed product id
- *
- * @param int $product_id.
- * @param bool $in_same_category Optional, whether to limit related to the same category.
- * @param bool $echo. Optional, whether to echo or return the results
- * @param int $limit. Optional The number of products we want to retrieve.
- * @param bool $simple_list Optional, whether to show the related products based on the "list_view" setting or as a simple unordered list
- * @param bool $in_same_tags Optional, whether to limit related to same tags
- */
- function mp_related_products() {
- global $mp, $post;
- $output = '';
- $categories = $tag_list = array();
- if( $mp->get_setting('related_products->show') == 0)
- return '';
- $defaults = array_merge($mp->defaults['related_products'], array(
- 'simple_list' => $mp->get_setting('related_products->simple_list'),
- 'relate_by' => $mp->get_setting('related_products->relate_by'),
- 'limit' => $mp->get_setting('related_products->show_limit'),
- ));
- $args = $mp->parse_args(func_get_args(), $defaults);
- if( !is_null($args['product_id']) ) {
- $args['product_id'] = ( isset($post) && $post->post_type == 'product' ) ? $post->ID : false;
- $product_details = get_post($args['product_id']);
- }else{
- $product_details = get_post($args['product_id']);
- $args['product_id'] = ( $product_details->post_type == 'product' ) ? $post->ID : false;
- }
- if( is_null($product_details) )
- return '';
- //setup the default args
- $query_args = array(
- 'orderby' => 'title',
- 'order' => 'ASC',
- 'post_type' => 'product',
- 'posts_per_page' => intval($args['limit']),
- 'post__not_in' => array($args['product_id']),
- 'tax_query' => array(), //we'll add these later
- );
- //get the tags for this product
- if ( 'both' == $args['relate_by'] || 'tags' == $args['relate_by'] ) {
- $tags = get_the_terms( $args['product_id'], 'product_tag');
- if ( is_array($tags) ) {
- foreach($tags as $tag) {
- $tag_list[] = $tag->term_id;
- }
- //add the tag taxonomy query
- $query_args['tax_query'][] = array(
- 'taxonomy' => 'product_tag',
- 'field' => 'id',
- 'terms' => $tag_list,
- 'operator' => 'IN'
- );
- }
- }
- //are we limiting to only the assigned categories
- if( 'both' == $args['relate_by'] || 'category' == $args['relate_by'] ) {
- $product_cats = get_the_terms( $args['product_id'], 'product_category' );
- if( is_array($product_cats) ) {
- foreach($product_cats as $cat) {
- $categories[] = $cat->term_id;
- }
- $query_args['tax_query'][] = array(
- 'taxonomy' => 'product_category',
- 'field' => 'id',
- 'terms' => $categories,
- 'operator' => 'IN'
- );
- }
- }
- //we only want to run the query if we have categories or tags to look for.
- if ( count($tag_list) > 0 || count($categories) > 0 ) {
- //make the query
- $related_query = new WP_Query($query_args);
- //how are we formatting the output
- if( $args['simple_list'] ) {
- $output = '<div id="mp_related_products">';
- $output .= '<div class="mp_related_products_title"><h4>' . apply_filters( 'mp_related_products_title', __('Related Products','mp') ) . '</h4></div>';
- if( $related_query->post_count ) {
- $list = '<ul class="mp_related_products_list">%s</ul>';
- $items = '';
- foreach($related_query->posts as $product) {
- $items .= '<li class="mp_related_products_list_item"><a href="'.get_permalink($product->ID).'">'.$product->post_title.'</a></li>';
- }
- $output .= sprintf($list, $items);
- }else{
- $output .= '<div class="mp_related_products_title"><h4>'. apply_filters( 'mp_related_products_title_none', __('No Related Products','mp') ) . '</h4></div>';
- }
- $output .= '</div>';
- } else {
- //we'll use the $mp settings and functions
- $layout_type = $mp->get_setting('list_view');
- $output = '<div id="mp_related_products" class="mp_' . $layout_type . '">';
- //do we have posts?
- if( $related_query->post_count ) {
- $output .= '<div class="mp_related_products_title"><h4>' . apply_filters( 'mp_related_products_title', __('Related Products','mp') ) . '</h4></div>';
- $output .= $layout_type == 'grid' ? _mp_products_html_grid($related_query) : _mp_products_html_list($related_query);
- }else{
- $output .= '<div class="mp_related_products_title"><h4>'. apply_filters( 'mp_related_products_title_none', __('No Related Products','mp') ) . '</h4></div>';
- }
- $output .= '</div>';
- }
- }
- $output = apply_filters('mp_related_products', $output, $args);
- //how are we sending back the data
- if($args['echo']) {
- echo $output;
- }else{
- return $output;
- }
- }
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement