Rating stars in Virtuemart

Winter sport ecommerce template for Joomla with VirtueMart support
GK User
Fri May 17, 2013 10:54 am
I've checked for solutions on showing rating stars on virtuemart instead of just numbers (4/5 and similar).
Found a solution that pretty much fixes it, but there's one problem.
All products get 5 stars even though they're not rated yet.
Here's the code I've added:
Code: Select all
<?php         
if ($this->showRating) {            
$maxrating = VmConfig::get('vm_maximum_rating_scale',5);                  if (empty($this->rating)) { ?>               
<span class="vote">                  
<span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $this->rating->rating . '/' . $maxrating) ?>" class="vmicon ratingbox" style="display:inline-block;">                     <span class="stars-orange" style="width:<?php echo $ratingwidth;?>%">                     </span>                  </span>                                 
<span class="rating-title"><?php echo JText::_('COM_VIRTUEMART_RATING').' '.JText::_('COM_VIRTUEMART_UNRATED') ?></span>                  </span>                  <?php } else {                  $ratingwidth = ( $this->rating->rating * 100 ) / $maxrating;//I don't use round as percetntage with works perfect, as for me                  ?>                  <span class="vote">                     
<span title="" class="vmicon ratingbox" style="display:inline-block;">                        <span class="stars-orange" style="width:<?php echo $ratingwidth;?>%">                        </span>                     </span>                     
<span class="rating-title"><?php echo JText::_('COM_VIRTUEMART_RATING').' '.round($this->rating->rating, 2) . '/'. $maxrating; ?></span>                  </span>            <?php   } ?>            <?php    } ?>


Can you see what's wrong?
User avatar
Junior Boarder

teitbite
Fri May 17, 2013 11:13 am
Hi

It's very hard to tell, becuase I would need to make some dump on it.

Can You tell me what is the value of $this->rating->rating when there is no rattings yet ?
User avatar
Moderator

GK User
Fri May 17, 2013 11:59 am
What do you mean by value?
How do I find out?
User avatar
Junior Boarder

teitbite
Fri May 17, 2013 12:42 pm
Hi

My simply showing what's inside this value with:

Code: Select all
print $this->rating->rating;
User avatar
Moderator

GK User
Fri May 17, 2013 1:27 pm
I'm sorry, I don't understand.
Got the suggested code from a guy at Virtuemart support.
But he thinks the problem has something to do with the template.
User avatar
Junior Boarder

GK User
Fri May 17, 2013 4:16 pm
This is added in the default.php-file if not obvious :)
User avatar
Junior Boarder

teitbite
Sun May 19, 2013 5:01 pm
Hi

They always say it's because of the template :)

Please send me ftp access, so I'll make a dump and check it.
User avatar
Moderator

teitbite
Tue May 21, 2013 2:59 pm
Hi

It may be difficult to check the problem if I will not be able to see results of dump or reaction to changes, but if FTP is not possible than please send me this file. I'll try to check the code and figure out what can be wrong.
User avatar
Moderator

GK User
Tue May 21, 2013 3:28 pm
You've got mail.
User avatar
Junior Boarder

teitbite
Wed May 22, 2013 9:58 pm
Hi

As I thought it is not possible to answer without making a dump and checking what has been passed in this variables. Becuase I see 2 different ratings values: $this->rating and $this->rating->rating

try add this below the new code:

Code: Select all
print '<pre>';
var_dump( $this->rating );
var_dump( $this->rating->rating );
print '</pre>';


and past the text which appeared on site after adding this.
User avatar
Moderator

GK User
Thu May 23, 2013 10:32 am
If I do right, this is what comes up

Code: Select all
object(stdClass)#328 (10) {
  ["virtuemart_rating_id"]=>
  string(2) "11"
  ["virtuemart_product_id"]=>
  string(3) "502"
  ["rates"]=>
  string(1) "5"
  ["ratingcount"]=>
  string(1) "1"
  ["rating"]=>
  string(3) "5.0"
  ["published"]=>
  string(1) "1"
  ["created_on"]=>
  string(19) "2013-03-30 05:45:56"
  ["created_by"]=>
  string(3) "647"
  ["modified_on"]=>
  string(19) "2013-03-30 05:46:04"
  ["modified_by"]=>
  string(3) "647"
}
string(3) "5.0"
User avatar
Junior Boarder

teitbite
Fri May 24, 2013 10:18 am
Hi

Ok this looks correct. And now can You please show me what it gives when there is no ratings for product ?
User avatar
Moderator

GK User
Fri May 24, 2013 2:13 pm
Stars are showing + the text that says no rating has been done

Then:
Code: Select all
NULL
NULL
User avatar
Junior Boarder

teitbite
Sun May 26, 2013 9:11 am
Hi

I'm afraid I cannot guess without ftp acces. Function is constructed the way that no NULL should be visible at all, because with NULL value it should not be entered. Is there a way I can get ftp access to solve it ?
User avatar
Moderator

GK User
Tue May 28, 2013 10:59 am
You've got PM
User avatar
Junior Boarder

teitbite
Thu May 30, 2013 3:03 pm
Hi

I got an access and was trying to check it, but I need a good example first, because any product I'm checking is showing just this text "Betyg: Inte betygsatt ännu". Is it a no rating yet or close to this ? Send me a link to product where You have this issue where not rated products are showing 5 stars.
User avatar
Moderator

GK User
Thu May 30, 2013 3:43 pm
You've got PM again...
User avatar
Junior Boarder

teitbite
Thu May 30, 2013 4:14 pm
Hi

Hehe. It was so hard to locate the issue because there is no issue in this code :)

I've checked this product closely and looks like it has 2 ratings posted. Both 5 stars, so the fact that is showing 5/5 is correct :) When there is no ratings than it's showing a text that no rated yet in Your language.
User avatar
Moderator

GK User
Thu May 30, 2013 4:43 pm
Yes..

Now there's no problem, but I want the stars to work :) (not the numbers x/5)
That code I mentioned above isn't there now as it doesn't work.
User avatar
Junior Boarder

teitbite
Fri May 31, 2013 10:29 am
Hi

Och. So You just wanted to show an image with stars :) I've misunderstood You. If this is what You meant by not working than I agree, there were no code for that at all. I've added a line for the stars images:

Code: Select all
      $maxrating = VmConfig::get('vm_maximum_rating_scale',5);
      $rating = empty($this->rating)? JText::_('COM_VIRTUEMART_RATING').' '.JText::_('COM_VIRTUEMART_UNRATED'):JText::_('COM_VIRTUEMART_RATING') . round($this->rating->rating) . '/'. $maxrating;
      echo   $rating;
      echo '<span class="vmicon vm2-stars'.round($this->rating->rating).'"></span>';
User avatar
Moderator

GK User
Fri May 31, 2013 10:32 am
Fantastic!!! :)
Really appreciate your help.

Is it possible to remove the numbers (x/5)?
Also, to add the stars on category overview?
User avatar
Junior Boarder

teitbite
Fri May 31, 2013 7:50 pm
Hi

x/5 removed. Show me what You have in mind by category overview please. I'll see it the same code will work when I see which layout should it be.
User avatar
Moderator

GK User
Fri May 31, 2013 8:36 pm
Ok. Seems like the work "Rating" also disappeared? If the word and x/5 somehow is the same, please undo the latest change.

When it comes to rating in category I would like something like this
User avatar
Junior Boarder

GK User
Fri May 31, 2013 8:37 pm
This would be one level up from product view...
User avatar
Junior Boarder

teitbite
Sat Jun 01, 2013 5:51 pm
Hi

Sorry, but the same code is not working in category page. I've tried to make my own, but looks like no informations about ratings are loaded with category layout. Anyway I've found a part of the code responsible for ratings by default. Looks like setting this information to Yes should unlock it "pshop_allow_reviews" . I believe it means that reviews should be allowed to all users in order to activate this code.
User avatar
Moderator

GK User
Sun Jun 02, 2013 12:14 am
Ok. Do you want me to change that to see if it works?!
User avatar
Junior Boarder

GK User
Sun Jun 02, 2013 4:51 am
When in category mode you need to use $products model not $this->products as $this refers to single item you are currently viewing such as product details page.

Image

This is for category view. ( templates/gk_esport/html/com_virtuemart/category/default.php )

Get Product Model from Virtuemart, add code after line 38 "ScriptDeclaration"
Code: Select all
$products = VmModel::getModel('product');
$ratingModel = VmModel::getModel('ratings');


Find this code around line 219
Code: Select all
               <p class="product_s_desc">
                  <?php echo shopFunctionsF::limitStringByWord($product->product_s_desc, 40, '...') ?>
               </p>
               <?php } ?>


Then replace your rating code with below or add new.
Code: Select all
               <?php // Product Rating
               $product->showRating = $ratingModel->showRating($product->virtuemart_product_id);
               if ($product->showRating) {
                     $product->vote = $ratingModel->getVoteByProduct($product->virtuemart_product_id);
                     $product->rating = $ratingModel->getRatingByProduct($product->virtuemart_product_id);
                     $maxrating = VmConfig::get('vm_maximum_rating_scale', 5);

                     if (empty($product->rating)) {
               ?>
                     <span class="vote"><?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' . JText::_('COM_VIRTUEMART_UNRATED') ?></span>
               <?php } else { ?>
                     <span class="vote">
                     <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' . round($product->rating->rating, 2) . '/' . $maxrating; ?><br/>
                        <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating . '/' . $maxrating) ?>" class="vmicon vm2-stars<?php echo round($product->rating->rating); ?>" style="display:inline-block;">
                        </span>
                     </span>
                 <?php } ?>
               <?php }?>


I left Rating 4/5 text in there for reference.
You also need to change html markup section in product details page as our style uses following css class to display star icons so that is why you might be seeing 5 stars in all products. See span class in this reply.

Code: Select all
class="vmicon vm2-stars<?php echo round($product->rating->rating); ?>" style="display:inline-block;">
                        </span>


Following css classes are created by rounding rating value.
Code: Select all
.vm2-stars
.vm2-stars1
.vm2-stars2
.vm2-stars3
.vm2-stars4
.vm2-stars5


See you around...

Search Terms: Virtuemart Category View Rating , Product Ratings, display ratings.
User avatar
Platinum Boarder

GK User
Mon Jun 03, 2013 10:55 am
normanUK wrote:I left Rating 4/5 text in there for reference.
You also need to change html markup section in product details page as our style uses following css class to display star icons so that is why you might be seeing 5 stars in all products. See span class in this reply.

Code: Select all
class="vmicon vm2-stars<?php echo round($product->rating->rating); ?>" style="display:inline-block;">
                        </span>


Following css classes are created by rounding rating value.
Code: Select all
.vm2-stars
.vm2-stars1
.vm2-stars2
.vm2-stars3
.vm2-stars4
.vm2-stars5



Hi
Thank you. Will try this.
But what do you mean by this last part.
Where do I add/edit this?
User avatar
Junior Boarder

teitbite
Mon Jun 03, 2013 11:01 am
Hi

It's working :) I see Norman You have mastered VM. I've been always focused on RedShop, but Your tip got me thinking if VM isn't better to customize.
User avatar
Moderator

GK User
Mon Jun 03, 2013 11:07 am
@teitbite,
Mastered? No : ) It is easy if you are building the complete site yourself as you can manipulate $data as you want, but it is not easy when putting general codes to use by lot of users :)

@westerberg,
That was for your product details page as I have already added this in category view I've posted.

I believe you can skip that step for your products details page as I was looking at your original code at your first post which calculates width then adds to css class. This was true for original VM layout but our style uses different approach. Looking at later replies I think @teitbite has done this already.

See you around...
User avatar
Platinum Boarder

GK User
Mon Jun 03, 2013 12:19 pm
Great!!!

Is it possible not to show anything on category page unless the product has been rated?
Looks a bit messy with all text...
User avatar
Junior Boarder

teitbite
Mon Jun 03, 2013 4:15 pm
Hi

Should be able with code like this:

Code: Select all
               <?php // Product Rating
                           $product->showRating = $ratingModel->showRating($product->virtuemart_product_id);
                           if ($product->showRating) {
                                 $product->vote = $ratingModel->getVoteByProduct($product->virtuemart_product_id);
                                 $product->rating = $ratingModel->getRatingByProduct($product->virtuemart_product_id);
                                 $maxrating = VmConfig::get('vm_maximum_rating_scale', 5);

                                 if (!empty($product->rating)) {
                        ?>
                                 <span class="vote">
                                 <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' . round($product->rating->rating, 2) . '/' . $maxrating; ?><br/>
                                 <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating . '/' . $maxrating) ?>" class="vmicon vm2-stars<?php echo round($product->rating->rating); ?>" style="display:inline-block;">
                                 </span>
                                 </span>
                          <?php } ?>


ftp access has changes, so I could not try it myself.
User avatar
Moderator


cron