0

I need to write a controller that get a product id and return the html that this product would generate if shown in a category page.

for example:

enter image description here

To be clear I got everything working to the point of getting the HTML. I tried several methods but they all failed, and I'm not sure what's the right approach to do it.

Thanks!

  • can you share your tried code? – Abdul Dec 22 '15 at 13:11
  • its a lot of code and messy and I didn't want to make the question too long, but basically I started by trying to inherit from Mage_Catalog_Block_Product_List to render a single product (using toHtml()), but as I went through things got more and more complicated and weird errors appeared until I finally felt like maybe there's a better way. if no answers will pop-up I will post some bits of code and try to get help on that specific direction. but for now I don't want to "lock" the answers to a single approach. thanks. – Ronen Ness Dec 22 '15 at 13:20
  • There won't be any direct method to get that HTML. You'll need to re-generate that in your own controller. If you are extending the Mage_Catalog_Block_Product_List, there shouldn't be any issue. Is there any specific issue in your code which you are trying to remove ? – Prateek Dec 22 '15 at 13:54
  • well right now I'm just getting null from toHtml() (but no errors), but I think I'll research some more and upload an another question for that if needed – Ronen Ness Dec 22 '15 at 15:40
0

Answering myself for future seekers:

First, define a customized block to render just products without the toolbar or the additions, based on product ids:

<?php

/**
 * Block used to render products list html, without any additions
 */
class Vendor_Package_Block_RenderProducts extends Mage_Catalog_Block_Product_List
{
    protected $_productIds = null;

    /**
     * set the list of product ids to render
     **/
    public function setProductsList($productIds)
    {
        $this->_productIds = $productIds;
    }


    /**
     * Retrieve loaded collection based on ids
     **/
    protected function _getProductCollection()
    {
        $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToFilter('entity_id', array('in' => $this->_productIds))
                ->addAttributeToSelect('*')
                ->load();
        return $collection;
    } 

    /**
     * We override this function so we won't dispatch the catalog_block_product_list_collection event.
     * Note: we must add the toolbar as child because it is used internally to determine how to display
     * the products. but we still need to not render it somehow.
     */
    protected function _beforeToHtml()
    {
        $toolbar = $this->getToolbarBlock();

        // called prepare sortable parameters
        $collection = $this->_getProductCollection();

        // use sortable parameters
        if ($orders = $this->getAvailableOrders()) {
            $toolbar->setAvailableOrders($orders);
        }
        if ($sort = $this->getSortBy()) {
            $toolbar->setDefaultOrder($sort);
        }
        if ($dir = $this->getDefaultDirection()) {
            $toolbar->setDefaultDirection($dir);
        }
        if ($modes = $this->getModes()) {
            $toolbar->setModes($modes);
        }

        // set collection to toolbar and apply sort
        $toolbar->setCollection($collection);
        $this->setChild('toolbar', $toolbar);

        // call the base _beforeToHtml(), while skipping the Mage_Catalog_Block_Product_List::beforeToHtml()
        return Mage_Catalog_Block_Product_Abstract::_beforeToHtml();
    }

    /**
     * override this to disable additions rendering
     */
    public function getAdditionalHtml()
    {
        return "";
    }

    /**
     * override this to disable toolbar rendering
     */
    public function getToolbarHtml()
    {
        return "";
    }
}

Now you can use it like this:

$block = $this->getLayout()->createBlock('vendor_package/renderProducts')
                            ->setTemplate('catalog/product/list.phtml');
$block->setProductsList(array(id1, id2, ...));
echo $block->toHtml();

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.