Created
June 28, 2012 09:31
-
-
Save anonymous/3010204 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* function basic_list_products | |
* Displays a list of products according to preference. Optional values default to the values in Presentation Settings -> Product List | |
* | |
* @param bool $echo Optional, whether to echo or return | |
* @param bool $paginate Optional, whether to paginate | |
* @param int $page Optional, The page number to display in the product list if $paginate is set to true. | |
* @param int $per_page Optional, How many products to display in the product list if $paginate is set to true. | |
* @param string $order_by Optional, What field to order products by. Can be: title, date, ID, author, price, sales, rand | |
* @param string $order Optional, Direction to order products by. Can be: DESC, ASC | |
* @param string $category Optional, limit to a product category | |
* @param string $tag Optional, limit to a product tag | |
*/ | |
function basic_list_products( $echo = true, $paginate = '', $page = '', $per_page = '', $order_by = '', $order = '', $category = '', $tag = '' ) { | |
global $wp_query, $mp; | |
$settings = get_option('mp_settings'); | |
//setup taxonomy if applicable | |
if ($category) { | |
$taxonomy_query = '&product_category=' . sanitize_title($category); | |
} else if ($tag) { | |
$taxonomy_query = '&product_tag=' . sanitize_title($tag); | |
} else if ($wp_query->query_vars['taxonomy'] == 'product_category' || $wp_query->query_vars['taxonomy'] == 'product_tag') { | |
$taxonomy_query = '&' . $wp_query->query_vars['taxonomy'] . '=' . get_query_var($wp_query->query_vars['taxonomy']); | |
} | |
//setup pagination | |
$paged = false; | |
if ($paginate) { | |
$paged = true; | |
} else if ($paginate === '') { | |
if ($settings['paginate']) | |
$paged = true; | |
else | |
$paginate_query = '&nopaging=true'; | |
} else { | |
$paginate_query = '&nopaging=true'; | |
} | |
//get page details | |
if ($paged) { | |
//figure out perpage | |
if (intval($per_page)) { | |
$paginate_query = '&posts_per_page='.intval($per_page); | |
} else { | |
$paginate_query = '&posts_per_page='.$settings['per_page']; | |
} | |
//figure out page | |
if ($wp_query->query_vars['paged']) | |
$paginate_query .= '&paged='.intval($wp_query->query_vars['paged']); | |
if (intval($page)) | |
$paginate_query .= '&paged='.intval($page); | |
else if ($wp_query->query_vars['paged']) | |
$paginate_query .= '&paged='.intval($wp_query->query_vars['paged']); | |
} | |
//get order by | |
if (!$order_by) { | |
if ($settings['order_by'] == 'price') | |
$order_by_query = '&meta_key=mp_price_sort&orderby=meta_value_num'; | |
else if ($settings['order_by'] == 'sales') | |
$order_by_query = '&meta_key=mp_sales_count&orderby=meta_value_num'; | |
else | |
$order_by_query = '&orderby='.$settings['order_by']; | |
} else { | |
if ('price' == $order_by) | |
$order_by_query = '&meta_key=mp_price_sort&orderby=meta_value_num'; | |
else | |
$order_by_query = '&orderby='.$order_by; | |
} | |
//get order direction | |
if (!$order) { | |
$order_query = '&order='.$settings['order']; | |
} else { | |
$order_query = '&order='.$order; | |
} | |
//The Query | |
$custom_query = new WP_Query('post_type=product&post_status=publish' . $taxonomy_query . $paginate_query . $order_by_query . $order_query); | |
//allows pagination links to work get_posts_nav_link() | |
if ($wp_query->max_num_pages == 0 || $taxonomy_query) | |
$wp_query->max_num_pages = $custom_query->max_num_pages; | |
$content = '<div id="product_list">'; | |
if ($last = $custom_query->post_count) { | |
$count = 1; | |
foreach ($custom_query->posts as $post) { | |
if ($settings['list_view'] == 'grid') | |
$class = 'product grid'; | |
else $class='product list'; | |
if ($settings['list_view'] == 'grid') | |
if(strlen($post->post_title)>30){ | |
$product_title = mb_substr($post->post_title,0,30,'UTF-8').'...'; | |
} | |
else{ | |
$product_title = $post->post_title; | |
} | |
else $product_title = $post->post_title; | |
$content .= '<div '.basic_product_class(false, $class, $post->ID).'>'; | |
$content .= '<h3 class="product_name"><a href="' . get_permalink( $post->ID ) . '">' . $product_title . '</a></h3>'; | |
$product_content = basic_product_image( false, 'list', $post->ID ); | |
if ($settings['show_excerpt'] == 1) | |
if ($settings['list_view'] == 'list') | |
$product_content .= $mp->product_excerpt($post->post_excerpt, $post->post_content, $post->ID); | |
$content .= apply_filters( 'mp_product_list_content', $product_content, $post->ID ); | |
$content .= '<div class="product_meta">'; | |
//price | |
$meta = basic_product_price(false, $post->ID); | |
//button | |
$meta .= basic_buy_button(false, 'list', $post->ID); | |
$content .= apply_filters( 'mp_product_list_meta', $meta, $post->ID ); | |
$content .= '</div>'; | |
$content .= '</div>'; | |
$count++; | |
} | |
} else { | |
$content .= '<div id="no_products">' . apply_filters( 'mp_product_list_none', __('No Products', 'basic') ) . '</div>'; | |
} | |
$content .= '</div>'; | |
if ( $wp_query->max_num_pages > 1 ) : | |
$content .= '<ul class="pager" style="clear: both; float: none;">'; | |
$content .= '<li class="previous">'.get_previous_posts_link( '<i class="icon-chevron-left"></i> Προηγούμενο' ).'</li>'; | |
$content .= '<li class="next">'.get_next_posts_link( __( 'Επόμενο <i class="icon-chevron-right"></i>') ).'</li>'; | |
$content .= '</ul>'; | |
endif; | |
if ($echo) | |
echo $content; | |
else | |
return $content; | |
} | |
/** | |
* Display the classes for the product div. | |
* | |
* @param bool $echo Whether to echo class. | |
* @param string|array $class One or more classes to add to the class list. | |
* @param int $post_id The post_id for the product. Optional if in the loop | |
*/ | |
function basic_product_class( $echo = true, $class = '', $post_id = null ) { | |
// Separates classes with a single space, collates classes for post DIV | |
$content = 'class="' . join( ' ', basic_get_product_class( $class, $post_id ) ) . '"'; | |
if ($echo) | |
echo $content; | |
else | |
return $content; | |
} | |
/** | |
* Retrieve the list of classes for the product as an array. | |
* | |
* The class names are add are many. If the post is a sticky, then the 'sticky' | |
* class name. The class 'hentry' is always added to each post. For each | |
* category, the class will be added with 'category-' with category slug is | |
* added. The tags are the same way as the categories with 'tag-' before the tag | |
* slug. All classes are passed through the filter, 'post_class' with the list | |
* of classes, followed by $class parameter value, with the post ID as the last | |
* parameter. | |
* | |
* | |
* @param string|array $class One or more classes to add to the class list. | |
* @param int $post_id The post_id for the product. Optional if in the loop | |
* @return array Array of classes. | |
*/ | |
function basic_get_product_class( $class = '', $post_id = null ) { | |
global $id; | |
$post_id = ( NULL === $post_id ) ? $id : $post_id; | |
$post = get_post($post_id); | |
$classes = array(); | |
if ( empty($post) ) | |
return $classes; | |
$classes[] = 'product-' . $post->ID; | |
$classes[] = $post->post_type; | |
// sticky for Sticky Posts | |
if ( is_sticky($post->ID)) | |
$classes[] = 'sticky'; | |
// hentry for hAtom compliace | |
$classes[] = 'hentry'; | |
// Categories | |
$categories = get_the_terms($post->ID, "product_category"); | |
foreach ( (array) $categories as $cat ) { | |
if ( empty($cat->slug ) ) | |
continue; | |
$classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->cat_ID); | |
} | |
// Tags | |
$tags = get_the_terms($post->ID, "product_tag"); | |
foreach ( (array) $tags as $tag ) { | |
if ( empty($tag->slug ) ) | |
continue; | |
$classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id); | |
} | |
if ( !empty($class) ) { | |
if ( !is_array( $class ) ) | |
$class = preg_split('#\s+#', $class); | |
$classes = array_merge($classes, $class); | |
} | |
$classes = array_map('esc_attr', $classes); | |
return $classes; | |
} | |
/* | |
* Displays the product featured image | |
* | |
* @param bool $echo Optional, whether to echo | |
* @param string $context Options are list, single, or widget | |
* @param int $post_id The post_id for the product. Optional if in the loop | |
* @param int $size An optional width/height for the image if contect is widget | |
*/ | |
function basic_product_image( $echo = true, $context = 'list', $post_id = NULL, $size = NULL ) { | |
global $id; | |
$post_id = ( NULL === $post_id ) ? $id : $post_id; | |
// Added WPML | |
$post_id = apply_filters('mp_product_image_id', $post_id); | |
$post = get_post($post_id); | |
$settings = get_option('mp_settings'); | |
$post_thumbnail_id = get_post_thumbnail_id( $post_id ); | |
if ($context == 'list') { | |
//quit if no thumbnails on listings | |
if (!$settings['show_thumbnail']) | |
return ''; | |
//size | |
$size = array(160, 120); | |
//link | |
$link = get_permalink($post_id); | |
$title = esc_attr($post->post_title); | |
} else if ($context == 'single') { | |
//size | |
if ($settings['product_img_size'] == 'custom') | |
$size = array($settings['product_img_width'], $settings['product_img_height']); | |
else | |
$size = $settings['product_img_size']; | |
//link | |
$temp = wp_get_attachment_image_src( $post_thumbnail_id, 'large' ); | |
$link = $temp[0]; | |
$title = __('View Larger Image »', 'mp'); | |
$class = ' class="mp_product_image_link mp_lightbox" rel="lightbox"'; | |
} else if ($context == 'widget') { | |
//size | |
if (intval($size)) | |
$size = array(intval($size), intval($size)); | |
else | |
$size = array(50, 50); | |
//link | |
$link = get_permalink($post_id); | |
$title = esc_attr($post->post_title); | |
} | |
$image = get_the_post_thumbnail($post_id, $size, array('class' => 'alignleft product_image_'.$context, 'title' => $title)); | |
//add the link | |
if ($link) | |
$image = '<div class="image-wrapper"><a id="product_image-' . $post_id . '"' . $class . ' href="' . $link . '">' . $image . '</a></div>'; | |
if ($echo) | |
echo $image; | |
else | |
return $image; | |
} | |
/* | |
* Displays the product price (and sale price) | |
* | |
* @param bool $echo Optional, whether to echo | |
* @param int $post_id The post_id for the product. Optional if in the loop | |
* @param sting $label A label to prepend to the price. Defaults to "Price: " | |
*/ | |
function basic_product_price( $echo = true, $post_id = NULL, $label = true ) { | |
global $id, $mp; | |
$post_id = ( NULL === $post_id ) ? $id : $post_id; | |
$label = ($label === true) ? __('', 'mp') : $label; | |
$settings = get_option('mp_settings'); | |
$meta = get_post_custom($post_id); | |
//unserialize | |
foreach ($meta as $key => $val) { | |
$meta[$key] = maybe_unserialize($val[0]); | |
if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link" && $key != "mp_file" && $key != "mp_price_sort") | |
$meta[$key] = array($meta[$key]); | |
} | |
if ((is_array($meta["mp_price"]) && count($meta["mp_price"]) >= 1) || !empty($meta["mp_file"])) { | |
if ($meta["mp_is_sale"]) { | |
$price = '<span class="mp_special_price badge badge-important"><del class="mp_old_price">'.$mp->format_currency('', $meta["mp_price"][0]).'</del>'; | |
$price .= '<span class="mp_current_price">'.$mp->format_currency('', $meta["mp_sale_price"][0]).'</span></span>'; | |
} else { | |
$price = '<span class="mp_normal_price"><span class="mp_current_price badge badge-important">'.$mp->format_currency('', $meta["mp_price"][0]).'</span></span>'; | |
} | |
} else { | |
return ''; | |
} | |
$price = apply_filters( 'mp_product_price_tag', '<span class="mp_product_price">' . $label . $price . '</span>', $post_id, $label ); | |
if ($echo) | |
echo $price; | |
else | |
return $price; | |
} | |
/* | |
* Displays the buy or add to cart button | |
* | |
* @param bool $echo Optional, whether to echo | |
* @param string $context Options are list or single | |
* @param int $post_id The post_id for the product. Optional if in the loop | |
*/ | |
function basic_buy_button( $echo = true, $context = 'list', $post_id = NULL ) { | |
global $id, $mp; | |
$post_id = ( NULL === $post_id ) ? $id : $post_id; | |
$settings = get_option('mp_settings'); | |
$meta = get_post_custom($post_id); | |
//unserialize | |
foreach ($meta as $key => $val) { | |
$meta[$key] = maybe_unserialize($val[0]); | |
if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link" && $key != "mp_file") | |
$meta[$key] = array($meta[$key]); | |
} | |
//check stock | |
$no_inventory = array(); | |
$all_out = false; | |
if ($meta['mp_track_inventory']) { | |
$cart = $mp->get_cart_contents(); | |
if (is_array($cart[$post_id])) { | |
foreach ($cart[$post_id] as $variation => $data) { | |
if ($meta['mp_inventory'][$variation] <= $data['quantity']) | |
$no_inventory[] = $variation; | |
} | |
foreach ($meta['mp_inventory'] as $key => $stock) { | |
if (!in_array($key, $no_inventory) && $stock <= 0) | |
$no_inventory[] = $key; | |
} | |
} | |
//find out of stock items that aren't in the cart | |
foreach ($meta['mp_inventory'] as $key => $stock) { | |
if (!in_array($key, $no_inventory) && $stock <= 0) | |
$no_inventory[] = $key; | |
} | |
if (count($no_inventory) >= count($meta["mp_price"])) | |
$all_out = true; | |
} | |
//display an external link or form button | |
if ($product_link = $meta['mp_product_link']) { | |
$button = '<a class="mp_link_buynow btn btn-primary" href="' . esc_url($product_link) . '">' . __('Buy Now »', 'mp') . '</a>'; | |
} else if ($settings['disable_cart']) { | |
$button = ''; | |
} else { | |
$button = '<form class="mp_buy_form" method="post" action="' . basic_cart_link(false, true) . '">'; | |
if ($all_out) { | |
$button .= '<span class="mp_no_stock">' . __('Out of Stock', 'mp') . '</span>'; | |
} else { | |
$button .= '<input type="hidden" name="product_id" value="' . $post_id . '" />'; | |
//create select list if more than one variation | |
if (is_array($meta["mp_price"]) && count($meta["mp_price"]) > 1 && empty($meta["mp_file"])) { | |
$variation_select = '<select class="mp_product_variations" name="variation">'; | |
foreach ($meta["mp_price"] as $key => $value) { | |
$disabled = (in_array($key, $no_inventory)) ? ' disabled="disabled"' : ''; | |
$variation_select .= '<option value="' . $key . '"' . $disabled . '>' . esc_html($meta["mp_var_name"][$key]) . ' - '; | |
if ($meta["mp_is_sale"] && $meta["mp_sale_price"][$key]) { | |
$variation_select .= $mp->format_currency('', $meta["mp_sale_price"][$key]); | |
} else { | |
$variation_select .= $mp->format_currency('', $value); | |
} | |
$variation_select .= "</option>\n"; | |
} | |
$variation_select .= "</select> \n"; | |
} else { | |
$button .= '<input type="hidden" name="variation" value="0" />'; | |
} | |
if ($context == 'list') { | |
if ($variation_select) { | |
$button .= '<a class="mp_link_buynow btn btn-primary" href="' . get_permalink($post_id) . '">' . __('Choose Option »', 'mp') . '</a>'; | |
} else if ($settings['list_button_type'] == 'addcart') { | |
$button .= '<input type="hidden" name="action" value="mp-update-cart" />'; | |
$button .= '<input class="mp_button_addcart btn btn-primary" type="submit" name="addcart" value="' . __('Add To Cart »', 'mp') . '" />'; | |
} else if ($settings['list_button_type'] == 'buynow') { | |
$button .= '<input class="mp_button_buynow btn btn-primary" type="submit" name="buynow" value="' . __('Buy Now »', 'mp') . '" />'; | |
} | |
} else { | |
$button .= $variation_select; | |
//add quantity field if not downloadable | |
if ($settings['show_quantity'] && empty($meta["mp_file"])) { | |
$button .= '<span class="mp_quantity"><label>' . __('Quantity:', 'mp') . ' <input class="mp_quantity_field" type="text" size="1" name="quantity" value="1" /></label></span> '; | |
} | |
if ($settings['product_button_type'] == 'addcart') { | |
$button .= '<input type="hidden" name="action" value="mp-update-cart" />'; | |
$button .= '<input class="mp_button_addcart btn btn-primary" type="submit" name="addcart" value="' . __('Add To Cart »', 'mp') . '" />'; | |
} else if ($settings['product_button_type'] == 'buynow') { | |
$button .= '<input class="mp_button_buynow btn btn-primary" type="submit" name="buynow" value="' . __('Buy Now »', 'mp') . '" />'; | |
} | |
} | |
} | |
$button .= '</form>'; | |
} | |
$button = apply_filters( 'mp_buy_button_tag', $button, $post_id, $context ); | |
if ($echo) | |
echo $button; | |
else | |
return $button; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment