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.
Best Answer
First thing's first:
Mage::getModel
in a loop... ever<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:
Then in your layout xml (possibly local.xml) you'll have to define that alias block:
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 likeYourCompany_YourModule_Block_Options
.So this is what your block class would look like:
app/code/local/YourCompany/YourModule/Block/Options.php
Now, your template file:
app/design/frontend/base/default/template/yourcompany/options.phtml