0

I am in Magento 1.9 and I need a page listing all products with special prices. I achieved that by simply adding a new category with the Custom layout field containing :

<reference name="content">
    <remove name="product_list"/>
    <block type="catalog/product_sale" name="product_sale" alias="sale" template="catalog/product/list.phtml">
    <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
    <block type="page/html_pager" name="product_list_toolbar_pager"/></block>

    <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>

I added the new Sale.php block in /app/code/local/Mage/Catalog/Block/Product/Sale.php that overrides the medthod _getProductCollection() :

class Mage_Catalog_Block_Product_Sale extends Mage_Catalog_Block_Product_List
{
    protected function _getProductCollection()
    {
        if (is_null($this->_productCollection)) {
            $layer = $this->getLayer();
            /* @var $layer Mage_Catalog_Model_Layer */
            if ($this->getShowRootCategory()) {
                $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
            }

            // if this is a product view page
            if (Mage::registry('product')) {
                // get collection of categories this product is associated with
                $categories = Mage::registry('product')->getCategoryCollection()
                                  ->setPage(1, 1)
                                  ->load();
                // if the product is associated with any category
                if ($categories->count()) {
                    // show products from this category
                    $this->setCategoryId(current($categories->getIterator()));
                }
            }

            $origCategory = null;

            if ($layer->getCurrentCategory()->getId()) {
                $category = Mage::getModel('catalog/category')->load($layer->getCurrentCategory()->getId());

                if ($category->getId()) {
                    $origCategory = $layer->getCurrentCategory();
                    $layer->setCurrentCategory($category);
                    $this->addModelTags($category);
                }
            }

            $page = Mage::getBlockSingleton('page/html_pager')->getCurrentPage();
            date_default_timezone_set(Mage::getStoreConfig('general/locale/timezone'));
            $todayDate = strftime("%Y-%m-%d",Mage::app()->getLocale()->storeTimeStamp(Mage::app()->getStore()->getId()));

            // On veut tous les produits en prix spécial
            $products = Mage::getModel('catalog/product')
                ->getCollection()
                ->addAttributeToSelect(array('name','status', 'price', 'special_price', 'small_image','required_options','special_from_date', 'special_to_date'), 'inner')
                ->joinField('stock_status','cataloginventory/stock_status','stock_status',
                    'product_id=entity_id', array(
                      'stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK,
                      'website_id' => Mage::app()->getWebsite()->getWebsiteId(),
                    ))
                ->addAttributeToFilter('special_price', array('gt' => 0), 'left')
                ->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('created_at', 'desc')
                ->addAttributeToFilter('status', 1)
                ->addFinalPrice()
                ->addUrlRewrite()
                ->setCurPage($page);

            $this->_productCollection = $products;

            $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

            if ($origCategory) {
                $layer->setCurrentCategory($origCategory);
            }
        }

        return $this->_productCollection;
    }
}

It is working, I get what I want, except the actions of the toolbar. I am mainly interested with the pagination. For example when I try to go on page 2, the ajax pagination fails with:

Fatal error: Call to a member function toHtml() on a non-object in
 C:\wamp\www\app\code\local\Magentothem\Layerednavigationajax\controllers\CategoryController
.php

If I manually add ?p=2 to the URL, it is working and goes to page 2. I guess the issue is related to a missing block in the Custom layout that breaks the ajax, but I can't find it out.

Thank you

  • This seems to have to do with your custom theme "Magentothem" which we don't all know about. Have you tried debugging what's in there to try and find out what's happening? – Julien Lachal May 10 '16 at 8:19
  • Obviously I acted before thinking ;] In the Magentothem module, the block name was the default one "product_list" $productlist = $this->getLayout()->getBlock('product_list')->toHtml(); when in my case I render a custom block called "product_sale". Thank you @JulienLachal – feub May 10 '16 at 11:27
0

Obviously I acted before thinking ;]

In the Magentothem module controller, the block name rendered was the default one "product_list" $productlist = $this->getLayout()->getBlock('product_list')->toHtml(); when in my case I render a custom block called "product_sale". For my particular case, I simply modified the Magentothem controller to check what block needs to be used.

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.