Magento中的数量下拉选择器仅提交一个产品

时间:2012-12-12 16:42:39

标签: php magento

我已为我的Magento商店整合了一个新的CMS产品页面,并希望使用下拉数量选择器供用户选择他们想要购买的商品数量。

到目前为止,我已在页面上显示数量选择器,但由于某种原因,它无法按预期运行。

1)我已将可购买的产品数量设置为最大值5,但数量选择器显示库存中的数量(当前设置为1000!)

2)当我选择一个大于1的金额时,只有1个产品被添加到购物篮

我一直在努力调整和修改代码,但我无法让它正常工作。如果有人知道我应该如何做到这一点,我会很感激你的抬头!

以下是代码:

<?php 
$category_id = "49"; // category_id for "Products"
$_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) { 
        ?>

    <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 span5">
       <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 span2"> 
        <h2 class="product-name">Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h2>
         <?php if($_product->isSaleable()): ?>
         <select name="qty">
            <?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)->getQty()) ?>
        </select>
        <div class="clearfix"></div>
            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-danger" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><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>

<?php endif; ?>
<script type="text/javascript">
    $j(document).ready(function() {
        $j(".fancybox").fancybox();
    });
</script>

1 个答案:

答案 0 :(得分:0)

如果有人遇到类似问题,您需要使用以下代码:

<?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 span5">
       <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 span2"> 
        <h2 class="product-name">Price <?php echo Mage::helper('core')->currency($_product->getPrice());; ?></h2>
         <?php if($_product->isSaleable()): ?>
   <select name="qty">
  <?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>
        <div class="clearfix"></div>
            <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>
</div>
<?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>

请务必添加多部分表单和javascript !!!