Question

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.

Was it helpful?

Solution

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; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top