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

Show recently viewed products in product details page

Started by d0ublezer0, October 11, 2012, 19:19:16 PM

Previous topic - Next topic

srekoble

My modification to show the images of the latest viewed products was the following.

....
<div class="product-recent-products">
            <h2>Recently viewed products:</h2>
            <ul class="recent-list">
                <?php
                    foreach ($recentProducts as $rProduct) {
                    ?>
                    <li>
                       <?php echo $this->product->images[0]->displayMediaFull('class="product-image"',false) ?>
                        <a href="<?php echo JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id='.$rProduct->virtuemart_product_id.'&virtuemart_category_id='.$rProduct->virtuemart_category_id); ?>">
                            <?php echo $rProduct->product_name; ?>
                        </a>
                    </li>
                    <?php } ?>
            </ul>
        </div>


The whole code remains the same and i also add this to display the image of the product too. <?php echo $this->product->images[0]->displayMediaFull('class="product-image"',false) ?>


BUT STILL DISPLAY ONLY 2 PRODUCTS !!! any ideas how to make it displays 3 of the latest viewed ?

DaggaTora

Quote from: srekoble on December 20, 2012, 08:56:41 AM
My modification to show the images of the latest viewed products was the following.


The whole code remains the same and i also add this to display the image of the product too. <?php echo $this->product->images[0]->displayMediaFull('class="product-image"',false) ?>


BUT STILL DISPLAY ONLY 2 PRODUCTS !!! any ideas how to make it displays 3 of the latest viewed ?

With this I only get the same image of the product showing but not the recently viewed product images.

Also get only 2...

any heroe to the rescue?
Joomla 2.5.17 | VM2.0.26d | PHP 5.3.28

DaggaTora

#17
Ok, finally I got 3 with images that is what i have in VM config:

<?php
        
// d0ublezer0 recent viewed products mixed with DaggaTora images and number of products

        
function getRecentProducts($currentId){
            
$actualIds=false;
            
$rProducts=false;
            
            
$rSession JFactory::getSession();
            
$rIds $rSession->get('vmlastvisitedproductids', array(), 'vm'); // get recent viewed from browser session
            
if (is_array($rIds)){ 
                foreach(
$rIds as $rId){
                    if (
$rId!=$currentId$actualIds[]=$rId// cut out from array currently viewed product 
                
}  
            }

            if (
is_array($actualIds)){
                if (!
class_exists('VirtueMartModelProducts')) // check possible if VM products class exists
                    
JModel::addIncludePath(JPATH_VM_ADMINISTRATOR DS 'models'); // if not exists, add them
                
$rModel JModel::getInstance('Product''VirtueMartModel');

                
$recent_products_rows VmConfig::get('recent_products_rows'); // set in VM admin panel
                
$products_per_row VmConfig::get('homepage_products_per_row'); // set in VM admin panel
                
$recent_products_count $products_per_row $recent_products_rows// get max recent products count

                
$rProducts $rModel->getProducts($actualIdsfalsefalse);  // no front, no calc, only published
            
}
            if (
is_array($rProducts)) $rProducts=array_slice($rProducts,0,$recent_products_count); // return only allowed num of products

            
return $rProducts;
        }

        
$recentProducts=getRecentProducts($rProducts->product->virtuemart_product_id);
        if (
$recentProducts){ // if we get recent products, display them
        
?>

        <div class="product-recent-products">
           
            <ul class="recent-list">
                <?php
                    
foreach ($recentProducts as $rProduct) {
                    
?>

                    <li style="float: left;
width: 100%;"><?php echo $rProduct->images[0]->displayMediaThumb('class="featuredProductImage" style="max-width:50px;float:left;vertical-align:middle" border="0"',false) ;?>
                     <a style="padding-top: 14px !important;padding-left: 10px;float: left;" href="<?php echo JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id='.$rProduct->virtuemart_product_id.'&virtuemart_category_id='.$rProduct->virtuemart_category_id); ?>">
                            <?php echo $rProduct->product_name?>
                        </a>
                    </li>
                    <?php ?>
            </ul>
        </div>
        <?php
        
}
?>


but... how could i get rid of child products and show only parents?

PS:Bad news... it gives an error if you dont visit at least 3 products:

Fatal error: Call to a member function displayMediaThumb() on a non-object

This is because the line <?php echo $rProduct->images[0]->displayMediaThumb('class="featuredProductImage" style="max-width:50px;float:left;vertical-align:middle" border="0"',false) ;?> should be with $this bu it doesn´t work with $this.

... Help?!  ::)
Joomla 2.5.17 | VM2.0.26d | PHP 5.3.28

DaggaTora

Joomla 2.5.17 | VM2.0.26d | PHP 5.3.28

d0ublezer0

#19
Add
if (!class_exists('VmMediaHandler')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'mediahandler.php');

This should add functions for media handling, including displayMediaThumb(), located in file administrator\components\com_virtuemart\helpers\mediahandler.php:
/**
* This function displays the image, when the image is not already a resized one,
* it tries to get first the resized one, or create a resized one or fallback in case
*
* @author Max Milbers
*
* @param string $imageArgs Attributes to be included in the <img> tag.
* @param boolean $lightbox alternative display method
* @param string $effect alternative lightbox display
* @param boolean $withDesc display the image media description
*/
function displayMediaThumb($imageArgs='',$lightbox=true,$effect="class='modal' rel='group'",$return = true,$withDescr = false,$absUrl = false, $width=0,$height=0){

if(empty($this->file_name)){

if($return){
if($this->file_is_downloadable){
$file_url = $this->theme_url.'assets/images/vmgeneral/'.VmConfig::get('downloadable','zip.png');
$file_alt = JText::_('COM_VIRTUEMART_NO_IMAGE_SET').' '.$this->file_description;
return $this->displayIt($file_url, $file_alt, '',true,'',$withDescr);
} else {
$file_url = $this->theme_url.'assets/images/vmgeneral/'.VmConfig::get('no_image_set');
$file_alt = JText::_('COM_VIRTUEMART_NO_IMAGE_SET').' '.$this->file_description;
return $this->displayIt($file_url, $file_alt, $imageArgs,$lightbox);
}
}
}

if(!empty($this->file_url_thumb)){
$file_url = $this->file_url_thumb;
}

$media_path = JPATH_ROOT.DS.str_replace('/',DS,$this->file_url_thumb);

if(empty($this->file_meta)){
if(!empty($this->file_description)){
$file_alt = $this->file_description;
} else if(!empty($this->file_name)) {
$file_alt = $this->file_name;
} else {
$file_alt = '';
}
} else {
$file_alt = $this->file_meta;
}

if ((empty($this->file_url_thumb) || !file_exists($media_path)) && is_a($this,'VmImage')) {

if(empty($width)) $width = VmConfig::get('img_width', 90);
if(empty($height)) $height = VmConfig::get('img_height', 90);
$this->file_url_thumb = $this->createThumb($width,$height);
// vmdebug('displayMediaThumb',$this->file_url_thumb);
$media_path = JPATH_ROOT.DS.str_replace('/',DS,$this->file_url_thumb);
$file_url = $this->file_url_thumb;

//Here we need now to update the database field of $this->file_url_thumb to prevent dynamic thumbnailing in future
if(empty($this->_db)) $this->_db = JFactory::getDBO();
$query = 'UPDATE `#__virtuemart_medias` SET `file_url_thumb` = "'.$this->_db->getEscaped($this->file_url_thumb).'" WHERE `#__virtuemart_medias`.`virtuemart_media_id` = "'.(int)$this->virtuemart_media_id.'" ';
$this->_db->setQuery($query);
$this->_db->query();
}

if($withDescr) $withDescr = $this->file_description;
if (empty($this->file_url_thumb) || !file_exists($media_path)) {
return $this->getIcon($imageArgs,$lightbox,$return,$withDescr,$absUrl);
}

if($return) return $this->displayIt($file_url, $file_alt, $imageArgs,$lightbox,$effect,$withDescr,$absUrl);

}

or, you can use clean HTML code to display your image (possible only, if you already have thumb image)
or, else use function displayIt() from mediaHandler class

d0ublezer0

By the way - your code should not to work. Did you really test it?

Your mistakes:

srekoble,
$this->product->images[0]
reflects current product

DaggaTora,
$rProduct->images[0]
will always be empty, because $rModel->getProducts did not return images

d0ublezer0

#21
Oh, okay, there is fully working solution, created today, much better and much simple (just throw old code from first post to trash, and change him to this) :

<?php
        
// d0ublezer0 recently viewed products -- start --
        // first, cleanup the variables, just in case
        
$actualIds=false
        
$recentProducts=false;

        
$rSession JFactory::getSession();
        
$rIds $rSession->get('vmlastvisitedproductids', array(), 'vm'); // get recent viewed from browser session
        
if (is_array($rIds)){ 
            foreach(
$rIds as $rId){
                if (
$rId!=$currentId$actualIds[]=$rId// cut out from array currently viewed product 
            
}  
        }
        if (
is_array($actualIds)){
            
$recent_products_rows VmConfig::get('recent_products_rows'); // set in VM admin panel
            
$products_per_row VmConfig::get('homepage_products_per_row'); // set in VM admin panel
            // override next line to define number of recent to display, for example: $recent_products_count = 7;
            
$recent_products_count $products_per_row $recent_products_rows// get max recent products count

            
$productModel VmModel::getModel('product');
            
$mediaModel VmModel::getModel('Media');
            
$recentProducts $productModel->getProducts($actualIdsfalsefalse);  // no front, no calc, only published
            
$mediaModel->addImages($recentProducts,1); // Digit 1 - is the limit of returned product images. For recent list usually you wil be need only one picture
        
}

        if (
is_array($recentProducts)) // if we have recent products
            
$recentProducts=array_slice($recentProducts,0,$recent_products_count); // return only allowed num of products

        
if ($recentProducts){ // if we get recent products, display them
        
?>

        <div class="product-recent-products">
            <h2>Recently viewed products:</h2>
            <ul class="recent-list">
                <?php
                    
foreach ($recentProducts as $rProduct) {
                    
?>

                    <li>
                        <a href="<?php echo JRoute::_('index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id='.$rProduct->virtuemart_product_id.'&virtuemart_category_id='.$rProduct->virtuemart_category_id); ?>">
                            <?php 
                                
// images[0] - this is first image in images list of the current processed recent product
                                // also, you can use ->file_url for the full image
                                
echo '<img src="'.$rProduct->images[0]->file_url_thumb.'" />';
                                echo 
$rProduct->product_name
                            
?>

                        </a>
                    </li>
                    <?php ?>
            </ul>
        </div>
        <?php
        
}
        
// d0ublezer0 recently viewed products -- end --
?>



With images, tested on 2.0.18

DaggaTora

Absolutly perfect!!!!  ;D

Thank you very very very very much! This should be implemented in a module inside aio.
Joomla 2.5.17 | VM2.0.26d | PHP 5.3.28

d0ublezer0

Thanks, glad to help.

Little hint:
$mediaModel->addImages($recentProducts,1);
Digit 1 - is the limit of returned product images.
For recent list usually you wil be need only one picture. Also, you can get more images by defining greater number.

den_piero

Hi d0ublezer0, Thanx for the code but it display warning message on 2.0.20 "COM_VIRTUEMART_MEDIA_CHOOSE_TYPE".

ttigerwods


Garyk01

#26
Quote from: den_piero on April 21, 2013, 07:48:18 AM
Hi d0ublezer0, Thanx for the code but it display warning message on 2.0.20 "COM_VIRTUEMART_MEDIA_CHOOSE_TYPE".
This message appears when you view the product with noimage.gif.
How to fix it?

Stonedfury

Funny thing about this post. Virtuemart already has a recently viewed module. Its the top ten. Thanks to virtueplanet I learned this.
Open up your modules\mod_virtuemart_product\mod_virtuemart_product.xml and find this line <option value="topten">MOD_VIRTUEMART_PRODUCT_BEST_SALES</option> and add below <option value="recent">Recently Viewed Products</option>

Open your module and adjust accordingly. Voila
No such thing as a stupid question, only a stupid answer. I won't ask if I know the answer or found it using the search as you suggested.

Garyk01

Quote from: Stonedfury on May 12, 2013, 18:49:42 PM
Funny thing about this post. Virtuemart already has a recently viewed module. Its the top ten. Thanks to virtueplanet I learned this.
Open up your modules\mod_virtuemart_product\mod_virtuemart_product.xml and find this line <option value="topten">MOD_VIRTUEMART_PRODUCT_BEST_SALES</option> and add below <option value="recent">Recently Viewed Products</option>

Open your module and adjust accordingly. Voila

Thank you! The perfect solution!
Very good! 

rajmohansoft

Hi

I have problem the recently viewed product only showed after clear cache.is there any solution for this problem.