News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

How to display rating in category view

Started by anti-conformiste, May 21, 2012, 19:30:26 PM

Previous topic - Next topic

anti-conformiste

Hello,

I read this post http://forum.virtuemart.net/index.php?topic=97611.msg325346#msg325346 and tried Vapro's code, but it don't work anyway

Is there any solution to display average rating in the category view ?

Is it a bug ?

Is anybody else experiencing this problem ?

tanks in advance.

dodgyjammer

Try this  :)

To pull back individual product ratings for each product in category view you need to edit com_virtuemart/views/category/view.html.php

Around line 142 you need to change...


foreach($products as $product)
{
$product->stock = $productModel->getStockIndicator($product);
}


to...


$ratingModel = VmModel::getModel('ratings');
       
foreach($products as $product)
{
$product->stock = $productModel->getStockIndicator($product);
        $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);
        }
}


THEN you add the following where you want to show the stars for each product (com_virtuemart/views/category/tmpl/default.php)...


<?php if($product->showRating): ?>
    <?php $maxrating VmConfig::get('vm_maximum_rating_scale'5); ?>
    <?php if (!empty($product->rating)):
        
$ratingwidth = ( $product->rating->rating 100 ) / $maxrating?>

        <span class="vote">
            <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' round($product->rating->rating2) . '/' $maxrating?><br/>
            <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating '/' $maxrating?>" class="vmicon ratingbox" style="display:inline-block;">
                <span class="stars-orange" style="width:<?php echo $ratingwidth.'%'?>"></span>
            </span>
        </span>
        <br/><br/>
    <?php endif ?>
<?php endif ?>


bingo bango bongo

Favazza


Favazza

Can someone show me how to:

1. add code so stars show instead of numbers (i.e 5/5) in product rating
2. add these stars at category overview as well

Much appreciated :)
VM 2.0.18
J2.5.9

vnbillion

Quote from: dodgyjammer on July 05, 2012, 17:07:10 PM
Try this  :)

To pull back individual product ratings for each product in category view you need to edit com_virtuemart/views/category/view.html.php

Around line 142 you need to change...


foreach($products as $product)
{
$product->stock = $productModel->getStockIndicator($product);
}


to...


$ratingModel = VmModel::getModel('ratings');
       
foreach($products as $product)
{
$product->stock = $productModel->getStockIndicator($product);
        $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);
        }
}


THEN you add the following where you want to show the stars for each product (com_virtuemart/views/category/tmpl/default.php)...


<?php if($product->showRating): ?>
    <?php $maxrating VmConfig::get('vm_maximum_rating_scale'5); ?>
    <?php if (!empty($product->rating)):
        
$ratingwidth = ( $product->rating->rating 100 ) / $maxrating?>

        <span class="vote">
            <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' round($product->rating->rating2) . '/' $maxrating?><br/>
            <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating '/' $maxrating?>" class="vmicon ratingbox" style="display:inline-block;">
                <span class="stars-orange" style="width:<?php echo $ratingwidth.'%'?>"></span>
            </span>
        </span>
        <br/><br/>
    <?php endif ?>
<?php endif ?>


bingo bango bongo
thank you for dodgyjammer. great work !

pixelofficer

Still no fix of this in newest release of VM? I don't want to hack the core of VM :(

Milbo

There is no need to hack the core, the correct way is todo it only via the layout of the template. Just copy the default.php and insert in the corefolder or better in the template override folder.

Then search for this around line 208

// Start the Output
foreach ($this->products as $product) {


just replace it by

$ratingModel = VmModel::getModel('ratings');
// Start the Output
foreach ($this->products as $product) {

$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);
        }


Then search for

<!-- The "Average Customer Rating" Part -->
<?php if ($this->showRating) { ?>


There is an outcommented part, just replace this part by
$ratingwidth = ( $product->rating->rating * 100 ) / $maxrating; ?>
        <span class="vote">
            <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' round($product->rating->rating2) . '/' $maxrating?><br/>
            <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating '/' $maxrating?>" class="vmicon ratingbox" style="display:inline-block;">
                <span class="stars-orange" style="width:<?php echo $ratingwidth.'%'?>"></span>
            </span>
        </span>#


Go then into the vmconfig and choose for your categories the other layout.

I am thinking about to move also getStockIndicator into the template. Then we do not need an extra configuration option and people can decide by the layout if they want it or not.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

pixelofficer

#7
Milbo, thank you very much - but - your code returned lots of errors

- Trying to get property of non-object in ...
- Undefined variable: maxrating in...
- Division by zero in
- Trying to get property of non-object in
- Undefined variable: maxrating in

All at once :) But, Good thing is that there's no need to change com_virtuemart/views/category/view.html.php - Only default.php
So I've done your edits, but replaced this your code

$ratingwidth = ( $product->rating->rating * 100 ) / $maxrating; ?>
        <span class="vote">
            <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' round($product->rating->rating2) . '/' $maxrating?><br/>
            <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating '/' $maxrating?>" class="vmicon ratingbox" style="display:inline-block;">
                <span class="stars-orange" style="width:<?php echo $ratingwidth.'%'?>"></span>
            </span>
        </span>#



with dodgyjammer's:


<?php if($product->showRating): ?>
    <?php $maxrating VmConfig::get('vm_maximum_rating_scale'5); ?>
    <?php if (!empty($product->rating)):
        
$ratingwidth = ( $product->rating->rating 100 ) / $maxrating?>

        <span class="vote">
            <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' round($product->rating->rating2) . '/' $maxrating?><br/>
            <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating '/' $maxrating?>" class="vmicon ratingbox" style="display:inline-block;">
                <span class="stars-orange" style="width:<?php echo $ratingwidth.'%'?>"></span>
            </span>
        </span>
        <br/><br/>
    <?php endif ?>
<?php endif ?>


All changes done in default.php - so it's now just in the theme :) thank you :)

Marga123

#8
Hi 
I'm still new to php I've tried your code but
I got this error message

Parse error: syntax error, unexpected '}' in /templates/gantry/html/com_virtuemart/category/default.php on line 352

// Start the Output
$ratingModel = VmModel::getModel('ratings');
// Start the Output
foreach ($this->products as $product) {

$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);
        }


<!-- The "Average Customer Rating" Part -->
<?php if($product->showRating): ?>
    <?php $maxrating VmConfig::get('vm_maximum_rating_scale'5); ?>
    <?php if (!empty($product->rating)):
        
$ratingwidth = ( $product->rating->rating 100 ) / $maxrating?>

        <span class="vote">
            <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' round($product->rating->rating2) . '/' $maxrating?><br/>
            <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating '/' $maxrating?>" class="vmicon ratingbox" style="display:inline-block;">
                <span class="stars-orange" style="width:<?php echo $ratingwidth.'%'?>"></span>
            </span>
        </span>
        <br/><br/>
    <?php endif ?>
<?php endif ?>
<?php ?>
<?php
if ( VmConfig::get ('display_stock'1)) { ?>

<!-- if (!VmConfig::get('use_as_catalog') and !(VmConfig::get('stockhandle','none')=='none')){?> -->
<div class="paddingtop8">
<span class="vmicon vm2-<?php echo $product->stock->stock_level ?>" title="<?php echo $product->stock->stock_tip ?>"></span>
<span class="stock-level"><?php echo JText::('COM_VIRTUEMART_STOCK_LEVEL_DISPLAY_TITLE_TIP'?></span>
</div>
<?php ?>
</div>


Many Thanks for your help  :)

Maxim Pishnyak

You can support Community by voting for Project on the JED
https://extensions.joomla.org/extension/virtuemart/#reviews
Join us at
https://twitter.com/virtuemart

Marga123

#10
Thank you for your reply Maxim
I've managed to fix the 1st error message
but I have this error message now

Division by zero in templates/gantry/html/com_virtuemart/category/default.php on line 258

LINE 258    <?php  $ratingwidth = ( $product->rating->rating 100 ) / $maxrating?>[/size]


        <span class="vote">
            <?php echo JText::_('COM_VIRTUEMART_RATING') . ' ' round($product->rating->rating2) . '/' $maxrating?><br/>
            <span title=" <?php echo (JText::_("COM_VIRTUEMART_RATING_TITLE") . $product->rating->rating '/' $maxrating?>" class="vmicon ratingbox" style="display:inline-block;">
                <span class="stars-orange" style="width:<?php echo $ratingwidth.'%'?>"></span>
            </span>
        </span>#


Many Thanks for your help

Maxim Pishnyak

Quote from: Marga123 on August 26, 2013, 20:48:14 PM
I've managed to fix the 1st error message
but I have this error message now
but you also deleted this peace of code
$maxrating = VmConfig::get('vm_maximum_rating_scale', 5);
Are you sure that you know what are you doing?

You could repeat again:
what code advices do you have
what do you do with these code peaces
why do you do these specific operations with these peaces.
You can support Community by voting for Project on the JED
https://extensions.joomla.org/extension/virtuemart/#reviews
Join us at
https://twitter.com/virtuemart