magento template for block with 2 columns - magento

I made a small magento (1.7) template file that displays a all products in a category. However they only display in a single column. I would like to display in 2 columns.
This executes the block from front page:
{{block type="catalog/product" name="msc.specials" template="mylib/featuredlist.phtml"}}
This is featuredlist.phtml -
<?php
//$_categoryId = $this->getCategoryId();
$productCollection = Mage::getModel('catalog/category')->load(4)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->setOrder('price', 'ASC');
$cartHelper = Mage::helper('checkout/cart');
?>
<div class='block block-list'>
<div class='block-title'><strong><span><?php echo $this->__('SPECIALS') ?></span></strong></div>
<div class='block-content'>
<ul>
<h2><?php echo $this->__( $this->getLabel() ); ?></h2>
<?php foreach ($productCollection as $product): ?>
<div class="item">
<a class="product-image" href="<?php echo $product->getProductUrl() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" />
</a>
<a class="product-name" href="<?php echo $product->getProductUrl() ?>"><?php echo $this->htmlEscape($product->getName()) ?></a>
<?php echo $this->getPriceHtml($product, true) ?>
</div>
<div class="cms-price-box" style=" text-align:center;"></div>
<div class="button-row" style="text-align:center;">
<button class="button btn-cart" type="button" onclick="setLocation('<?php echo $this->getUrl('')."checkout/cart/add?product=".$product->getId()."&qty=1" ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button>
</div>
<br/><br/>
<?php endforeach ?>
</ul>
</div>
</div>

This is actually going to be a bit complicated. Maybe I am over reaching but you will need to do a couple things. Its making it a single item column because it is doing a foreach and just spitting each item out. To make it do two columns you will need to count the items in your list and divide by two. From there you could use some iterations to split it into two arrays and then do a foreach for both resulting arrays.
For each array you will want it in a div with css stylings for left or right columns. Additionally you should decide if you need items split in the middle, the first part goes in column 1 and the latter part goes into column 2. Or you may prefer to have odd items go in column 1 and even items go in column 2.
If you are looking for a more detailed answer I can give you one, however it is not a simple one. Will definitely take a little recoding if you are comfortable with that.
Here is a overview of what you could do: How to display two table columns per row in php loop

Here is a solution based on Nate's suggestion and reference. Would be interested in other ways of doing the same
<?php
$_categoryId = $this->getCategoryId();
$productCollection = Mage::getModel('catalog/category')->load($_categoryId)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->setOrder('price', 'ASC');
$cartHelper = Mage::helper('checkout/cart');
$x = 1;
?>
<div class='block block-list'>
<div class='block-title'><strong><span><?php echo $this->__('FEATURED PRODUCTS') ?></span></strong></div>
<div class='block-content'>
<ul>
<h2><?php echo $this->__( $this->getLabel() ); ?></h2>
<table>
<tr>
<?php foreach ($productCollection as $product): ?>
<?php
$x++;
if (($x % 2) == 0){
echo "</tr><tr>";
}
?>
<td>
<div class="item">
<a class="product-image" href="<?php echo $product->getProductUrl() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(180); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" />
</a>
<br/>
<a class="product-name" href="<?php echo $product->getProductUrl() ?>"><?php echo $this->htmlEscape($product->getName()) ?></a>
<?php echo $this->getPriceHtml($product, true) ?>
</div>
<div class="cms-price-box" style=" text-align:center;"></div>
<div class="button-row" style="text-align:center;">
<button class="button btn-cart" type="button" onclick="setLocation('<?php echo $this->getUrl('')."checkout/cart/add?product=".$product->getId()."&qty=1" ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button>
</div>
</td>
<?php endforeach ?>
</tr>
</table>
</ul>
</div>
</div>

Related

Magento: Show recently added products homepage

I want show the last 8 products that I've added on the homepage. How can I do that?
Using this code snippet for Block File form the previous answer will give a more well structured collection, because:
Filter product by status Enabled
Filter product by visibility (simple products of configurable (which are by default 'Not Visible Individually') will not show in this collection)
$_productCollection = Mage::getModel("catalog/product")
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter(
'status',
array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
);
<b> To show recently added products,</b>
1. Create Block file and template file (phtml)
2. To show in homepage go to admin panel. Click on CMS->Pages->Homepage(Homepage will be set based on your theme). Inside that Click on Design tab and add the below code:
<reference name="content">
<block type="rileytheme/recentproducts" name="recentproducts_recentproducts" template="recentproducts/recentproducts.phtml"></block>
<block type="cms/block" name="myelement"><action method="setBlockId"<block_id>homepage_block</block_id></action></block>
</reference>
Block File:
//class Namespace_Module_Block_Filename
class MGS_Rileytheme_Block_Recentproducts extends Mage_Core_Block_Template {
public function getRecentProducts() { <br/>
$products = Mage::getModel("catalog/product")
->getCollection()
->addAttributeToSelect('*')
->setOrder('entity_id', 'DESC')
->setPageSize(5); //set page size as your wish
return $products;
}
View File (phtml):
<?php $products = $this->getRecentProducts(); ?>
<?php shuffle($products); ?>
<div class="container">
<div class="box recently" style="padding-left:15px; padding-right:15px;">
<h1 class="text-center fw400 text-red"><?php echo $this->__('Recent Products') ?></h3>
<div class="listing-type-grid catalog-listing">
<?php $_collectionSize = count($products) ?>
<?php $i=0; foreach ($products as $_res): ?>
<?php $_product = Mage::getModel('catalog/product')->load($_res->getId()); ?>
<?php if ($i++%3==0): ?><tr><?php endif ?>
<div class="col-xs-12 col-sm-6 col-md-3 prod-list">
<div class="grid_list">
<div class="product">
<div class="image-container">
<?php if($_product->getProductLabel()): ?>
<a href="<?php echo $_product->getProductUrl() ?>">
<span class="onsale <?php echo strtolower($_product->getAttributeText('product_label')) ?>"><?php echo $_product->getAttributeText('product_label') ?></span>
</a>
<?php endif ?>
<img alt="" class="img-responsive" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(800,800); ?>">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->__('View detail')?>" class="icon-left icon-top ">
<i class="fa fa-eye"></i>
</a>
<a href="<?php echo $_product->getProductUrl()?>" title="<?php echo $this->__('Add to Cart')?>" class="icon-right icon-top">
<i class="fa fa-shopping-cart"></i>
</a>
<?php if($this->helper('wishlist')->isAllow()): ?>
<!--<a class="icon-left ves-boxcolor icon-bottom" href="<?php echo $this->helper('wishlist')->getAddUrl($_product)?>" title="<?php echo $this->__('Add to wishlist')?>">
<i class="fa fa-heart"></i>
</a>-->
<?php endif;?>
<?php if($this->getAddToCompareUrl($_product)): ?>
<!--<a class="icon-right boxcolor icon-bottom" href="<?php echo $this->getAddToCompareUrl($_product)?>" title="<?php echo $this->__('Add to compare')?>">
<i class="fa fa-retweet"></i>
</a>-->
<?php endif;?>
</div>
<!--<div class="media-productlist">
<a href="<?php // echo $_product->getProductUrl() ?>" title="<?php // echo $this->htmlEscape($_product->getName()) ?>">
<img class="product-image" src="<?php // echo $this->helper('catalog/image')->init($_product, 'small_image');?>" alt="<?php // echo $this->htmlEscape($_product->getName()) ?>" />
</a>
</div>-->
<div class="product-list-data">
<a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $this->htmlEscape($_product->getName()) ?></a>
<div class="product-list-data-inner">
<div class="cart-item-price">
<?php $_product->getPrice();?>
<span class="price"><?php echo $_formattedActualPrice = Mage::helper('core')->currency($_product->getPrice(),true,false);?></span>
</div>
<div class="cart-item-stars">
<div class="rating-shop-item">
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product) ?>
<?php else:?>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
<?php endif; ?>
</div>
<div class="space10"></div>
</div>
<div class="book-now-btn">
<button onclick="setLocation('<?php echo $_product->getProductUrl() ?>')" title="<?php echo $this->__('Book Now') ?>" type="button" class="btn btn-primary btn-sm"><?php echo $this->__('Book Now') ?></button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php if ($i%3==0 && $i!=$_collectionSize): ?></tr><?php endif ?>
<?php endforeach ?>
<?php for($i;$i%3!=0;$i++): ?>
<td class="empty-product"> </td>
<?php endfor ?>
<?php if ($i%3==0): ?> <?php endif ?>
</div>
</div>
</div>

insert a item in to masnory jquery

In a products listing page of magento, i used the masonry and it is working good. But now i want to add a div having some text in the masonry area. I tried this and i get the result, but it break masonry item. My used code...
<?php if ($category_nameplace == round($_collectionSize/2)): ?>
<div class="item">
<h2 class="productcategory-name"><?php echo $this->getLayer()->getCurrentCategory()->getName() ?></h2>
</div>
<?php endif; ?>
This above code is used for insert extra item to the masonry area. In the above code i count the items number and as i got the middle number i insert my div in to bellow written area
<div id="basic" class="products-grid unstyled thubmnails js-masonry">
<?php $category_nameplace = 0; foreach ($_productCollection as $_product): ?>
<?php $category_nameplace++; ?>
<div class="item">
<div class="thumbnail">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(rand(135,400)); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<div class="caption">
<h2 class="product-name product-info"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<h2 class="product-info"><?php echo "Dimension:".$_product->getDimension(); ?></h2>
<h2 class="product-info"><?php echo $_product->getPrice(); ?></h2>
</div>
</div>
</div>
<?php endforeach ?>
</div>

I can't get magento random product list images and data correctly

my problem is, that i can get correct product url, but it doesn't get product name, image, price and description.
i have 6 item random product block like this:
http://www.upload.ee/image/3374325/magento.png
all the six items are clickable and they have correctly random products url's behind. Just can't figure out why image, price and name won't apper.
current code:
<?php $_productCollection = Mage::getModel('catalog/product')->getCollection(); ?>
<?php if(!$_productCollection->count()): ?>
<div class="padder">
<div class="note-msg">
<?php echo $this->__('There are no products matching the selection.') ?>
</div>
</div>
<?php else: ?>
<div class="listing-type-cell catalog-listing padder" style="margin-bottom:-20px;margin-top:10px;border-top: 1px solid rgba(221,221,221,0.53);"> <!-- the class name will change to .listing-type-grid if viewing in grid mode -->
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_items = $_productCollection->getItems();
shuffle($_items); ?>
<h2 style="margin:20px 0;">Seda toodet vaadanud inimesed vaatasid ka neid tooteid</h2>
<table cellspacing="0" class="products-grid" id="product-grid-table">
<?php $i=0; foreach ($_items as $_product): ?>
<?php if ($i++%6==0): ?>
<tr height="290">
<?php endif ?>
<td class="random_item" style="width:150px;padding-right:10px;">
<a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_product->getName() ?>">
<img src="<?php echo $_product->getImageUrl(); ?>" width="144" height="216" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
<h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></h3>
<?php echo $this->getPriceHtml($_product, true) ?>
</a>
</td>
<?php if ($i%12==0 && $i!=$_collectionSize): ?>
</tr>
<?php endif ?>
<?php if ($i==6) break; // show 6 products max ?>
<?php endforeach ?>
<?php for($i;$i%12!=0;$i++): ?>
<td class="empty-product"> </td>
<?php endfor ?>
<?php if ($i%12==0): ?>
</tr>
<?php endif ?>
</table>
<script type="text/javascript">decorateTable('product-grid-table')</script>
</div>
<?php endif; ?>
See this question for adding name and price: retrieve product attributes in collection and this thread for images: load the media_gallery in a product collection

How to limit amount of product thumbnail images in Magento product page

In Magento, does anyone know how to limit the amount of thumbnails to be shown under the main product image?
Is this something that is easily controlled via the admin or should I go into media.phtml and edit the php?
<div class="more-views">
<ul>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" title="<?php echo $_product->getName();?>" onclick="$('image').src = this.href; return false;">
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(103, 103); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
The quickest way would be
<div class="more-views">
<ul>
<?php $limit = 5; ?>
<?php $ct = 0; ?>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" title="<?php echo $_product->getName();?>" onclick="$('image').src = this.href; return false;">
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(103, 103); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/>
</a>
</li>
<?php
if(++$ct >= $limit)
break;
?>
<?php endforeach; ?>
</ul>
</div>
Yes, you can easily control via the admin, which images you want to show under the main product image, but you need to set for all the products individually.
Simply go to images tabs of add/edit product. click on the exclude checkbox which you don't want to show on image gallery and then hit the save button.

Magento getProductCollection not working.

Here is my Magento code to get all new products but it has suddenly stopped working. I added some new categories and then when I saw frontend New Arrivals section was empty. Any help would be appreciated.
Here is my code:
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="new-products">
<div class="title_index">
<!-- <div class="pagination_carousel" id="foo3_pag"></div>-->
<h2><?php echo $this->__('New Arrivals') ?></h2>
</div>
<div class="index_cont">
<div class="list_carousel">
<ul id="user_interaction">
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
<li><h3 class="product-name"><?php echo $this->htmlEscape($_product->getName()) ?></h3>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(220, 200) ?>" width="150" height="130" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
<div class="descr"><?php echo($_product->getSku()); ?></div>
<div class="new_price"><?php echo $this->getPriceHtml($_product, true, '-new') ?></div>
<div class="new_pr_btn"><input value="1" type="text" name="qty" class="my-qty-box" /><button type="button" title="<?php echo $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></div>
</li>
<?php endforeach; ?>
</ul>
<div class="clear"></div>
</div>
</div>
<div class="shadow-div"></div>
</div>
<?php endif; ?>
Thanks
Dani
$sql=$this->getProductCollection()->getSelect(); place this code at top of your phtml.
echo $sql;
This will show you sql qquery.
Now fire this query directly in phpMyAdmin or miniadmin and check whether you have records for this query or not.
If not diagnose this sql query, think of filters that resulted in some conditions this will lead you to a fix.
Else you paste sql query here for further comments.
Note : if flat catalog is on code will check flat catalog table.So check if these tables do have all products opulated.If not run indexing.

Resources