Skip to content

Instantly share code, notes, and snippets.

@JudeRosario
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JudeRosario/2430a65a15998c13afd4 to your computer and use it in GitHub Desktop.
Save JudeRosario/2430a65a15998c13afd4 to your computer and use it in GitHub Desktop.
MarketPress Related Products Sort Logic
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(
'post_type' => 'product',
'order' => 'ASC',
'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;
}
}
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(
'post_type' => 'product',
'order' => 'DESC',
'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;
}
}
@JudeRosario
Copy link
Author

Just copy/paste the code into the functions.php file of your child theme or a site specific plugin if you use one.

WARNING Copy/paste ONLY ONE VERSION ... NOT BOTH. Pick either one ( Ascending or Descending )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment