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

d0ublezer0

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

Datatonic

This works great bar 1 thing.  It shows the currently viewed product in the recent list.
Any idea how to hide it?

d0ublezer0

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.

Datatonic

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?

d0ublezer0

#4
Ok, change the logical part of the code to:
-- del --
I've updated code in first message.


d0ublezer0

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.

ocean1

How can I override the theme/template for this item.
And also for the top10 theme

Datatonic

Thank you very much for the edits.

This is a great addition to the product details page.

DaggaTora

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!
Joomla 2.5.17 | VM2.0.26d | PHP 5.3.28

DaggaTora

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
Joomla 2.5.17 | VM2.0.26d | PHP 5.3.28

avpfilms

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 :)

srekoble

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.

srekoble

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 ..

gpessoa

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?



J 4.4.4; VM 4.2.6; PHP 8.3.6