Reputation: 252
Hello
I am using magento 1.7.0.2 CE. I need to display a dropdown for product quantity to allow a user to select quantity from the dropdown on cart page. I have added a code in checkout/cart/item/default.phtml
for the same which is,
<?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>
This code displays the dropdown correctly for simple products. But when I add configurable product to my cart, It displays me dropdown without any option to select.
Can anyone help me with it? Thanks in Advance.
Upvotes: 0
Views: 4078
Reputation: 447
I've tested and this works for me.
$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>
Enjoy :)
Upvotes: 6
Reputation: 11
Step :1 Go to app/design/frontend/base/default/template/catalog/product**/view/addtocart.phtml
In the addtocart.phtml file find the following code(around line 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>
Hope you'll solve your problem.
Upvotes: 0