-1

我为我的 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>
4

1 回答 1

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

于 2012-12-13T13:20:31.437 回答