Showing posts with label Woo-commerce. Show all posts
Showing posts with label Woo-commerce. Show all posts

Friday, May 24, 2013

Show Average Rating on Product Page in Woocommerce

To display average rating of product on product page , Add below code in theme's functions.php file

add_action('woocommerce_after_shop_loop_item', 'my_print_stars' );


function my_print_stars(){
    global $wpdb;
    global $post;
    $count = $wpdb->get_var("
    SELECT COUNT(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 = $post->ID
    AND comment_approved = '1'
    AND meta_value > 0
");

$rating = $wpdb->get_var("
    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 = $post->ID
    AND comment_approved = '1'
");

if ( $count > 0 ) {

    $average = number_format($rating / $count, 2);

    echo '<div class="starwrapper" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';

    echo '<span class="star-rating" title="'.sprintf(__('Rated %s out of 5', 'woocommerce'), $average).'"><span style="width:'.($average*16).'px"><span itemprop="ratingValue" class="rating">'.$average.'</span> </span></span>';

    echo '</div>';
    }

Woo commerce Clear Cart Functionality on Car Page

Below is the code to Add 'Clear Cart ' functionality in woocommerce

1) Add this in Theme's functions.php file
add_action('init', 'woocommerce_clear_cart_url');

function woocommerce_clear_cart_url() {

 global $woocommerce;

 if( isset($_REQUEST['clear-cart']) ) {

  $woocommerce-> cart->empty_cart();

 }

}
2) Add Below code in plugins/woocommerce/templates/cart.php file

<input class="button" name="clear-cart" type="submit" value="&lt;?php _e('Empty Cart', 'woocommerce'); ?&gt;" />

Note:(You can add this just before below existing code


<input class="button" name="update_cart" type="submit" value="&lt;?php _e('Update Cart', 'woocommerce'); ?&gt;" /></code>