rafique12
asked on
Add products to cart from CMS page using foreach?
I am trying to find a way to add products to cart using from a CMS page using a foreach loop which displays the list of products from a specific category. The problem is that my add to cart form will only submit one of the products to the cart.
Could anyone point me in the right direction, I've been here for hours!
I'm not sure if it is my form or if this is even possible in Magento but I'd appreciate som advice...
Could anyone point me in the right direction, I've been here for hours!
I'm not sure if it is my form or if this is even possible in Magento but I'd appreciate som advice...
<?php
$category_id = "49"; // category_id for "products"
$_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()): ?>
<?php
$products = array();
foreach ($_productCollection as $_product) {
?>
<form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multiadd/form-data"<?php endif; ?> class="form">
<a class="fancybox static-thumbs pull-left" href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(500, 450); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" 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(150, 125); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
<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>
<p>
<?php echo $_product->_data['short_description']; ?>
</p>
<h2 class="product-name"><?php echo $this->__('Price:'); ?> <?php echo Mage::helper('core')->currency($_product->getPrice()); ?></h2><?php if($_product->isSaleable()): ?>
<p class="product-name"><?php echo $this->__('Quantity:'); ?></p>
<select name="qty" class="span1">
<?php $i = 1 ?>
<?php do { ?>
<option value="<?php echo $i?>">
<?php echo $i?>
<?php $i++ ?>
</option>
<?php } while ($i <= (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getMaxSaleQty()) ?>
</select>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger" onclick="productAddToCartForm.submit(this)"><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 } ?>
</form>
<?php endif; ?>
<hr />
<script type="text/javascript">
$j(document).ready(function() {
$j(".fancybox").fancybox();
});
</script>
<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'];
if (this.validator.validate()) {
if (url) {
this.form.action = url;
}
this.form.submit();
}
Object.extend(Validation.methods, nv);
}
}.bind(productAddToCartForm);
//]]>
</script>
ASKER
Hi,
Thankyou for your response. Although the code you posted is correct I am trying to use a slightly different method.
I have used catalog_product_list_relat ed and included an add to cart button and dropdown quantity chooser.
The problem now is that I cannot add more than on +1 item to the cart even if I select a quantity of 5. However, only the last product in the list allows me to choose a quantity....
Any idea why?
Thankyou for your response. Although the code you posted is correct I am trying to use a slightly different method.
I have used catalog_product_list_relat
The problem now is that I cannot add more than on +1 item to the cart even if I select a quantity of 5. However, only the last product in the list allows me to choose a quantity....
Any idea why?
ASKER
Updated code:
<?php if($this->getItems()->getSize()): ?>
<div class="block block-related">
<div class="block-title">
<strong><span><?php echo $this->__('Related Products') ?></span></strong>
</div>
<div class="block-content">
<p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or add to your wishlist') ?> <br /></p>
<div class="form-horizontal">
<?php foreach($this->getItems() as $_item): ?>
<div class="control-group">
<label for="related-checkbox<?php echo $_item->getId() ?>" class="control-label">
<a class="fancybox static-thumbs pull-left" href="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(500, 450); ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" ><img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(135, 135) ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
</label>
<div class="controls">
<label class="checkbox">
<?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; ?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="pull-right" title="<?php echo $this->__('Add to Wishlist') ?>" rel="tooltip"><span class="icon-check"></span></a>
<p class="product-name span6">
<a href="<?php echo $_item->getProductUrl() ?>" class="product-title"><?php echo $this->htmlEscape($_item->getName()) ?></a>
<br />
<?php echo nl2br($this->getProduct()->getDescription()) ?>
</p>
<form action="<?php echo $this->getAddToCartUrl($_item) ?>" method="post">
<fieldset>
<label class="product-name"><?php echo $this->__('Quantity:'); ?></label>
<select name="qty" class="span1">
<?php $i = 1 ?>
<?php do { ?>
<option value="<?php echo $i?>">
<?php echo $i?>
<?php $i++ ?>
</option>
<?php } while ($i <= (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getMaxSaleQty()) ?>
</select>
<button class="btn btn-danger"><span><?php echo $this->__('Add to Cart') ?></span></button>
</fieldset>
</form>
<?php endif; ?>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
</label>
</div>
</div>
<hr />
<?php endforeach ?>
</div>
</div>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, essentially you are using the code I posted by using the getAddToCartUrl(). It produces code very similar to what I posted it just uses it as a form action.
I'm not going to object but in the spirit of this site, you should have accepted my response instead of requesting deletion.
I'm not going to object but in the spirit of this site, you should have accepted my response instead of requesting deletion.
ASKER
I can now add related products to the shopping cart from a CMS page
Open in new window