title: Add products to cart from CMS page using foreach?
body: <p>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.</p>
  
  <p>Could anyone point me in the right direction, I've been here for hours!</p>
  
  <p>I'm not sure if it is my form or if this is even possible in Magento but I'd appreciate som advice...</p>
  
  <pre><code>&lt;?php if($this-&gt;getItems()-&gt;getSize()): ?&gt;
  &lt;div class="block block-related"&gt;
      &lt;div class="block-title"&gt;
          &lt;strong&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Related Products') ?&gt;&lt;/span&gt;&lt;/strong&gt;
      &lt;/div&gt;
      &lt;div class="block-content"&gt;
          &lt;p class="block-subtitle"&gt;&lt;?php echo $this-&gt;__('Check items to add to the cart or add to your wishlist') ?&gt;&amp;nbsp;&lt;br /&gt;&lt;/p&gt;
          &lt;div class="form-horizontal"&gt;
          &lt;?php foreach($this-&gt;getItems() as $_item): ?&gt;
  
                  &lt;div class="control-group"&gt;
                      &lt;label for="related-checkbox&lt;?php echo $_item-&gt;getId() ?&gt;" class="control-label"&gt;
                          &lt;a class="fancybox static-thumbs pull-left" href="&lt;?php echo $this-&gt;helper('catalog/image')-&gt;init($_item, 'small_image')-&gt;resize(500, 450); ?&gt;" title="&lt;?php echo $this-&gt;htmlEscape($_item-&gt;getName()) ?&gt;" &gt;&lt;img src="&lt;?php echo $this-&gt;helper('catalog/image')-&gt;init($_item, 'small_image')-&gt;resize(135, 135) ?&gt;" width="150" height="125" alt="&lt;?php echo $this-&gt;htmlEscape($_item-&gt;getName()) ?&gt;" /&gt;&lt;/a&gt;
                      &lt;/label&gt;
  
                      &lt;div class="controls"&gt;
                      &lt;label class="checkbox"&gt;
                          &lt;?php if(!$_item-&gt;isComposite() &amp;&amp; $_item-&gt;isSaleable()): ?&gt;
                              &lt;?php if (!$_item-&gt;getRequiredOptions()): ?&gt;
                                  &lt;input type="checkbox" class="checkbox related-checkbox" id="related-checkbox&lt;?php echo $_item-&gt;getId() ?&gt;" name="related_products[]" value="&lt;?php echo $_item-&gt;getId() ?&gt;" /&gt;
                              &lt;?php endif; ?&gt;
                          &lt;?php endif; ?&gt;
                                                          &lt;?php if ($this-&gt;helper('wishlist')-&gt;isAllow()) : ?&gt;
                              &lt;a href="&lt;?php echo $this-&gt;getAddToWishlistUrl($_item) ?&gt;" class="pull-right" title="&lt;?php echo $this-&gt;__('Add to Wishlist') ?&gt;" rel="tooltip"&gt;&lt;span class="icon-check"&gt;&lt;/span&gt;&lt;/a&gt;
  
  &lt;form action="&lt;?php echo $this-&gt;getAddToCartUrl($_item) ?&gt;" method="post"&gt;
         &lt;fieldset&gt;
          &lt;p class="product-name"&gt;&lt;?php echo $this-&gt;__('Quantity:'); ?&gt;&lt;/p&gt;
    &lt;select name="qty" class="span1"&gt;
    &lt;?php $i = 1 ?&gt;
    &lt;?php do { ?&gt;
      &lt;option value="&lt;?php echo $i?&gt;"&gt;
        &lt;?php echo $i?&gt;
        &lt;?php $i++ ?&gt;
      &lt;/option&gt;
      &lt;?php } while ($i &lt;= (int)Mage::getModel('cataloginventory/stock_item')-&gt;loadByProduct($_item)-&gt;getMaxSaleQty()) ?&gt;
  &lt;/select&gt;
  
              &lt;button title="&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;" class="btn btn-danger" onclick="productAddToCartForm.submit(this)"&gt;&lt;span&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;
  
                  &lt;?php else: ?&gt;
              &lt;p class="availability out-of-stock"&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Out of stock') ?&gt;&lt;/span&gt;&lt;/p&gt;
         &lt;/fieldset&gt;
  &lt;/form&gt;
                          &lt;?php endif; ?&gt;
                          &lt;?php echo $this-&gt;getPriceHtml($_item, true, '-related') ?&gt;
                          &lt;a href="&lt;?php echo $_item-&gt;getProductUrl() ?&gt;"&gt;&lt;?php echo $this-&gt;htmlEscape($_item-&gt;getName()) ?&gt;&lt;/a&gt;
  
  
                      &lt;/label&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
          &lt;?php endforeach ?&gt;
          &lt;/div&gt;
      &lt;/div&gt;
    &lt;script type="text/javascript"&gt;
      $j(document).ready(function() {
          $j(".fancybox").fancybox();
      });
  &lt;/script&gt;
  
   &lt;script type="text/javascript"&gt;
      //&lt;![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 &amp;&amp; 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);
      //]]&gt;
      &lt;/script&gt;
  &lt;/div&gt;
  &lt;?php endif ?&gt;
  </code></pre>