简体   繁体   中英

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...

<?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') ?>&nbsp;<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="150" height="125" 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>

<form action="<?php echo $this->getAddToCartUrl($_item) ?>" method="post">
       <fieldset>
        <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($_item)->getMaxSaleQty()) ?>
</select>

            <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>
       </fieldset>
</form>
                        <?php endif; ?>
                        <?php echo $this->getPriceHtml($_item, true, '-related') ?>
                        <a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a>


                    </label>
                    </div>
                </div>
        <?php endforeach ?>
        </div>
    </div>
  <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>
</div>
<?php endif ?>

Got this to work, for anyone trying to add related products on a CMS page use this 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') ?>&nbsp;<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM