0

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.

2

This happens because the configurable product does not have minimum sale qty and a total qty.
You need to get the simple product that is represented by the configurable product and get the values from it. Try this:

$simpleProduct = null;
foreach ($_item->getQuote()->getAllItems() as $simpleItem){
   if ($simpleItem->getParentId() == $_item->getId()){
       $simpleProduct = $simpleItem->getProduct();
       break;
   }
}
if ($simpleProduct){
   //work your magic with $simpleProduct
}

I didn't test, but it seams like the way to go.
I've tested and this works for me. I was close. Instead of getParentId() it should be getParentItemId(). Here is the code from my checkout/cart/item/default.phtml file:

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

I'm almost sure that there is an other way of getting the simple product and avoid the foreach loop but this will do for now.

  • Hi Marius. I have tried as per your suggestion but the code is not working. $simpleItem->getParentId() doesn't return anything. And I don't understand why you added if ($simpleItem->getParentId() == $_item->getId()) condition. – Jaimin Sutariya Oct 3 '13 at 9:57
  • @JaiminSutariya Like I said. This was of the top of my head. It seamed like the way to go. I will test and get back to you on this. – Marius Oct 3 '13 at 10:28
  • @JaiminSutariya. See my edit. – Marius Oct 3 '13 at 10:47
  • due to getModel being called for every item, this script uses a lot of resources when there's around 10+ different products in cart – Ricky Odin Matthews Feb 16 '16 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.