3

How do I change the Quantity text input box to a dropdown on configurable products?

I've got this piece of code in my /catalog/product/view/addtocart.phtml file

 <label for="qty"><?php echo $this->__('Quantity') ?></label>
    <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>
    </div>
    <?php endif; ?>

This works well for the simple products, showing 1 to however many items I have in stock. But on configurable products, it only shows '1' in the dropdown. (likely because the configurable product itself doesn't have inventory)

I saw this stackexchange page, but haven't been able to adapt it to what I want.

Edit: Here's a use case -

Say I've got boots available in four sizes.

Boots:
Size 6 - 3 pairs
Size 7 - 2 pairs
Size 8 - 1 pair
Size 9 - 4 pairs

When I select "Size 6," I'd like the Quantity dropdown to be 1-3. If I select size 9, it should go up to 4.

2
  • What behaviour do you expect for configurable products? Baring in mind the quantity will likely be different for each simple product associated with it. – Peter O'Callaghan Feb 10 '14 at 21:17
  • Added a little more text above. Best results would be that on a configurable product, the dropdown would show the number available for the selected option. I.e. If i've got five large shirts, the dropdown will show 1-5. But if I change the size option to small (and have three in stock), it'll show 1-3. That make better sense? – SamC Feb 10 '14 at 21:38
3

First thing's first:

  • At present your current code is re-querying the database for the inventory information on every iteration of the loop. Don't call Mage::getModel in a loop... ever
  • With that in mind, you probably don't want to ask Magento to get a model inside the template file either. So you probably want to add some method to your block to get the maximum quantity available in the database and then stop creating <option>s when you reach that limit.

Assuming the above is true you should create a custom module with a custom block. The template file you're currently editing should be modified to read as such:

<?php echo $this->getChildHtml('quantityoptionsdropdown');?>

Then in your layout xml (possibly local.xml) you'll have to define that alias block:

<catalog_product_view>
    <block type="yourcompany/options" template="yourcompany/options.phtml" name="quantityoptionsdropdown" as="quantityoptionsdropdown"/>
</catalog_product_view>

Notice that we used yourcompany/options there - which should map to your custom module's block class named Options. So the full class path (at least for this example) would be something like YourCompany_YourModule_Block_Options.

So this is what your block class would look like:

app/code/local/YourCompany/YourModule/Block/Options.php

class YourCompany_YourModule_Block_Options extends Mage_Core_Block_Abstract
{
    public function getStockMaximumQty()
    {
        $product = $this->getProduct();

        return $this->getProduct()->getStockItem()->getQty();
    }

    public function getProduct()
    {
        if(!$this->product){
            $this->product = Mage::registry('current_product');
        }
        return $this->product;
    }
}

Now, your template file:

app/design/frontend/base/default/template/yourcompany/options.phtml

<?php if($this->getStockMaximumQty()>0): ?>
    <? $i = 0; ?>
    <select name="quantity" id="qty">
        <?php while($i<=$this->getStockMaximumQty():?>
        <option value="<?php echo $i; ?>"><?php echo $i++; ?></option>
        <?php endwhile; ?>
    </select>
<?php else: ?>
    <?php echo $this->__('This product is currently out of stock.'); ?>
<?php endif; ?>
3
  • Oh, let me clarify, & I'll edit my text above. I'm not looking to change the increment. I want to change the max number available in the dropdown when I select an option on a configurable product. So if I've got three pairs in stock of size 9 chukka boots, I want the dropdown to show 1, 2 & 3. But if I change the size option to be 'size 10' (which has 4 pairs in stock) adjust the dropdown to show 1, 2, 3, & 4. – SamC Feb 10 '14 at 21:32
  • See edit. I didn't bother to check if the <select> name and ID are correct so you'll have to mess with that. Hope that helps. – philwinkle Feb 10 '14 at 22:10
  • qhat about editing the product after it has been dropped in the cart? How would you initialize the "selected" <option>? – lrkwz Jul 10 '14 at 13:03

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