Product Attributes Not Loading - php

I'm adding a "Most Viewed" section to our website.
While the Product URL is loading correctly, all other attributes (image, product name, price, etc) are not.
Any idea what I might be doing wrong here?
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class=" most_viewed">
<?php $_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output'); ?>
<?php $_collectionSize = 5;//count($_products->getItems()); echo $_collectionSize; ?>
<?php $_columnCount = 4; ?>
<div class="category-products">
<ul class="products-grid">
<?php $i=1; foreach ($_products->getItems() as $_product): ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php echo $this->getPriceHtml($_product, true) ?>
<div class="actions">
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $_product->getProductUrl() ?>')"><span><span><?php echo $this->__('Details') ?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</li>
<?php $i++; endforeach; $kol = $_collectionSize; ?>
</ul>
</div>
</div>
<?php endif; ?>

when you create attribute then
1)make select display list page yes
2)assign to product group
3)provide some value
4)reindexing
5) add in phtml where you want to display , your case might be ( echo $_product->getMostViewed() ).

Related

Display random products on product page of magento every time i refresh the page

I want to Display random products on product page of magento every time i refresh the page.
I have tried finding the answer on google and some of the solutions were to change my list.phtml code.
i have tried replacing
$_productCollection=$this->getLoadedProductCollection();
with
$_productCollection=shuffle($this->getLoadedProductCollection());
or adding
$items = $_productCollection->getitems;
$item = shuffle($items);
but its not working, Even if shuffle() would work on collections (it doesn't), this would only show the same products in a different order. i want my products which are on the later page to come forward.
Here is my list.phtml code:
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
$gallery_helper = $this->helper('productlistgallery/data');
// Chech if Tm_SpecialPriceCountdown module is installed
if (Mage::helper('core')->isModuleEnabled('Tm_SpecialPriceCountdown')) {
$spcd_helper = $this->helper('specialpricecountdown/data');
$currDate = $spcd_helper->currDate();
}
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php echo $this->getToolbarHtml() ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ul class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
<li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>" itemscope itemtype="http://schema.org/product">
<?php // Product Image ?>
<div class="product-image-container">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image" itemprop="url" style="width:<?php echo $gallery_helper->getListData('image_width') + 20; ?>px;">
<img id="product-collection-image-<?php echo $_product->getId(); ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($gallery_helper->getListData('image_width'), $gallery_helper->getListData('image_height')); ?>" width="<?php echo $gallery_helper->getListData('image_width'); ?>" height="<?php echo $gallery_helper->getListData('image_height'); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" itemprop="image" />
</a>
<?php
if($gallery_helper->getListData('active')):
?>
<ul class="product-thumbs">
<?php
$count_thumbs = 1;
foreach ($_product->getMediaGalleryImages() as $image):
if($count_thumbs <= 3):
?>
<li class="product-thumb">
<a href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image', $image->getFile())->resize($gallery_helper->getListData('image_width')); ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $image->getFile())->resize($gallery_helper->getListData('thumb_size_w'), $gallery_helper->getListData('thumb_size_h')); ?>" width="<?php echo $gallery_helper->getListData('thumb_size_w'); ?>" height="<?php echo $gallery_helper->getListData('thumb_size_h'); ?>" alt="" />
</a>
</li>
<?php endif ?>
<?php $count_thumbs++; ?>
<?php endforeach ?>
</ul>
<?php endif ?>
</div>
<?php // Product description ?>
<div class="product-shop">
<div class="f-fix">
<div class="product-primary">
<?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
<h2 class="product-name" itemprop="name"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product) ?>
<?php endif; ?>
<?php
// Provides extra blocks on which to hang some features for products in the list
// Features providing UI elements targeting this block will display directly below the product name
if ($this->getChild('name.after')) {
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach ($_nameAfterChildren as $_nameAfterChildName) {
$_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
$_nameAfterChild->setProduct($_product);
echo $_nameAfterChild->toHtml();
}
}
?>
<div class="desc std">
<div itemprop="description"><?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?></div>
<?php echo $this->__('Learn More') ?>
</div>
</div>
<div class="product-secondary">
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
<p class="actions">
<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>
</p>
<?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
<p class="actions"><a title="<?php echo $this->__('View Details') ?>" class="button btn-details" href="<?php echo $_product->getProductUrl() ?>"><span><span><?php echo $this->__('View Details') ?></span></span></a></p>
<?php else: ?>
<p class="actions availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a title="<?php echo $this->__('Add to Wishlist') ?>" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist tooltips"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span class="separator">|</span> <a title="<?php echo $this->__('Add to Compare') ?>" href="<?php echo $_compareUrl ?>" class="link-compare tooltips"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</div>
<div class="clear"></div>
</div>
</div>
<div class="label-product">
<?php if($_product->getNew()){echo '<span class="new">'.$this->__('New').'</span>'; }?>
<?php if($_product->getSale()){echo '<span class="sale">'.$this->__('Sale').'</span>'; }?>
</div>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
<?php else: ?>
<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php //$_columnCount = $this->getColumnCount(); ?>
<?php $_columnCount = 3; ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php
$_productId = $_product->getId();
if ($i++%$_columnCount==0): ?>
<ul class="products-grid row">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?> col-xs-12 col-sm-4" itemscope itemtype="http://schema.org/product">
<div class="wrapper-hover">
<div class="product-image-container">
<?php if ($gallery_helper->getGridData('type_hover') == 'carusel_images'): ?>
<?php include("include_gallery.phtml"); ?>
<?php else: ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image" itemprop="url">
<img id="product-collection-image-<?php echo $_productId; ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($gallery_helper->getGridData('image_width'), $gallery_helper->getGridData('image_height')); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" width="<?php echo $gallery_helper->getGridData('image_width'); ?>" height="<?php echo $gallery_helper->getGridData('image_height'); ?>" itemprop="image" />
</a>
<?php endif ?>
<?php if($gallery_helper->getGridData('type_hover') == 'thumbnails'): ?>
<ul class="product-thumbs">
<?php $count_thumbs = 1; foreach ($_product->getMediaGalleryImages() as $image): ?>
<?php if($count_thumbs <= 3): ?>
<li class="product-thumb">
<a href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image', $image->getFile())->resize($gallery_helper->getGridData('image_width')); ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $image->getFile())->resize($gallery_helper->getGridData('thumb_size_w'), $gallery_helper->getGridData('thumb_size_h')); ?>" width="<?php echo $gallery_helper->getGridData('thumb_size_w'); ?>" height="<?php echo $gallery_helper->getGridData('thumb_size_h'); ?>" alt="" />
</a>
</li>
<?php endif; ?>
<?php $count_thumbs++; ?>
<?php endforeach ?>
</ul>
<?php endif ?>
</div>
<div class="product-details">
<h2 class="product-name" itemprop="name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<h3 style="font-size:14px;text-transform:capitalize;"><?php echo $this->htmlEscape($_product->getData('artistname')); ?></h3>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if(isset($spcd_helper) && $spcd_helper->isEnabledGridList()): ?>
<?php
$setDate = $spcd_helper->countdownTime($_product);
if ($setDate > 0):
if ($currDate < $setDate): ?>
<div id="special_countdown_<?php echo $_product->getId(); ?>" class="special_countdown"></div>
<script type="text/javascript">
setCountdown("<?php echo $setDate; ?>", "<?php echo $_product->getId(); ?>");
</script>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php
// Provides extra bloc->keepFrame(false)s on which to hang some features for products in the list
// Features providing UI elements targeting this block will display directly below the product name
if ($this->getChild('name.after')) {
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach ($_nameAfterChildren as $_nameAfterChildName) {
$_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
$_nameAfterChild->setProduct($_product);
echo $_nameAfterChild->toHtml();
}
}
?>
<div class="wrapper-hover-hiden">
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<div class="actions">
<?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
<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>
<?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
<a title="<?php echo $this->__('View Details') ?>" class="button btn-details" href="<?php echo $_product->getProductUrl() ?>"><span><span><?php echo $this->__('View Details') ?></span></span></a>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><?php echo $this->__('Add to Wishlist') ?></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<?php endif; ?>
</ul>
</div>
</div>
</div>
<div class="label-product">
<?php if($_product->getNew()){echo '<span class="new">'.$this->__('New').'</span>'; }?>
<?php if($_product->getSale()){echo '<span class="sale">'.$this->__('Sale').'</span>'; }?>
</div>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endif; ?>
<div class="toolbar-bottom">
<?php echo $this->getToolbarHtml() ?>
</div>
</div>
<?php endif; ?>
<?php
// Provides a block where additional page components may be attached, primarily good for in-page JavaScript
if ($this->getChild('after')) {
$_afterChildren = $this->getChild('after')->getSortedChildren();
foreach ($_afterChildren as $_afterChildName) {
$_afterChild = $this->getChild('after')->getChild($_afterChildName);
//set product collection on after blocks
$_afterChild->setProductCollection($_productCollection);
echo $_afterChild->toHtml();
}
}
?>
First Override your list.phtml file in your custom theme.
Add this code in your list.phtml file.
$productArray = iterator_to_array($_productCollection);
shuffle($productArray);
and add this $productArray in foreach loop.
<?php foreach ($productArray as $_product): ?>
cacheable="false" in your catalog_category_view.xml file.
<block class="Magento\Catalog\Block\Product\ListProduct"
name="category.products.list"
as="product_list"
template="Magento_Catalog::product/list.phtml"
cacheable="false"
>
The collection is an iterator and not an array, array functions do not work with iterators. but you can convert it.
Try this:-
$productCollection=$this->getLoadedProductCollection();
$productArray = iterator_to_array($productCollection);
$items = shuffle($productArray);

how to change add to cart button to view button in list of products section

I am building an ecommerce website named bookslab.in. I want to change add to cart button to view details button when users see list of products. Whenever i click on all products, then list of products will come with add to cart button i want to change that button to view details button & when users click on this button then they should go on details page of products.
I sending the view.phtml code:
<?php
?>
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<script type="text/javascript">
var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
</script>
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->toHtml() ?></div>
<div class="product-view">
<div class="product-essential">
<form action="<?php echo $this->getSubmitUrl($_product, array('_secure' => $this->_isSecure())) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<?php echo $this->getBlockHtml('formkey') ?>
<div class="no-display">
<input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
<input type="hidden" name="related_product" id="related-products-field" value="" />
</div>
<div class="product-img-box">
<div class="product-name">
<h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
</div>
<?php echo $this->getChildHtml('media') ?>
</div>
<div class="product-shop">
<div class="product-name">
<span class="h1"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></span>
</div>
<div class="price-info">
<?php echo $this->getPriceHtml($_product); ?>
<?php echo $this->getChildHtml('bundle_prices') ?>
<?php echo $this->getTierPriceHtml() ?>
</div>
<div class="extra-info">
<?php echo $this->getReviewsSummaryHtml($_product, 'default', false)?>
<?php echo $this->getChildHtml('product_type_availability'); ?>
</div>
<?php echo $this->getChildHtml('alert_urls') ?>
<?php if ($_product->getShortDescription()):?>
<div class="short-description">
<div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
</div>
<?php endif;?>
<?php echo $this->getChildHtml('other');?>
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container1', '', true, true) ?>
<?php endif;?>
</div>
<div class="add-to-cart-wrapper">
<?php echo $this->getChildHtml('product_type_data') ?>
<?php echo $this->getChildHtml('extrahint') ?>
<?php if (!$this->hasOptions()):?>
<div class="add-to-box">
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
<span class="or"><?php echo $this->__('OR') ?></span>
<?php endif; ?>
<?php endif; ?>
<?php echo $this->getChildHtml('addto') ?>
<?php echo $this->getChildHtml('sharing') ?>
</div>
<?php echo $this->getChildHtml('extra_buttons') ?>
<?php elseif (!$_product->isSaleable()): ?>
<div class="add-to-box">
<?php echo $this->getChildHtml('addto') ?>
<?php echo $this->getChildHtml('sharing') ?>
</div>
<?php endif; ?>
</div>
<?php echo $this->getChildHtml('related_products') ?>
<div class="clearer"></div>
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container2', '', true, true) ?>
<?php endif;?>
</form>
<script type="text/javascript">
//<![CDATA[
var productAddToCartForm = new VarienForm('product_addtocart_form');
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
try {
this.form.submit();
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
if (button && button != 'undefined') {
button.disabled = true;
}
}
}.bind(productAddToCartForm);
productAddToCartForm.submitLight = function(button, url){
if(this.validator) {
var nv = Validation.methods;
delete Validation.methods['required-entry'];
delete Validation.methods['validate-one-required'];
delete Validation.methods['validate-one-required-by-name'];
// Remove custom datetime validators
for (var methodName in Validation.methods) {
if (methodName.match(/^validate-datetime-.*/i)) {
delete Validation.methods[methodName];
}
}
if (this.validator.validate()) {
if (url) {
this.form.action = url;
}
this.form.submit();
}
Object.extend(Validation.methods, nv);
}
}.bind(productAddToCartForm);
//]]>
</script>
</div>
<div class="product-collateral toggle-content tabs">
<?php if ($detailedInfoGroup = $this->getChildGroup('detailed_info', 'getChildHtml')):?>
<dl id="collateral-tabs" class="collateral-tabs">
<?php foreach ($detailedInfoGroup as $alias => $html):?>
<dt class="tab"><span><?php echo $this->escapeHtml($this->getChildData($alias, 'title')) ?></span></dt>
<dd class="tab-container">
<div class="tab-content"><?php echo $html ?></div>
</dd>
<?php endforeach;?>
</dl>
<?php endif; ?>
</div>
<?php echo $this->getChildHtml('upsell_products') ?>
<?php echo $this->getChildHtml('product_additional_data') ?>
</div>
Ok You posted the view.phtml file. That's detail page of the product. So you're in the product DETAILS page. You want to look the file that is in the same folder
list.phtml
<?php
<?php
/**
* Product list template
*
* #see Mage_Catalog_Block_Product_List
*/
?>
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php echo $this->getToolbarHtml();?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ol class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
<li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
<?php // Product Image ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<?php // Product description ?>
<div class="product-shop">
<div class="f-fix">
<?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product) ?>
<?php endif; ?>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if($_product->isSaleable()): ?>
<p><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></p>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<div class="desc std">
<?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
<?php echo $this->__('Learn More') ?>
</div>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><?php echo $this->__('Add to Wishlist') ?></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span class="separator">|</span> <?php echo $this->__('Add to Compare') ?></li>
<?php endif; ?>
</ul>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
<?php else: ?>
<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php echo $this->getPriceHtml($_product, true) ?>
<div class="actions">
<?php if($_product->isSaleable()): ?>
<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>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><?php echo $this->__('Add to Wishlist') ?></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span class="separator">|</span> <?php echo $this->__('Add to Compare') ?></li>
<?php endif; ?>
</ul>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endif; ?>
<div class="toolbar-bottom">
<?php echo $this->getToolbarHtml() ?>
</div>
Then look for this element
<ul class="products-grid" >
And within this element find
<div class="actions">
Inside of this div, look for this button
<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>
now change it to
<button type="button" title="<?php echo $this->__('View Details') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getProductUrl($_product) ?>')"><span><span><?php echo $this->__('View Details') ?></span></span></button>

How to filter related.phtml by in-stock products only in magento

How would one go about filtering magento's related products block to show only in stock products?
<?php foreach($this->getItems() as $_item): ?>
<li class="item">
<?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
<?php endif; ?>
<?php endif; ?>
<div class="product">
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
<div class="product-details">
<p class="product-name"><?php echo $this->htmlEscape($_item->getName()) ?></p>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<?php echo $this->__('Add to Wishlist') ?>
<?php endif; ?>
</div>
</div>
</li>
<?php endforeach ?>
I've uploaded my current iteration of magentos realted.phtml to github:
https://gist.github.com/anonymous/6126772
Wrap the <li> in <?php if ($_item->isSaleable()): ?> . . . <?php endif; ?>.

add products to cart from cms page magento

I have a relatively small problem but I cannot find the solution.
I have a static CMS page which shows a list of products from a category in my Magento store.
I would like to include an add to cart button and a quantity box as well so users can add each product to their shopping cart.
The problem is I have included the add to cart button but when clicked it doesn't add a product to cart. I've read that I need to include the product id into the string but I am displaying more than one product from a category dynamically using a foreach loop....
Is there a workaround or solution to my problem? Any ideas will be welcomed.
Here's the code:
<?php
$category_id = "49"; // category_id for "Accessories"
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
?>
<?php if($_productCollection->count()): ?>
<ul class="media-list pull-left">
<?php
$products = array();
foreach ($_productCollection as $_product) {
?>
<li class="media">
<a class="fancybox pull-left" href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(220, 200); ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
<img class="media-object" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(100, 100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
</li>
<div class="media-body">
<h4 class="media-heading"><a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Order'); ?> <?php echo $this->htmlEscape($_product->getName())?><?php echo $this->__('™'); ?></a></h4>
<div class="media">
<?php echo $_product->_data['short_description']; ?>
<div class="clearfix"></div>
<h6>Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h6>
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger btn-mini" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</div>
<?php } ?>
</ul>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script><?php endif; ?>
<script type="text/javascript">
$j(document).ready(function() {
$j(".fancybox").fancybox();
});
</script>
You could use querystrings to achieve this, as explained here
I am able to add a product to cart by using the following block code:
 
{{block type="catalog/product_list_related" category_id="49" template="PATH_TO_TEMPLATE_FILE"}}
Then add a a checkout button to the file and it works!
`<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?> `
<?php
$category_id = 49; // category_id for "Accessories"
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$_productCollection = $category->getProductCollection();
$_productCollection->addAttributeToSelect('*');
?>
<?php if($_productCollection->count()): ?>
<ul class="media-list pull-left">
<?php
$products = array();
foreach ($_productCollection as $_product) {
?>
<li class="media">
<a class="fancybox pull-left" href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(220, 200); ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
<img class="media-object" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(100, 100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
</li>
<div class="media-body">
<h4 class="media-heading"><a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Order'); ?> <?php echo $this->htmlEscape($_product->getName())?><?php echo $this->__('™'); ?></a></h4>
<div class="media">
<?php echo $_product->_data['short_description']; ?>
<div class="clearfix"></div>
<h6>Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h6>
<?php if($_product->isSaleable()): ?>
<form id="addtocartform_<?=$_product->getId()?>" action="<?=Mage::helper('checkout/cart')->getAddUrl($_product)?>">
<input type="hidden" value="<?=$_product->getId()?>" name="product" id="product"/>
<?php if(!$_product->isGrouped()): ?>
<input type="text" name="qty" id="qty" maxlength="12" value="1" />
<?php endif; ?>
<button type="button" class="button btn-cart" onclick="this.form.submit()"><span><span>Add To Cart</span></span></button>
</form>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</div>
<?php } ?>
</ul>
<?php endif; ?>

How to list 4 random products from a categoy in Magento

The question is pretty simple, the code below shows the items in one category. Those categories have 4 products.
However my plan is to put in those categories 20 products, and make the following code to choose randomly 4 products of those categories.
I suppose the only line I need to change is this:
$_productCollection=$this->getLoadedProductCollection();
<?php
/**
* Product list template
*
* #see Mage_Catalog_Block_Product_List
*/
?>
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php // List mode ?>
<?php $_iterator = 0; ?>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); $_columnCount=4; ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170); ?>" width="170" height="170" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<div class="actions">
<div style="clear:both;"></div>
<div>
<?php $Deal = $_product->getResource()->getAttribute('deal')->getFrontend()->getValue($_product);?>
<?php $onSale = $_product->getResource()->getAttribute('on_sale')->getFrontend()->getValue($_product);?>
<?php $hotItem = $_product->getResource()->getAttribute('hot_item')->getFrontend()->getValue($_product);?>
<?php $freeShip = $_product->getResource()->getAttribute('free_shipping')->getFrontend()->getValue($_product);?>
<?php if($Deal == 'Yes'){ ?>
<img width="91px" height="21px" alt="Deal" title="Deal" src="<?php echo $this->getSkinUrl('images/icon-deal.gif') ?>" oncontextmenu="return false" >
<?php } ?>
<?php if($onSale == 'Yes'){ ?>
<img width="91px" height="21px" alt="On Sale" title="On Sale" src="<?php echo $this->getSkinUrl('images/icon-sale.gif') ?>" oncontextmenu="return false" >
<?php } ?>
<?php if($hotItem == 'Yes'){ ?>
<img width="91px" height="21px" alt="Hot Item" title="Hot Item" src="<?php echo $this->getSkinUrl('images/icon-hot.gif') ?>" oncontextmenu="return false" >
<?php } ?>
<?php if($freeShip == 'Yes'){ ?>
<img alt="Free Shipping" width="91px" height="21px" title="Free Shipping" src="<?php echo $this->getSkinUrl('images/icon-freeship.gif') ?>" oncontextmenu="return false" >
<?php }?>
</div>
</div>
<?php echo $this->getPriceHtml($_product, true) ?>
<div class="actions">
<?php if($_product->isSaleable()): ?>
<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>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<?php /*?><ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><?php echo $this->__('Add to Wishlist') ?></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span class="separator">|</span> <?php echo $this->__('Add to Compare') ?></li>
<?php endif; ?>
</ul><?php */?>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
</div>
<?php endif; ?>
Try this:
<?php
// $_productCollection = $this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
$_category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addCategoryFilter($_category)
->setVisibility(array(2,3,4));
$_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$_productCollection->setPage(1, 4);
?>

Categories