VirtueMart Forum

VirtueMart 2 + 3 + 4 => Templating & Layouts => Topic started by: d0ublezer0 on October 11, 2012, 19:19:16 PM

Title: Show recently viewed products in product details page
Post by: d0ublezer0 on October 11, 2012, 19:19:16 PM
I remember someone here asked the question, how to show a list of recently viewed products?

On the main page is visible by default.
But on the other pages of this functionality does not exist.

I suggest to use a solution that will display a list of the last viewed products:

<?php
        
// d0ublezer0 recent viewed 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($this->product->virtuemart_product_id);
        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 echo $rProduct->product_name?>
                        </a>
                    </li>
                    <?php ?>
            </ul>
        </div>
        <?php
        
}
?>



This code must be inserted into your template view the product details.
Put this code in any place, where it should to be, according to your design.
For this purpose I used template file:
\templates\my_template\html\com_virtuemart\productdetails\default.php
copied from
\components\com_virtuemart\views\productdetails\tmpl\default.php

P.S. Tested on 2.0.12
Title: Re: Show recently viewed products in product details page
Post by: Datatonic on October 20, 2012, 19:09:46 PM
This works great bar 1 thing.  It shows the currently viewed product in the recent list.
Any idea how to hide it?
Title: Re: Show recently viewed products in product details page
Post by: d0ublezer0 on October 20, 2012, 20:38:47 PM
Quote from: Datatonic on October 20, 2012, 19:09:46 PM
This works great bar 1 thing.  It shows the currently viewed product in the recent list.
Any idea how to hide it?
Heh, this code shows a list of recent products where it is not provided by developers.
In your case, I think it is enough to turn off the option "Show Recent Products" in VM settings.
Title: Re: Show recently viewed products in product details page
Post by: Datatonic on October 20, 2012, 21:15:29 PM
Sorry I'll explain clearer.

When recently viewed products is viewed on the main shop page, where this code is taken from, you are not at that point viewing a product.

When you implement this code on a product page, it shows all recently viewed products including the product you are currently viewing.

This doesn't quite make sense to a user, so my question is, is there a way to get this to hide the listing for the current product but show all the other recently viewed products?

Make more sense?
Title: Re: Show recently viewed products in product details page
Post by: d0ublezer0 on October 21, 2012, 19:44:29 PM
Ok, change the logical part of the code to:
-- del --
I've updated code in first message.
Title: Re: Show recently viewed products in product details page
Post by: VM_Fans on October 22, 2012, 03:12:47 AM
good solution.
Title: Re: Show recently viewed products in product details page
Post by: d0ublezer0 on October 22, 2012, 07:14:11 AM
Quote from: VM_Fans on October 22, 2012, 03:12:47 AM
good solution.
Thank you. Updated code in first message.

I would also like to get the additional parameters of the products, in order to display this list with prices and pictures.
Title: Re: Show recently viewed products in product details page
Post by: ocean1 on October 22, 2012, 19:17:26 PM
How can I override the theme/template for this item.
And also for the top10 theme
Title: Re: Show recently viewed products in product details page
Post by: Datatonic on October 25, 2012, 01:11:45 AM
Thank you very much for the edits.

This is a great addition to the product details page.
Title: Re: Show recently viewed products in product details page
Post by: DaggaTora on November 14, 2012, 19:16:37 PM
Great!

Thanks a lot. Can be the image be displayed before the name?

And... can be the number of the items in the list diferent from the VM config? for example, I want 4 to be displayed in the frontpage but 6 in the list. (I have a problem cause in the admin i have 4 to show and only get 3... but i  think that is another issue)


Thank you!
Title: Re: Show recently viewed products in product details page
Post by: DaggaTora on November 19, 2012, 11:58:35 AM
Quote from: DaggaTora on November 14, 2012, 19:16:37 PM
Great!

Thanks a lot. Can be the image be displayed before the name?

And... can be the number of the items in the list diferent from the VM config? for example, I want 4 to be displayed in the frontpage but 6 in the list. (I have a problem cause in the admin i have 4 to show and only get 3... but i  think that is another issue)


Thank you!

any idea?  :'(  ;D
Title: Re: Show recently viewed products in product details page
Post by: avpfilms on December 09, 2012, 17:53:33 PM
Quote from: DaggaTora on November 14, 2012, 19:16:37 PM
Great!

Thanks a lot. Can be the image be displayed before the name?

And... can be the number of the items in the list diferent from the VM config? for example, I want 4 to be displayed in the frontpage but 6 in the list. (I have a problem cause in the admin i have 4 to show and only get 3... but i  think that is another issue)


Thank you!

Exact same issue- I've tried putting in number, the code isn't getting to config file for some reason.  If anyone has solution, that would be great.  I need to get 12 recently viewed to display with manufacturer showing as well.  The number is set in the config , but no matter what it is set to, this will only return 3.  Any suggestions welcomed :)
Title: Re: Show recently viewed products in product details page
Post by: srekoble on December 18, 2012, 11:28:01 AM
Hello, anybody knows how to modify this code to show also the pictures of the latest products ? in my case when i put this code in default.php it only display 2 links of my latest products.
Title: Re: Show recently viewed products in product details page
Post by: srekoble on December 19, 2012, 18:13:56 PM
Hello, i modify the code and i manage to display the image of teh product too. But in my case it only display 2 of the recent viewed products what should i change to make it display the last 3 recent viewed products ?

I tried to make  $recent_products_count = 3;   but it didn't work.

Please any help ..
Title: Re: Show recently viewed products in product details page
Post by: gpessoa on December 20, 2012, 00:29:34 AM
I have the same issue.

Put the code, but only two recently viewed products are showed.

Any ideas how to do this to show more?



Title: Re: Show recently viewed products in product details page
Post by: srekoble on December 20, 2012, 08:56:41 AM
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 ?
Title: Re: Show recently viewed products in product details page
Post by: DaggaTora on January 28, 2013, 16:28:34 PM
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?
Title: Re: Show recently viewed products in product details page
Post by: DaggaTora on January 30, 2013, 11:35:11 AM
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?!  ::)
Title: Re: Show recently viewed products in product details page
Post by: DaggaTora on February 13, 2013, 18:16:13 PM
someone with a big soul and some php knowledge?  ;)
Title: Re: Show recently viewed products in product details page
Post by: d0ublezer0 on March 01, 2013, 19:32:39 PM
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
Title: Re: Show recently viewed products in product details page
Post by: d0ublezer0 on March 01, 2013, 19:59:47 PM
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
Title: Re: Show recently viewed products in product details page
Post by: d0ublezer0 on March 02, 2013, 16:15:43 PM
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
Title: Re: Show recently viewed products in product details page
Post by: DaggaTora on March 04, 2013, 12:33:30 PM
Absolutly perfect!!!!  ;D

Thank you very very very very much! This should be implemented in a module inside aio.
Title: Re: Show recently viewed products in product details page
Post by: d0ublezer0 on March 07, 2013, 07:12:13 AM
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.
Title: Re: Show recently viewed products in product details page
Post by: 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".
Title: Re: Show recently viewed products in product details page
Post by: ttigerwods on April 27, 2013, 22:30:28 PM
Thank's, very good
Title: Re: Show recently viewed products in product details page
Post by: Garyk01 on May 12, 2013, 11:36:55 AM
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?
Title: Re: Show recently viewed products in product details page
Post by: 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
Title: Re: Show recently viewed products in product details page
Post by: Garyk01 on May 12, 2013, 22:07:36 PM
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! 
Title: Re: Show recently viewed products in product details page
Post by: rajmohansoft on October 24, 2015, 15:03:34 PM
Hi

I have problem the recently viewed product only showed after clear cache.is there any solution for this problem.
Title: Re: Show recently viewed products in product details page
Post by: d0ublezer0 on August 20, 2020, 09:32:58 AM
Hello, guys! Glad to see you again  ;)
I give you another fresh and simpy solution:

<?php
$productModel 
VmModel::getModel('Product');
    
$recent_products["products"] = $productModel->getProductListing('recent'6);
if (
count($recent_products["products"]) > 1) {
        
// current product will be always in recent, so we need more than 1
    
        
?>

        <h2>Recently viewed</h2>
        <?php
        
// check current product 
        
$current_key false;
        foreach (
$recent_products["products"] as $key => $prd) {
            if (
$prd->virtuemart_product_id == $this->product->virtuemart_product_id) {
                
$current_key $key;
            }
        }
        
// and remove him from recent list
        
if ($current_key !== false) unset($recent_products["products"][$current_key]);

        
$productModel->addImages($recent_products["products"], 1);
        echo 
shopFunctionsF::renderVmSubLayout('products', array('products' => $recent_products'currency' => $this->currency));
    } }


You able to use it on product details page and anywhere else.