Ratings in loops for sort by rating

This commit is contained in:
Mike Jolley 2013-01-12 10:53:24 +00:00
parent 0caca2d401
commit 261f413405
9 changed files with 130 additions and 61 deletions

File diff suppressed because one or more lines are too long

View File

@ -800,7 +800,7 @@ p.demo_store {
} }
} }
/* Start rating */ /* Star rating */
.star-rating { .star-rating {
float: right; float: right;
width: 80px; width: 80px;
@ -815,6 +815,14 @@ p.demo_store {
} }
} }
.products {
.star-rating {
display: block;
margin: 0 0 .5em;
float: none;
}
}
.hreview-aggregate { .hreview-aggregate {
.star-rating { .star-rating {
margin: 10px 0 0 0; margin: 10px 0 0 0;

View File

@ -879,25 +879,51 @@ abstract class WC_Product {
return '<del>' . ( ( is_numeric( $from ) ) ? woocommerce_price( $from ) : $from ) . '</del> <ins>' . ( ( is_numeric( $to ) ) ? woocommerce_price( $to ) : $to ) . '</ins>'; return '<del>' . ( ( is_numeric( $from ) ) ? woocommerce_price( $from ) : $from ) . '</del> <ins>' . ( ( is_numeric( $to ) ) ? woocommerce_price( $to ) : $to ) . '</ins>';
} }
/** /**
* Returns the product rating in html format - ratings are stored in transient cache. * get_average_rating function.
* *
* @access public * @access public
* @param string $location (default: '')
* @return void * @return void
*/ */
function get_rating_html( $location = '' ) { function get_average_rating() {
if ( $location )
$location = '_' . $location;
$star_size = apply_filters( 'woocommerce_star_rating_size' . $location, 16 );
if ( false === ( $average_rating = get_transient( 'wc_average_rating_' . $this->id ) ) ) { if ( false === ( $average_rating = get_transient( 'wc_average_rating_' . $this->id ) ) ) {
global $wpdb; global $wpdb;
$average_rating = '';
$count = $this->get_rating_count();
if ( $count > 0 ) {
$ratings = $wpdb->get_var( $wpdb->prepare("
SELECT SUM(meta_value) FROM $wpdb->commentmeta
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
WHERE meta_key = 'rating'
AND comment_post_ID = %d
AND comment_approved = '1'
", $this->id ) );
$average_rating = number_format( $ratings / $count, 2 );
}
set_transient( 'wc_average_rating_' . $this->id, $average_rating );
}
return $average_rating;
}
/**
* get_rating_count function.
*
* @access public
* @return void
*/
function get_rating_count() {
if ( false === ( $count = get_transient( 'wc_rating_count_' . $this->id ) ) ) {
global $wpdb;
$count = $wpdb->get_var( $wpdb->prepare(" $count = $wpdb->get_var( $wpdb->prepare("
SELECT COUNT(meta_value) FROM $wpdb->commentmeta SELECT COUNT(meta_value) FROM $wpdb->commentmeta
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
@ -907,26 +933,40 @@ abstract class WC_Product {
AND meta_value > 0 AND meta_value > 0
", $this->id ) ); ", $this->id ) );
$ratings = $wpdb->get_var( $wpdb->prepare(" set_transient( 'wc_rating_count_' . $this->id, $count );
SELECT SUM(meta_value) FROM $wpdb->commentmeta
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
WHERE meta_key = 'rating'
AND comment_post_ID = %d
AND comment_approved = '1'
", $this->id ) );
if ( $count > 0 )
$average_rating = number_format($ratings / $count, 2);
else
$average_rating = '';
set_transient( 'wc_average_rating_' . $this->id, $average_rating );
} }
if ( $average_rating > 0 ) return $count;
return '<div class="star-rating" title="' . sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $average_rating ) . '"><span style="width:' . ( $average_rating * $star_size ) . 'px"><span class="rating">' . $average_rating . '</span> ' . __( 'out of 5', 'woocommerce' ) . '</span></div>'; }
else
return ''; /**
* Returns the product rating in html format - ratings are stored in transient cache.
*
* @access public
* @param string $location (default: '')
* @return void
*/
function get_rating_html( $location = '' ) {
$average_rating = $this->get_average_rating();
if ( $average_rating > 0 ) {
if ( $location )
$location = '_' . $location;
$star_size = apply_filters( 'woocommerce_star_rating_size' . $location, 16 );
$average_rating = $this->get_average_rating();
$rating_html = '<div class="star-rating" title="' . sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $average_rating ) . '">';
$rating_html .= '<span style="width:' . ( $average_rating * $star_size ) . 'px"><span class="rating">' . $average_rating . '</span> ' . __( 'out of 5', 'woocommerce' ) . '</span>';
$rating_html .= '</div>';
return $rating_html;
}
} }

View File

@ -12,6 +12,6 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product; global $product;
?> ?>
<?php if ($price_html = $product->get_price_html()) : ?> <?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span> <span class="price"><?php echo $price_html; ?></span>
<?php endif; ?> <?php endif; ?>

20
templates/loop/rating.php Normal file
View File

@ -0,0 +1,20 @@
<?php
/**
* Loop Rating
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.0.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product;
if ( get_option( 'woocommerce_enable_review_rating' ) == 'no' )
return;
?>
<?php if ( $rating_html = $product->get_rating_html() ) : ?>
<?php echo $rating_html; ?>
<?php endif; ?>

View File

@ -183,21 +183,6 @@ function woocommerce_nav_menu_items( $items, $args ) {
return $items; return $items;
} }
/**
* Update catalog ordering if posted.
*
* @access public
* @return void
*/
function woocommerce_update_catalog_ordering() {
global $woocommerce;
if ( isset( $_REQUEST['sort'] ) && $_REQUEST['sort'] != '' )
$woocommerce->session->orderby = esc_attr( $_REQUEST['sort'] );
}
/** /**
* Remove from cart/update. * Remove from cart/update.
* *
@ -1252,12 +1237,16 @@ function woocommerce_products_rss_feed() {
* @return void * @return void
*/ */
function woocommerce_add_comment_rating($comment_id) { function woocommerce_add_comment_rating($comment_id) {
if ( isset($_POST['rating']) ) : if ( isset( $_POST['rating'] ) ) {
global $post; global $post;
if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 ) return;
add_comment_meta( $comment_id, 'rating', (int) esc_attr($_POST['rating']), true ); if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 )
return;
add_comment_meta( $comment_id, 'rating', (int) esc_attr( $_POST['rating'] ), true );
delete_transient( 'wc_average_rating_' . esc_attr($post->ID) ); delete_transient( 'wc_average_rating_' . esc_attr($post->ID) );
endif; }
} }

View File

@ -79,6 +79,7 @@ if ( ! is_admin() || defined('DOING_AJAX') ) {
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 ); add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
/** /**
* Subcategories * Subcategories
@ -241,13 +242,6 @@ add_action( 'get_header', 'woocommerce_clear_cart_after_payment' );
*/ */
add_filter( 'show_admin_bar', 'woocommerce_disable_admin_bar', 10, 1 ); add_filter( 'show_admin_bar', 'woocommerce_disable_admin_bar', 10, 1 );
/**
* Catalog sorting/ordering
*
* @see woocommerce_update_catalog_ordering()
*/
add_action( 'init', 'woocommerce_update_catalog_ordering' );
/** /**
* Cart Actions * Cart Actions
* *

View File

@ -352,6 +352,19 @@ if ( ! function_exists( 'woocommerce_template_loop_price' ) ) {
woocommerce_get_template( 'loop/price.php' ); woocommerce_get_template( 'loop/price.php' );
} }
} }
if ( ! function_exists( 'woocommerce_template_loop_rating' ) ) {
/**
* Display the average rating in the loop
*
* @access public
* @subpackage Loop
* @return void
*/
function woocommerce_template_loop_rating() {
woocommerce_get_template( 'loop/rating.php' );
}
}
if ( ! function_exists( 'woocommerce_show_product_loop_sale_flash' ) ) { if ( ! function_exists( 'woocommerce_show_product_loop_sale_flash' ) ) {
/** /**
@ -432,10 +445,9 @@ if ( ! function_exists( 'woocommerce_catalog_ordering' ) ) {
function woocommerce_catalog_ordering() { function woocommerce_catalog_ordering() {
global $woocommerce; global $woocommerce;
if ( ! isset( $woocommerce->session->orderby ) ) $orderby = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
$woocommerce->session->orderby = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
woocommerce_get_template( 'loop/sorting.php' ); woocommerce_get_template( 'loop/sorting.php', array( 'orderby' => $orderby ) );
} }
} }

View File

@ -832,7 +832,12 @@ class Woocommerce {
'delete_terms' => 'delete_product_terms', 'delete_terms' => 'delete_product_terms',
'assign_terms' => 'assign_product_terms', 'assign_terms' => 'assign_product_terms',
), ),
'rewrite' => array( 'slug' => $product_category_slug, 'with_front' => false, 'hierarchical' => true ), 'rewrite' => array(
'slug' => $product_category_slug,
'with_front' => false,
'hierarchical' => true,
//'ep_mask' => EP_CATEGORIES
),
) )
); );
@ -1851,6 +1856,7 @@ class Woocommerce {
'wc_product_children_ids_', 'wc_product_children_ids_',
'wc_product_total_stock_', 'wc_product_total_stock_',
'wc_average_rating_', 'wc_average_rating_',
'wc_rating_count_',
'woocommerce_product_type_', // No longer used 'woocommerce_product_type_', // No longer used
'wc_product_type_', // No longer used 'wc_product_type_', // No longer used
); );