Jump to content

Random products in category page, session vars


jass82
 Share

Recommended Posts

Good morning everyone,

I have the category page of my prestashop, which loads the products randomly, so that the old products do not sink, and we have a greater product rotation. I get this rotation through smarty with the variable {$products|@shuffle}; which randomly orders the array of products.

The problem is that when I access a product and I want to go back, when reloading the page, that array orders it for me differently and it's a nuisance, when you have hundreds of products in the same category, since you don't know where to go. you have been, since the order is different from the previous preview.

I think that through SESSION variables I could do it, I have a session variable [previous_category] in which, I am saving the visited category, and which I consult when going back to know if I have changed the category or not.

IF I continue in the same category, I would have to retrieve the array of products saved in that session variable [array_products_anterior], to be able to show you the same list that you saw in your first visit to that category, or in case of changing to another category, generate again the content of those variables.

The problem I have is that $ _SESSION ["array_productos_anterior"] = {$products}; I can't get it to work and to keep the array of products visited for me, to later recover it. I have tried many different syntax or pass it through json_encode, but I cannot store that array of products and be able to retrieve it again, however the $ _SESSION ["categoria_anterior"] = {$id_category}; if it picks it up well as it is a "simple" variable and not an array

 

{php} 
         session_start();
            {$products|@shuffle};
            if($_SESSION["categoria_anterior"] == "")
            {
            	 $_SESSION["categoria_anterior"] = {$id_category};
            	 $_SESSION["array_productos_anterior"] = {$products};
            }
            else
             { 
                if($_SESSION["categoria_anterior"] == {$id_category})
                { 
                	echo "same category ".$_SESSION["array_productos_anterior"]."";
                    //take value of  $_SESSION["array_productos_anterior"]  and assign to {$products}          	
                }
                else
                 {    echo "category changed";
                 	 $_SESSION["categoria_anterior"] = {$id_category};
                     $_SESSION["array_productos_anterior"] =  {$products};
                 }  
             } 
     

 

this line is not correct, and dont save the array into session var  ¿Why?

 $_SESSION["array_productos_anterior"] = {$products};

 

Edited by jass82 (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More