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>