使用foreach从CMS页面添加产品到购物车?

时间:2012-12-26 19:17:23

标签: php javascript magento

我正在尝试使用foreach循环从CMS页面中找到一种将产品添加到购物车的方法,该循环显示特定类别的产品列表。问题是我的添加到购物车表单只会将其中一个产品提交到购物车。

有人能指出我正确的方向,我已经在这里待了好几个小时了!

我不确定这是不是我的表格,或者甚至可以在Magento中使用,但我很感激som的建议......

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

1 个答案:

答案 0 :(得分:0)

让这个工作,对于任何试图在CMS页面上添加相关产品的人都使用以下代码:

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