0

I have created a random.phtml with the following code to list few products in the product page.

<?php        
$manufacturer = Mage::registry('current_product')->getMerchantName();

$productCollection = Mage::getModel('catalog/product')->getCollection()
                     ->addAttributeToSelect('*')
                     ->addAttributeToFilter('merchant_name',$manufacturer);
$productCollection->getSelect()->order('RAND()');
$productCollection->getSelect()->limit(5);

foreach ($productCollection as $_product) {
   echo $_product->getName();
}
?>

then created Static Block "random_product_brand" with

Other Items from this manufacturer
{{block type="core/template" name="random_manufacture_products" template="random/random.phtml"}}

then added the following block to view.phtml

<?php echo $this->getLayout()->createBlock("cms/block")->setBlockId('random_product_brand')->toHtml();?>

But it just shows the plain product name in frond end.

enter image description here

Now I need to show the product as in product list page. (Image, Name, price etc...) like this

enter image description here

Here is my list.phtml code:

<div class="products-list-grid columns<?php echo $_columnCount ?>">
<?php $i=0; foreach ($_productCollection as $_product): $i++; ?>
<div class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php include('view/labels.phtml')  ?>
<?php $mainImg = $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgSize); ?>
<?php if($hoverSwap): $backImg = $this->getLayout()->createBlock("ajax/listmedia")->setTemplate("catalog/product/list/media.phtml")->setData('size', $imgSize)->setData('product', $_product)->toHtml(); endif; ?>

<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
<?php if($backImg != 'null' && $backImg): ?><img src="<?php echo $backImg ?>" width="<?php echo $imgSize ?>" height="<?php echo $imgSize ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /><?php endif; ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgSize) ?>" <?php if($backImg != 'null' && $backImg): ?>onmouseover="hideImage(this)" onmouseout="showImage(this)"<?php endif; ?> width="<?php echo $imgSize ?>" height="<?php echo $imgSize ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
</a>

<div id="productimgover<?php echo $_product->getId()?>" style="display: none;"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(64); ?>" width="64" height="64" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></div>

<div class="moreinfo">
<h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
<div id='productname<?php echo $_product->getId()?>' style='display:none'><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></div>


<?php   
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){?>
<div>by <?php echo $merchant_name;?> </div>
<?php }

else if ($_product->getIsEbayaffiliate()) { ?>
<div>by eBay</div>
<?php }

else { ?>
<div>by Home Done</div>
<?php } ?>


<?php echo $this->getPriceHtml($_product, true) ?>
<?php if($_product->isSaleable()): ?>
<?php if ( !($_product->getTypeInstance(true)->hasRequiredOptions($_product) || $_product->isGrouped()) ) { ?>
<p>
<?php if ($product = Mage::getModel('catalog/product')->load($_product->getId())); { ?>
<?php if ($merchant_name){ ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
<button type="button" title="View Product Detail" class="button btn-cart"><span><span>View Product Detail</span></span> </button>
</a>
<?php } else if ($product->getIsEbayaffiliate()) { ?>
<a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
<button type="button" title="View Product Detail" class="button btn-cart"><span><span>View Product Detail</span></span> </button>
</a>
<?php } else {  ?>

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="<?php if($enableAjax): ?>setLocationAjax<?php else: ?>setLocation<?php endif; ?>('<?php echo $this->getAddToCartUrl($_product) ?>','<?php echo $_product->getId()?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php } ?>
<?php } ?>
</p>

<?php } else { ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="<?php if($enableAjax): ?>showOptions('<?php echo $_product->getId()?>')<?php else: ?>setLocation('<?php echo $this->getAddToCartUrl($_product) ?>','<?php echo $_product->getId()?>')<?php endif;?>"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<a href='<?php echo $this->getUrl('ajax/index/options',array('product_id'=>$_product->getId()));?>' class='fancybox' id='fancybox<?php echo $_product->getId()?>' style="display: none;" ><?php echo $this->__('Add to Cart') ?></a>
<?php }  ?>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<div class="clear"></div>
<?php if(themeOptions('addto')): ?>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li>+ <a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li>+ <span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
<?php endif; ?>
<div class="clear"></div>
</div>
</div>
<?php endforeach ?>

any help Would be appreciated.

0

For this case,you need to create a custom block class by

  • Creating new extension and create a new block for your collection
  • or Create Block class file Random.php at app/code/local/Mage/Catalog/Block/

Then at new block class write your code.

For my point of view,First copy code from app/code/core/Mage/Catalog/Block/Product/New.php

and then that class(Mage_Catalog_Block_Random) on _getProductCollection function change your collection code.

Random.php may be:

<?php
class Mage_Catalog_Block_Random extends Mage_Catalog_Block_Product_Abstract
{
    /**
     * Default value for products count that will be shown
     */
    const DEFAULT_PRODUCTS_COUNT = 10;

    /**
     * Products count
     *
     * @var null
     */
    protected $_productsCount;

    /**
     * Initialize block's cache
     */
    protected function _construct()
    {
        parent::_construct();

        $this->addColumnCountLayoutDepend('empty', 6)
            ->addColumnCountLayoutDepend('one_column', 5)
            ->addColumnCountLayoutDepend('two_columns_left', 4)
            ->addColumnCountLayoutDepend('two_columns_right', 4)
            ->addColumnCountLayoutDepend('three_columns', 3);

        $this->addData(array('cache_lifetime' => 86400));
        $this->addCacheTag(Mage_Catalog_Model_Product::CACHE_TAG);
    }

    /**
     * Get Key pieces for caching block content
     *
     * @return array
     */
    public function getCacheKeyInfo()
    {
        return array(
           'CATALOG_PRODUCT_NEW',
           Mage::app()->getStore()->getId(),
           Mage::getDesign()->getPackageName(),
           Mage::getDesign()->getTheme('template'),
           Mage::getSingleton('customer/session')->getCustomerGroupId(),
           'template' => $this->getTemplate(),
           $this->getProductsCount()
        );
    }

    /**
     * Prepare and return product collection
     *
     * @return Mage_Catalog_Model_Resource_Product_Collection|Object|Varien_Data_Collection
     */
    protected function _getProductCollection()
    {
        if(Mage::registry('current_product')){
        $manufacturer = Mage::registry('current_product')->getMerchantName();


        /** @var $collection Mage_Catalog_Model_Resource_Product_Collection */
        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());


        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter('merchant_name',$manufacturer)

        $collection->getSelect()->order('RAND()');
        $collection->getSelect()->limit(5);  
        ;
        }else{
        return false;
        }


        return $collection;
    }

    /**
     * Prepare collection with new products
     *
     * @return Mage_Core_Block_Abstract
     */
    protected function _beforeToHtml()
    {
        $this->setProductCollection($this->_getProductCollection());
        return parent::_beforeToHtml();
    }

    /**
     * Set how much product should be displayed at once.
     *
     * @param $count
     * @return Mage_Catalog_Block_Product_New
     */
    public function setProductsCount($count)
    {
        $this->_productsCount = $count;
        return $this;
    }

    /**
     * Get how much products should be displayed at once.
     *
     * @return int
     */
    public function getProductsCount()
    {
        if (null === $this->_productsCount) {
            $this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
        }
        return $this->_productsCount;
    }
}

edit:

Change block type from core/template to catalog/randon

so

{{block type="core/template" name="random_manufacture_products" template="random/random.phtml"}}

should be

{{block type="catalog/random" name="random_manufacture_products" template="random/random.phtml"}}

Also,no need of use

$manufacturer = Mage::registry('current_product')->getMerchantName();

$productCollection = Mage::getModel('catalog/product')->getCollection()
                     ->addAttributeToSelect('*')
                     ->addAttributeToFilter('merchant_name',$manufacturer);
$productCollection->getSelect()->order('RAND()');
$productCollection->getSelect()->limit(5);

foreach ($productCollection as $_product) {
   echo $_product->getName();
} 

at random.phtml and make $_productCollection=$this->getProductCollection() first of this file file

| improve this answer | |
  • I have edited my query. Can you plz check this? – Doodu Apr 1 '16 at 8:53

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.