Display Promotion & Random Products in Magento

Featured Image

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 …

66
Top

Care to rate this post?

Author

Ivan Weiler

Technical Educator and Consultant

Ivan is a Technical Educator and Consultant. He gained lots of experience managing some of the most complex Magento projects we had at Inchoo.

Other posts from this author

Discussion 66 Comments

1 2
Add Comment
  1. 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

  2. David

    Emilio, in 1.5 you should put this

    34

    in Design / XML Layout

  3. imran

    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……

  4. Shashank

    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 :)

  5. @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=”"

  6. 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:

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

    This code displays a single product.

  7. 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

  8. bruno

    hi,
    and for the new produts how i display them randomly??

  9. Gaurav

    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

  10. 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

  11. 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?

  12. 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?

  13. Liz

    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.

  14. Liz

    oops, here is the code:

    &lt;action method=&quot;setProductsCount&quot;&gt;&lt;count&gt;3&lt;/count&gt;&lt;/action&gt;
  15. Shivam

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

  16. Mr Fieldz

    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

1 2

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top