Monday, January 18, 2010

A list of all the products in the website (for travelingwilly)

Problem: a list of all the products in the website.

Possible solution:

The easiest (less code) solution is to create a category called 'All' an put all the products in that category(beside the category that they belong to).
The second method implies some code.
Create the Mage_Catalog_Block_Product_All class and put it in a file located here (app/code/code/Mage/Catalog/Block/Product/All.php)
<?php
class Mage_Catalog_Block_Product_All extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter(array(array('attribute'=>'visibility', 'eq'=>"4")));
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
}
?>

After this create a CMS page. and in it's content put the following
{{block type="catalog/product_all" template="catalog/product/list.phtml"}}
In the "Layout" field (from the 'Custom design' tab) select '2columns-left' (or any other design just don't leave it to 'empty')
Save and that's it.
If the page you just created has the identifier ('all-products') and is enabled you will see a paginated list of all your products here:
http://www.yoursite/all-products (or http://www.yoursite/{YOUR STORE CODE}/all-products if you have store codes in the url)

I know that this is not 100% clean because you need to add some code in the core. This is no problem, you can add this also in the local folder.

EDIT: I forgot to close a curly bracket at the end of the class. Thanks To John (see who John is the comments below) I fixed it.

12 comments:

  1. Thanks a lot

    Can I ask here a question, if you don't mind (english or romanian :) ). How can I generate a customer password ot store directly into customer entity varchar? I already tried this but it doesn't seem to work:

    $obj = Mage::getModel('core/encryption'); //instantiate the core encryption mode,
    $helper = Mage::helper('core'); //instantiate the core helper,
    $obj->setHelper($helper); //set the encryptions helper,
    $enc_pwd = $obj->getHash($pwd, 2); //get the password's hash

    (http://czetsuya-tech.blogspot.com/2009/08/magento-customer-password-encryption.html)

    Can you tell me pls what's wrong? Thanks. Dan.

    ReplyDelete
  2. You can generate an password using the customer model.


    $passwordLength = 10;
    $customer = Mage::getModel('customer/customer');
    $pass = $customer->generatePassword($passwordLength);


    If you want to set this password to a specific customer just do this

    $customerId = 1;//replace with your customer id
    $_customer = Mage::getModel('customer/customer')->load($customerId)->setPassword($pass);
    $customer->save()

    ReplyDelete
  3. sorry I made a mistake
    Last line should be $_customer->save();

    ReplyDelete
  4. Hi,

    thanks for this code. I'm trying to use it as a navigation. My store is quite small (10 products) so I prefer to list the products in a block instead of the categories. I did everything you mentioned here.

    When I tried to use this code I get the following error:
    Parse error: syntax error, unexpected ';', expecting T_FUNCTION in /home/domain/app/code/local/Mage/Catalog/Block/Product/All.php on line 15

    Line 15 contains the following: ?>

    Would you be able to help. I'm using Magento 1.4.x

    Look forward to your reply.

    John

    ReplyDelete
  5. Hi John,
    Sorry, I forgot to close a curly bracket right before closing the php tag (?>).
    I'll modify the post. Thanks for the feed-back.

    ReplyDelete
  6. Hi,
    also we need to add store filter:
    ->addStoreFilter()
    so only the products related to current store are selected. :) thanks helped a lot.

    Sunny

    ReplyDelete
  7. Hi,

    how to add Layered Navigation Menu to the left column with subcategories?

    DavorNo

    ReplyDelete
  8. I am trying to show all in stock product can anyone tell me what I am missing?
    _productCollection)) {
    $collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addFieldToFilter('is_salable', '1');
    $this->_productCollection = $collection;
    }
    return $this->_productCollection;
    }
    }
    ?>

    ReplyDelete
  9. I am trying to show all in stock product can anyone tell me what I am missing?
    _productCollection)) {
    $collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addFieldToFilter('is_salable', '1');
    $this->_productCollection = $collection;
    }
    return $this->_productCollection;
    }
    }
    ?>

    ReplyDelete