Jump to content

Newest product page redirect


Marwen

Recommended Posts

I am designing a web shop and would like to know if (and how) it is possible to redirect those who visit the website to the product page of the product that was added most recently.

 

Does anyone know if that can be done (I do not want to redirect to a certain product ID but always to the newest product, whichever product id that might have).

 

Thanks for your help!

Marwen

Link to comment
Share on other sites

You can try with a custom query in the front controller (init method)

$last_id = Db::getInstance()->getValue('SELECT MAX(id_product) FROM '._DB_PREFIX_.'product');

Tools:redirect($this->context->link->getProductLink($last_id));

Didn't test it, but should work

Link to comment
Share on other sites

You can try with a custom query in the front controller (init method)

 

$last_id = Db::getInstance()->getValue('SELECT MAX(id_product) FROM '._DB_PREFIX_.'product');

 

Tools:redirect($this->context->link->getProductLink($last_id));

 

Didn't test it, but should work

Thank you for your answer. I have tried it, but it doesn't work. I'm sure I did something wrong. I am quite new at changing/adding code, so could you help me by explaining exactly where I should paste the code you provided? Thanks again!

Link to comment
Share on other sites

IndexController must extend IndexControllerCore, not the product one. After you fix it, clear cache, it will run

Thanks, I am getting closer to what I want. I get the newest product now, but I have trouble filtering the disabled products. Any idea what code to use to include only the Enabled products? So, the newest product that is active... I tried to include WHERE 'Active' = '1' , but could not get it to work.

 

What I have now (without the conditional active) is:

 

public function initContent()

    {

         parent::initContent();

   $lastproduct_id=Db::getInstance()->getValue('SELECT

     MAX(id_product) FROM '._DB_PREFIX_.'product');

   Tools::redirect('index.php?id_product='.$lastproduct_id.'&controller=product');

   $this->context->smarty->assign('HOOK_HOME', Hook::exec('displayHome'));

   $this->setTemplate(_PS_THEME_DIR_.'index.tpl');

    }

}

 

Thanks again!

Link to comment
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
×
×
  • Create New...