Question

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!

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top