0

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

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

我不确定这是否是我的形式,或者这在 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 ?>
4

1 回答 1

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>
于 2012-12-28T10:56:16.230 回答