简体   繁体   English

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

[英]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. 我正在尝试找到一种方法,可以使用foreach循环从CMS页面使用产品添加到购物车,该循环显示特定类别的产品列表。 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... 我不确定这是我的表格还是在Magento中甚至可以,但是我很乐意为您提供建议。

<?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: 使此方法有效,对于任何试图在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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