0

我正在使用 CMS 页面来展示一个主要产品和一系列配件。我遇到的问题是,当我从配件部分将产品添加到购物车时,它没有被添加。相反,主要产品被添加到购物车中。

我已设法将问题隔离到主要产品的添加到表单中:

<?php
    $_product = $this->getProduct();
    $_helper = $this->helper('catalog/output');
?>
<div class="pull-right">
<form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?> class="form">
 <h2 class="product-name"><?php echo $this->__('Price:'); ?> <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h2>
         <?php if($_product->isSaleable()): ?>
    <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($_product)->getMaxSaleQty()) ?>
</select>
            <button type="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>
        <?php endif; ?> 
</form></div>
<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>

这是配件的代码 - 提交表单与上面的相同:

<?php 
$category_id = "49"; // category_id for "Accessories"
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
?>
<?php if($_productCollection->count()): ?>

       <?php 
       $products = array();
       foreach ($_productCollection as $_product) { 
        ?>
<form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?> class="form">
    <div class="media">
          <a class="fancybox static-thumbs pull-left" href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(500, 450); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
            <img class="media-object" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150, 125); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
          </a>
    <div class="media-body span6">
       <h4 class="media-heading"><a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Order'); ?> <?php echo $this->htmlEscape($_product->getName())?><?php echo $this->__('&#8482;'); ?></a></h4>

        <p>
            <?php echo $_product->_data['short_description']; ?>
        </p>
      </div>
      </div>
      <div class="media pull-right"> 
        <h2 class="product-name"><?php echo $this->__('Price:'); ?> <?php echo Mage::helper('core')->currency($_product->getPrice()); ?></h2><?php if($_product->isSaleable()): ?>

  <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($_product)->getMaxSaleQty()) ?>
</select>

            <button type="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>
        <?php endif; ?> 
        <?php } ?>

    </div></form>

<?php endif; ?>
<hr />
<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>

如果我删除主要产品添加到购物车的表格,则附件已成功添加。我不知道如何调试这个,请给一些建议!?!?

4

1 回答 1

0

不得不求助于使用立即订购按钮将客户带到产品视图页面。这意味着他们也可以在该页面上配置产品!

于 2012-12-21T21:50:27.547 回答