Question

I have a magento store that needs to have two pages that show both the new products added to the store and the ones that are on a discount. Both show the same issue but I'll give you the code for the latter because I think it's the most problematic.

The issue is that I can succesfully show the list of products showing the criteria but I can't make the toolbar appear as it would on any other category. Also, whenever I click on a product from there I would lose the sidebar on the detail page of the product, as if the xml rules I have set up won't apply to the page because I'm comming from said promotions page.

This is the class I use to get the collection of products with a promotion active:

class Mage_Catalog_Block_Product_Promo extends Mage_Catalog_Block_Product_List{
protected $_productsCount = null;

const DEFAULT_PRODUCTS_COUNT = 5;

protected function _beforeToHtml()
{
    $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

    $collection = Mage::getResourceModel('catalog/product_collection');
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    $collection = $this->_addProductAttributesAndPrices($collection)
        ->addStoreFilter()
        ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
        ->addAttributeToFilter('special_to_date', array('or'=> array(
            0 => array('date' => true, 'from' => $todayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
        ), 'left')
        ->addAttributeToSort('flash', 'desc')
        ->setPageSize($this->getProductsCount())
        ->setCurPage(1)
    ;
    $this->setProductCollection($collection);

    $toolbar = $this->getToolbarBlock();
    $toolbar->setCollection($collection);
    $this->setChild('toolbar', $toolbar); 

    return parent::_beforeToHtml();
}

 public function setCollection($collection)
{
    $this->_productCollection = $collection;
    return $this;
}

public function setProductsCount($count)
{
    $this->_productsCount = $count;
    return $this;
}

public function getProductsCount()
{
    if (null === $this->_productsCount) {
        $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
    }
    return $this->_productsCount;
}
} 

This is my phtml where I get all my products:

<?php $current_cat= Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId(); ?>

<?php 
$_productCollection = $this->getProductCollection();
$_helper = $this->helper('catalog/output');
$_collectionSize = $_productCollection->count() ?>
<div class="promos_new_list">
    <div class="countcat">
        <?php //$_productCollection = $this->getLoadedProductCollection(); 
        $count = $_productCollection->getSize(); 
        echo $count; ?> Produits 
    </div>
<?php echo $this->getToolbarHtml(); ?>
<?php $_columnCount = 4; ?>
<?php $i=0; 
foreach ($_productCollection as $_product): ?>
    <?php if ($i++%$_columnCount==0): ?>
    <ul class="products-grid">
    <?php endif ?>
        <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
            <?php include('view/labels.phtml')  ?>                
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(178,108); ?>" width="178" height="108" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <div id="productimgover<?php echo $_product->getId()?>" style="display: none;"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(64); ?>" width="64" height="64" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></div>
            <div class="moreinfo">
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <div id='productname<?php echo $_product->getId()?>' style='display:none'><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></div>
                <p class="product-desc">
                    <?$desc=$this->htmlEscape($_product->getShortDescription());?>
                    <?php echo Mage::helper('function')->unalinea($desc,30);?>
                </p>
                <?php echo $this->getPriceHtml($_product, true) ?>

                <div class="nowmore orangegradient">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">En savoir plus </a><span></span>
                </div>

                <div class="clear"></div>

            </div>
        </li>
    <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
    </ul>
    <?php endif ?>
<?php endforeach ?>
</div>

I don't know if any more code is needed. A colleague says magento is not working properly when I do this because it doesn't have an active category but I don't really understand why? Can you guide me here?

Was it helpful?

Solution

Well so far I've dodged the toolbar problem using this:

<?php
$toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
$toolbar->setCollection($_productCollection);
echo $toolbar->toHtml();
?>

I'm not sure if that's the right way to approach the situation but it works for me for now.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top