Display Promotion & Random Products in Magento

Display Promotion & Random Products in Magento

There are two unused product list blocks in Magento which can be very useful if you push a few buttons, edit few layouts ..

1. Promotion
Block located in app\code\core\Mage\Catalog\Block\Product\List\Promotion.php

This is basically built in featured product functionality. It reacts to “promotion” attribute which needs to be created, so let’s click
Catalog->Attributes->Manage Attributes->Create New Attribute

Attribute Code: promotion
Scope: Global
Catalog Input Type for Store Owner: Yes/No

promotion-and-random-1

Label: Promotion (second tab)

Other params can be left alone, but it’s up to you of course. I also labeled it Promotion just for this article.

Now we need to add created attribute to attribute set, so navigate to
Catalog->Attributes->Manage Attribute Sets
select attribute set you’re using and drag promotion to General group for example.

promotion-and-random-2

Now when you’re editing your products, there is new “Promotion” option under General tab.

promotion-and-random-3

Products on which you select Yes will be shown on promotion block which can be displayed through layouts with

<block type="catalog/product_list_promotion" name="product_promotion" template="catalog/product/list.phtml"/>

or as cms content with

{{block type='catalog/product_list_promotion' template='catalog/product/list.phtml'}}

This attribute should really be included in default attribute set or in Magento sample data.

2. Random
Block located in app\code\core\Mage\Catalog\Block\Product\List\Random.php

This block loads random product collection from current store.

The fastest way to display it would also be something like

<block type="catalog/product_list_random" name="product_random" template="catalog/product/list.phtml"/>

since it also extends product_list block, however, since it is random product listing, that toolbar has no purpose here, so create phtml that will suit your shop needs, based on catalog/product/list.phtml. For example, i’m using similar Random block to display random products in sidebar.

So long, take care, i’m off …

You made it all the way down here so you must have enjoyed this post! You may also like:

Adding gallery tab to a CMS page Antun Martinovic
Antun Martinovic, | 5

Adding gallery tab to a CMS page

Implementing javascript minifier Tomislav Nikcevski
Tomislav Nikcevski, | 5

Implementing javascript minifier

How to set up a CDN (Amazon CloudFront) in Magento Dario Trbovic
Dario Trbovic, | 18

How to set up a CDN (Amazon CloudFront) in Magento

69 comments

  1. i got this error, any ideas?

    Fatal error: Call to a member function getSource() on a non-object in /home/camposne/public_html/demo/app/code/core/Mage/Catalog/Model/Product.php on line 1389

  2. hello,
    I use magento 1.7.0.2
    i want to display random products in catalog page of a category.
    please help

  3. Hi Ivan,

    This is really helpful. I have added a block that lists random products. I have tried to limit the number of products displaying in this list to 3 using the line below but it doesnt work. Any ideas?

    3

    Thanks again.

  4. Hello
    I work with magento 1.7.0.2 and random products in my home.
    I need help placing 4 columns in the products randon.
    Any tips?

  5. I coming back because I’ve succed to show Random Product from my catgory but not on my selected promotion product. I’d like to show Random Product from my selected promotion from my catégory:-) product_list_promotion or product_list_random but together that doesnt work. An tips for that?

  6. Thanks for the blog – I have a problem with Random. When I select “No” the product is always showed on my homepage. I know why!
    My question is, how does it possible to show Random Product only selected with my attribute ?
    Thanks for helping us

  7. Hi,
    Nice clue to show Random products into home page, As i do the same and it works great! thanks for tip.

    Here, My question is, Why it didn’t show the pagination? It seems it used “list.phtml” to show all the products but here pagination (first page, next,last page) is missing.

    And tips for that?

    thanks

  8. Hi, promotion products is working fine but…
    I have 4 stores and would love to show promotion products for each store specific. Right now its showing all products with the promotion atribute.
    Can someone tell me how to do that?
    Thanks

  9. Thank you for the blog!
    Probably following code will help someone to show products randomly from a particular category:

    We need to write code for home cms like:
    {{block type=”catalog/product_list” category_id=”50″ template=”catalog/product/list_home.phtml”}}

    Code:

    <?php
    $_productCollection=$this->getLoadedProductCollection();
    $categoryID = $this->category_id;
    //$categoryID = $this->getCategoryId();
    if($categoryID){
      $category = new Mage_Catalog_Model_Category();
      $category->load($categoryID); // this is category id
      $collection = $category->getProductCollection();
    } else{
      $collection = Mage::getResourceModel('catalog/product_collection');
    }
    Mage::getModel('catalog/layer')->prepareProductCollection($collection);
    $collection->getSelect()->order('rand()');
    $collection->addStoreFilter();
    $numProducts = $this->getNumProducts() ? $this->getNumProducts() : 1;
    $collection->setPage(1, $numProducts)->load();
    $_productCollection = $this->_productCollection = $collection;
    ?>
    <?php if(!$_productCollection->count()): ?>
    <div class="note-msg">
        <?php echo $this->__('There are no products matching the selection. Please provide a category ID.') ?>
    </div>
    <?php else: ?>
      <ul>
    	<?php $_collectionSize = $_productCollection->count() ?>
        <?php foreach ($_productCollection as $_product): ?>		        
    		<li>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(105, 105); ?>" width="105" height="105" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
            </a>
    		</li>
        <?php endforeach ?>
    </ul>
    <?php endif; ?>

    This code displays a single product.

  10. @Shashank
    catalog.xml
    -Category default layout

    u can create a new list.phtml and make it show only 10 products

    as for the category use “category_id=””

  11. Am a regular reader of your blog but am commenting for the 1st time..I want to add 10 random products from a specific category lets say “Popular” below my main product on the product view page..am struggling for the last three days…any help wud be greatly appreciated…am using latest Magento lite version that u released as an experiment before 🙂

  12. Hi! I want to set a promotional offer for customer’s depending on there “Date of Birth’s” how can i do that & in my magento its not displaying any of the condition with respect to the Customer. please help me for doing this……

  13. I use magento 1.5. and I want to show random products on home page. Could you please tell where on the home page information I have to put the code? on the “Custom Layout Update XML” or on the end of “Layout Update XML”?
    Best regards

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.