daily pastebin goal
64%
SHARE
TWEET

Marketpress List Products Function

chelle Sep 23rd, 2011 186 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function simplemarket_list_products( $echo = true, $paginate = '', $page = '', $per_page = '', $order_by = '', $order = '', $category = '', $tag = '' ) {
  2.   global $wp_query, $mp;
  3.   $settings = get_option('mp_settings');
  4.  
  5.   //setup taxonomy if applicable
  6.   if ($category) {
  7.     $taxonomy_query = '&product_category=' . sanitize_title($category);
  8.   } else if ($tag) {
  9.     $taxonomy_query = '&product_tag=' . sanitize_title($tag);
  10.   } else if ($wp_query->query_vars['taxonomy'] == 'product_category' || $wp_query->query_vars['taxonomy'] == 'product_tag') {
  11.     $taxonomy_query = '&' . $wp_query->query_vars['taxonomy'] . '=' . get_query_var($wp_query->query_vars['taxonomy']);
  12.   }
  13.  
  14.   //setup pagination
  15.   $paged = false;
  16.   if ($paginate) {
  17.     $paged = true;
  18.   } else if ($paginate === '') {
  19.     if ($settings['paginate'])
  20.       $paged = true;
  21.     else
  22.       $paginate_query = '&nopaging=true';
  23.   } else {
  24.     $paginate_query = '&nopaging=true';
  25.   }
  26.  
  27.   //get page details
  28.   if ($paged) {
  29.     //figure out perpage
  30.     if (intval($per_page)) {
  31.       $paginate_query = '&posts_per_page='.intval($per_page);
  32.     } else {
  33.       $paginate_query = '&posts_per_page='.$settings['per_page'];
  34.                 }
  35.  
  36.     //figure out page
  37.     if ($wp_query->query_vars['paged'])
  38.       $paginate_query .= '&paged='.intval($wp_query->query_vars['paged']);
  39.  
  40.     if (intval($page))
  41.       $paginate_query .= '&paged='.intval($page);
  42.     else if ($wp_query->query_vars['paged'])
  43.       $paginate_query .= '&paged='.intval($wp_query->query_vars['paged']);
  44.   }
  45.  
  46.   //get order by
  47.   if (!$order_by) {
  48.     if ($settings['order_by'] == 'price')
  49.       $order_by_query = '&meta_key=mp_price_sort&orderby=meta_value_num';
  50.     else if ($settings['order_by'] == 'sales')
  51.       $order_by_query = '&meta_key=mp_sales_count&orderby=meta_value_num';
  52.     else
  53.       $order_by_query = '&orderby='.$settings['order_by'];
  54.   } else {
  55.         if ('price' == $order_by)
  56.                 $order_by_query = '&meta_key=mp_price_sort&orderby=meta_value_num';
  57.     else
  58.         $order_by_query = '&orderby='.$order_by;
  59.   }
  60.  
  61.   //get order direction
  62.   if (!$order) {
  63.     $order_query = '&order='.$settings['order'];
  64.   } else {
  65.     $order_query = '&order='.$order;
  66.   }
  67.  
  68.   //The Query
  69.   $custom_query = new WP_Query('post_type=product&post_status=publish' . $taxonomy_query . $paginate_query . $order_by_query . $order_query);
  70.  
  71.   //allows pagination links to work get_posts_nav_link()
  72.   if ($wp_query->max_num_pages == 0 || $taxonomy_query)
  73.     $wp_query->max_num_pages = $custom_query->max_num_pages;
  74.  
  75.   $content = '<div id="mp_product_list">';
  76.  
  77.   if ($last = count($custom_query->posts)) {
  78.     $count = 1;
  79.     foreach ($custom_query->posts as $post) {
  80.  
  81.                         //add last css class for styling grids
  82.                         if ($count == $last)
  83.                           $class = array('mp_product', 'last-product');
  84.                         else
  85.                           $class = 'mp_product';
  86.  
  87.       $content .= '<div '.mp_product_class(false, $class, $post->ID).'>';
  88.       $content .= '<h3 class="mp_product_name"><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></h3>';
  89.       $content .= '<div class="mp_product_content">';
  90.       $product_content = mp_product_image( false, 'list', $post->ID );
  91.       $product_content .= $mp->product_excerpt($post->post_excerpt, $post->post_content, $post->ID);
  92.       $content .= apply_filters( 'mp_product_list_content', $product_content, $post->ID );
  93.       $content .= '</div>';
  94.  
  95.       $content .= '<div class="mp_product_meta">';
  96.       //price
  97.       $meta = mp_product_price($post->ID);
  98.       //button
  99.       $meta .= mp_buy_button(false, 'list', $post->ID);
  100.       $content .= apply_filters( 'mp_product_list_meta', $meta, $post->ID );
  101.       $content .= '</div>';
  102.  
  103.       $content .= '</div>';
  104.  
  105.       $count++;
  106.     }
  107.   } else {
  108.     $content .= '<div id="mp_no_products">' . apply_filters( 'mp_product_list_none', __('No Products', 'mp') ) . '</div>';
  109.   }
  110.  
  111.   $content .= '</div>';
  112.  
  113.   if ($echo)
  114.     echo $content;
  115.   else
  116.     return $content;
  117. }
RAW Paste Data
Top