0

I'm trying to generate a search result/category page with a custom product collection. The plan is to create the product collection based on attributes the customer can choose in a custom html form. I have an IndexController

public function indexAction() {
    $this->loadLayout();
    $this->renderLayout();
}

public function postAction() {
    $postData = $this->getRequest()->getPost();
    $productCollection = Mage::getModel('mymodule/api')->checkPost($postData);
    //$this->_redirect(???);
}

which is receiving postData from my form.phtml and passing it to my checkPost function (postData gets evaluated) which returns a custom productCollection. I would like to redirect the customer from the form to a category/search result page with the productCollection as results but I have no idea how to get this done.

  • I know this is not part of the question, but why did you use a POST instead of a GET? – Vernard Luz Jun 24 '15 at 15:41
  • GET is actually suitable for search results as it allows user the share the link of the search result instead of doing the search it all over again. – Vernard Luz Jun 24 '15 at 15:42
  • Hm I guess the main reason is that I'm at the very beginning of programming web applications... Thanks for pointing that out, I'll check it out. – mpfmon Jun 24 '15 at 16:23
1

You can not send a product collection between page requests, this would mean storing it in a session but sessions only work well for simple values.

You can load the blocks that Magento uses to display search results and add the collection to those. So add the catalog/product/list.phtml like it is done in the catalog.xml for example and add the following to your controller. However, I would use GET variables instead of POST for practical reasons

public function postAction() {
    $postData = $this->getRequest()->getPost();
    $productCollection = Mage::getModel('mymodule/api')->checkPost($postData);

    $this->loadLayout();

    $this->getLayout()->getBlock('product_list')->setCollection(productCollection);

    $this->renderLayout();
}

Side note: From the functionality you're describing I think Magento's native Advanced Search should fit your needs tho. Check out this post on how to set it up.

  • This looks promising! I'll update here as soon as I figured everything out. Thanks alot. Btw. yes, Advanced Search is actually really similiar to what I have in mind, the only difference is that I want to give much more customisation (for the admin) on how attributes affect each other. – mpfmon Jun 24 '15 at 19:06
0

You might want to use a GET request for that action. That way, you can directly display what you want using the collection (assuming that you are going to redirect for displaying the collection)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.