如何在购物车中为可配置产品添加数量下拉菜单?

时间:2013-10-03 09:26:33

标签: magento-1.7 checkout cart configurable-product

您好,我正在使用magento 1.7.0.2 CE。我需要显示产品数量的下拉列表,以允许用户从购物车页面上的下拉菜单中选择数量。我在checkout/cart/item/default.phtml中添加了一个代码,用于

<?php echo $min_sale_qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($this->getProduct())->getData('min_sale_qty');
 $total_qyt = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($this->getProduct())->getQty();
     ?>
    <select name="cart[<?php echo $_item->getId() ?>][qty]">
    <?php for($i = $min_sale_qty; $i <= $total_qyt; $i = $i + $min_sale_qty)
      {
      ?>
    <option value="<?php echo $i?>" <?php echo ($i == $this->getQty())? "selected=selected": ""; ?>>
      <?php echo $i?>
    </option>
   <?php }?> 
   </select>

此代码正确显示简单产品的下拉列表。但是当我将可配置产品添加到我的购物车时,它会向我显示下拉列表而没有任何选项可供选择 任何人都可以帮助我吗?在此先感谢。

2 个答案:

答案 0 :(得分:6)

我已经测试过,这对我有用。

    $simpleProduct = $this->getProduct();
    if ($this->getProduct()->getTypeId() == 'configurable') {
        foreach ($_item->getQuote()->getAllItems() as $simpleItem){
            if ($simpleItem->getParentItemId() == $_item->getId()){
                $simpleProduct = $simpleItem->getProduct();
                break;
            }
        }
    }
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleProduct);
    $min_sale_qty = (int)$stockItem->getData('min_sale_qty');
    $total_qyt = (int)$stockItem->getQty();

    ?>
    <select name="cart[<?php echo $_item->getId() ?>][qty]">
        <?php for($i = $min_sale_qty; $i <= $total_qyt; $i = $i + $min_sale_qty) : ?>
            <option value="<?php echo $i?>" <?php echo ($i == $this->getQty())? "selected=selected": ""; ?>>
                <?php echo $i?>
            </option>
        <?php endfor;?>
    </select>

享受:)

答案 1 :(得分:0)

步骤:1转到app / design / frontend / base / default / template / catalog / product ** / view / addtocart.phtml

在addtocart.phtml文件中找到以下代码(第33行)

<input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" />



Replace with this code:

This code shows the “Available Qty for Product”.
<select class="input-text qty" name="qty" id="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>


**This code shows the “Maximum Qty Allowed in Shopping Cart”.**

<select class="input-text qty" name="qty" id="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>

希望你能解决问题。