4

On the home page, I am trying to display70 only 6 random products on one row, from a category (id 24) that has 70 products. And randomly changed each time the page gets re-loaded.

The only method that works so far is from the following link
http://www.magikcommerce.com/blog/random-products-magento-home-page/
and using
{{block type="catalog/product_list" category_id="24" num_products="6" template="catalog/random.phtml"}} in home CMS.
Problem is that this is not responsive.

So I have been trying to use the rwd/default/template/catalog/product/list.phtml
with the following code in home cms without success.
{{block type="catalog/product_list_random" category_id="24" num_products="6" column_Count ="6" template="catalog/product/list.phtml"}}

With the above in home cms
1) it does not display products from id 24.
2) Second I can't control the the number of products it displays.
3) I can't find block type="catalog/product_list_random" any where. Is that a file?

ps. using rwd template files

2
  • 1
    The random block does exist - I'm not sure if I've ever seen it used, but it is there: github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/…
    – pspahn
    Commented Jul 16, 2015 at 18:02
  • 1
    Be very careful with the random on each page refresh - it can be very taxing on your server and could result in slow loads and/or with enough traffic, the site can become inaccessible.
    – SR_Magento
    Commented Jul 16, 2015 at 23:31

2 Answers 2

1

Looking at the block 'catalog/product_list_random` you can only set the number of products to retrieve from the store.

In order get the desired result you want you could create your own custom block that does the following:

$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$collection->addCategoryFilter($this->getCategoryId())
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
$collection->setPage(1, $numProducts);

$this->_productCollection = $collection;
1

I found a good way to do this, but there are a couple of things i wanted to go over before i do.

First, when you make a call to a block in the cms page/cms block, that is the block call you are making to either a custom block or a core block. So when you say something like:

{{block type="catalog/product_list" category_id="24" num_products="6" template="catalog/random.phtml"}}

this means you are calling in the block Mage_Catalog_Block_Product_List from the core. This means you have access to any of the methods in the block and all the methods that that block inherits from. What this means in your case is that you can construct anything in your phtml that is based on that code. But it also means that something like:

{{block type="catalog/product_list_random" category_id="24" num_products="6" column_Count ="6" template="catalog/product/list.phtml"}}

would only work with you had a class like Mage_Catalog_Block_Product_List_Random what doesn't exist. Not sure off the top of my head if you can use that same class in your custom module's class and it will be picked up correctly. Would be worth a try, but in this case you don't need it.

For your question...

I think it's much easier in M1 to just use the core template for displaying a template in a cms block and then call in the objects you need in the phtml itself. So for the call in the homepage use this:

{{block type="core/template" category_id="9" template="catalog/product/list-custom.phtml"}}

Where you will change category_id="9" to whatever category you want to highlight on the homepage (found in the admin):

enter image description here

From there you can create a phtml with the needed code at app/design/frontend/{your_namespace}/{you_theme}/template/catalog/product/list-custom.phtml.

In there, past this:

<?php
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$products = Mage::getModel('catalog/product')->getCollection();
$products->addCategoryFilter($category);
$products->addAttributeToSelect(array('name', 'thumbnail', 'price')); //feel free to add any other attribues you need.
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->getSelect()->order('RAND()');
$products->getSelect()->limit(5);
$_columnCount = 5;
$i=0;
?>
<div class="widget widget-new-products">
    <ul class="products-grid products-grid--max-<?php echo $_columnCount; ?>-col-widget">
        <?php foreach ($products as $_product) : ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <?php $_imgSize = 210; ?>
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_product->getName(); ?>" class="product-image">
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(210); ?>" width="100" height="80" alt="<?php echo $_product->getName(); ?>" />
                </a>
                <div class="product-info">
                    <h3 class="product-name">
                        <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>
                    </h3>
                    <div class="price-box">
                        <span class="regular-price">
                            <span class="price"><?php echo Mage::app()->getStore()->getCurrentCurrency()->format($_product->getFinalPrice()); ?></span>
                        </span>
                    </div>
                    <div class="ratings">
                        <div class="rating-box">
                            <?php echo $this->getReviewsSummaryHtml($_product, false, true); // Reviews ?>
                        </div>
                    </div>
                    <div class="actions">
                        <?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
                            <p><button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                        <?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
                            <p><a title="<?php echo $this->__('View Details') ?>" class="button btn-cart" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->__('View Details') ?></a></p>
                        <?php else: ?>
                            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                        <?php endif; ?>
                    </div>
                </div>
            </li>
        <?php endforeach; ?>
    </ul>
</div>

All this is based on using the rwd theme. The code will work in any theme, but the classes are set up to display with the rwd styles.

A lot of this is coming from this post, with some changes to work with what you are looking to do:

Four Random Products on Homepage

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.