Advertisement
Guest User

Modified mp_related_products function

a guest
Aug 19th, 2015
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.64 KB | None | 0 0
  1. <?php
  2.  
  3. if (!function_exists('mp_related_products')) :
  4. /**
  5.  * Displays related products for the passed product id
  6.  *
  7.  * @param int $product_id.
  8.  * @param bool $in_same_category Optional, whether to limit related to the same category.
  9.  * @param bool $echo. Optional, whether to echo or return the results
  10.  * @param int $limit. Optional The number of products we want to retrieve.
  11.  * @param bool $simple_list Optional, whether to show the related products based on the "list_view" setting or as a simple unordered list
  12.  * @param bool $in_same_tags Optional, whether to limit related to same tags
  13.  */
  14. function mp_related_products() {
  15.     global $mp, $post;
  16.  
  17.     $output = '';
  18.     $categories = $tag_list = array();
  19.  
  20.     if( $mp->get_setting('related_products->show') == 0)
  21.         return '';
  22.  
  23.     $defaults = array_merge($mp->defaults['related_products'], array(
  24.         'simple_list' => $mp->get_setting('related_products->simple_list'),
  25.         'relate_by' => $mp->get_setting('related_products->relate_by'),
  26.         'limit' => $mp->get_setting('related_products->show_limit'),
  27.     ));
  28.  
  29.     $args = $mp->parse_args(func_get_args(), $defaults);
  30.  
  31.     if( !is_null($args['product_id']) ) {
  32.         $args['product_id'] = ( isset($post) && $post->post_type == 'product' ) ? $post->ID : false;
  33.         $product_details = get_post($args['product_id']);
  34.     }else{
  35.         $product_details = get_post($args['product_id']);
  36.         $args['product_id'] = ( $product_details->post_type == 'product' ) ? $post->ID : false;
  37.     }
  38.  
  39.     if( is_null($product_details) )
  40.         return '';
  41.  
  42.     //setup the default args
  43.     $query_args = array(
  44.         'orderby' => 'title',
  45.         'order'   => 'DESC',
  46.         'post_type'      => 'product',
  47.         'posts_per_page' => intval($args['limit']),
  48.         'post__not_in'   => array($args['product_id']),
  49.         'tax_query'      => array(), //we'll add these later
  50.     );
  51.  
  52.     //get the tags for this product
  53.     if ( 'both' == $args['relate_by'] || 'tags' == $args['relate_by'] ) {
  54.         $tags = get_the_terms( $args['product_id'], 'product_tag');
  55.  
  56.         if ( is_array($tags) ) {
  57.             foreach($tags as $tag) {
  58.                 $tag_list[] = $tag->term_id;
  59.             }
  60.  
  61.             //add the tag taxonomy query
  62.             $query_args['tax_query'][] = array(
  63.                     'taxonomy' => 'product_tag',
  64.                     'field' => 'id',
  65.                     'terms' => $tag_list,
  66.                     'operator' => 'IN'
  67.             );
  68.         }
  69.     }
  70.  
  71.     //are we limiting to only the assigned categories
  72.     if( 'both' == $args['relate_by'] || 'category' == $args['relate_by'] ) {
  73.         $product_cats = get_the_terms( $args['product_id'], 'product_category' );
  74.  
  75.         if( is_array($product_cats) ) {
  76.             foreach($product_cats as $cat) {
  77.                 $categories[] = $cat->term_id;
  78.             }
  79.  
  80.             $query_args['tax_query'][] = array(
  81.                     'taxonomy' => 'product_category',
  82.                     'field' => 'id',
  83.                     'terms' => $categories,
  84.                     'operator' => 'IN'
  85.             );
  86.         }
  87.     }
  88.  
  89.     //we only want to run the query if we have categories or tags to look for.
  90.     if ( count($tag_list) > 0 || count($categories) > 0 ) {
  91.         //make the query
  92.         $related_query = new WP_Query($query_args);
  93.  
  94.         //how are we formatting the output
  95.         if( $args['simple_list'] ) {
  96.  
  97.             $output = '<div id="mp_related_products">';
  98.             $output .= '<div class="mp_related_products_title"><h4>' . apply_filters( 'mp_related_products_title', __('Related Products','mp') ) . '</h4></div>';
  99.             if( $related_query->post_count ) {
  100.                 $list = '<ul class="mp_related_products_list">%s</ul>';
  101.                 $items = '';
  102.                 foreach($related_query->posts as $product) {
  103.                     $items .= '<li class="mp_related_products_list_item"><a href="'.get_permalink($product->ID).'">'.$product->post_title.'</a></li>';
  104.                 }
  105.                 $output .= sprintf($list, $items);
  106.             }else{
  107.                 $output .= '<div class="mp_related_products_title"><h4>'. apply_filters( 'mp_related_products_title_none', __('No Related Products','mp') ) . '</h4></div>';
  108.             }
  109.  
  110.             $output .= '</div>';
  111.         } else {
  112.             //we'll use the $mp settings and functions
  113.             $layout_type = $mp->get_setting('list_view');
  114.             $output = '<div id="mp_related_products" class="mp_' . $layout_type . '">';
  115.             //do we have posts?
  116.             if( $related_query->post_count ) {
  117.                 $output .= '<div class="mp_related_products_title"><h4>' . apply_filters( 'mp_related_products_title', __('Related Products','mp') ) . '</h4></div>';
  118.                 $output .= $layout_type == 'grid' ? _mp_products_html_grid($related_query) : _mp_products_html_list($related_query);
  119.             }else{
  120.                 $output .= '<div class="mp_related_products_title"><h4>'. apply_filters( 'mp_related_products_title_none', __('No Related Products','mp') ) . '</h4></div>';
  121.             }
  122.  
  123.             $output .= '</div>';
  124.         }
  125.     }
  126.  
  127.     $output = apply_filters('mp_related_products', $output, $args);
  128.  
  129.     //how are we sending back the data
  130.     if($args['echo']) {
  131.         echo $output;
  132.     }else{
  133.         return $output;
  134.     }
  135. }
  136. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement