0

We currently display product quantities in select box <select><option> type inputs and limit the select dropdown to a maximum Qty of 30. This has been working fine for us however we want to start using increments with larger Qty values for instance a max of 1000 with increments of up to 200. However this creates an issue for our products with smaller increments e.g. increments of 2 where the select options are now excessively long e.g. 2 - 1000. I know we can simply limit the max allowed quantity for those specific products but we want to avoid placing restrictions on the max order quantity and just limit the number of select options shown if possible. Then if a customer needs to they can always increase the quantity at checkout where we use the regular text input qty field.

Here is our code so far:

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if((!$_product->isGrouped()) && (!$_product->isConfigurable())) { ?>
<?php if ( Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getEnableQtyIncrements() ){ 
       $inStockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
       $QtyIncrements = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getMinSaleQty();
}
?>
<?php } ?>
<?php if($_product->isSaleable() && ($inStockQty >= $QtyIncrements)): ?>
<div class="add-to-cart">
<?php if((!$_product->isGrouped()) && (!$_product->isConfigurable())) { ?>
        <label for="qty"><?php echo $this->__('Qty:') ?></label>
        <?php if ( Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getEnableQtyIncrements() ){ ?>
        <div class="qty-holder" style="width: auto;">
        <select class="input-text qty" name="qty" id="qty">
            <?php $i = (Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getMinSaleQty()?Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getMinSaleQty():1);  ?>
            <?php $inStockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
                   if  ($inStockQty < 30) {
                      $maxDropdownQty = $inStockQty;
                   } else {
                     $maxDropdownQty = 30;
                   }
             ?>      
            <?php do { ?>
            <option value="<?php echo $i?>">
                <?php echo $i?>
                <?php $i+= Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQtyIncrements() ?>
            </option>

            <?php } while ($i <= $maxDropdownQty) ?>
        </select>
        </div> <!-- closing qty div holder -->

         <?php } else { ?>

        <div class="qty-holder">

            <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
                <div class="qty-changer">
                    <a href="javascript:void(0)" class="qty_inc"><i class="icon-up-dir"></i></a>
                    <a href="javascript:void(0)" class="qty_dec"><i class="icon-down-dir"></i></a>
                </div>

        </div> <!-- closing qty div holder -->


        <?php } ?>


        <?php }  else { ?>

        <?php if ( Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getEnableQtyIncrements() ){ ?>
        <select class="input-text qty" name="qty" id="qty">
            <?php $i = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQtyIncrements(); ?>
              <?php  $maxDropdownQty = 30;
             ?>      
            <?php do { ?>
            <option value="<?php echo $i?>">
                <?php echo $i?>
                <?php $i+= Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQtyIncrements() ?>
            </option>

            <?php } while ($i <= $maxDropdownQty) ?>
        </select>
    <?php } else { ?>
        <label for="qty"><?php echo $this->__('Qty:') ?></label>
        <div class="qty-holder">
            <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
                <div class="qty-changer">
                    <a href="javascript:void(0)" class="qty_inc"><i class="icon-up-dir"></i></a>
                    <a href="javascript:void(0)" class="qty_dec"><i class="icon-down-dir"></i></a>
                </div>
        </div> <!-- closing qty div holder -->

        <?php } ?>




        <?php } ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><i class="icon-cart"></i><?php echo $buttonTitle ?></span></span></button>
        <span id='ajax_loader' style='display:none'><i class="ajax-loader small animate-spin"></i></span>
        <?php echo $this->getChildHtml('', true, true) ?>

</div>
<?php endif; ?>

How can we limit the number of options for our smaller increment values where they are increment of 10 or less please?

e.g. (not working code)

where $QtyIncrements >= 10 limit options to 10
0

Not sure if this the correct or most efficient way but for the time being i have accomplished this with additional if statements

<div class="qty-holder" style="width: auto;">
<select class="input-text qty" name="qty" id="qty">
    <?php $i = (Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getMinSaleQty()?Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getMinSaleQty():1);  ?>
    <?php $inStockQty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
           if  ($inStockQty < 30) {
              $maxDropdownQty = $inStockQty;
           } else if ($i <= 2) {
             $maxDropdownQty = 10;
           } else if ($i <= 3) {
             $maxDropdownQty = 20;
           } else if ($i <= 4) {
             $maxDropdownQty = 20;
           } else if ($i <= 5) {
             $maxDropdownQty = 25;
           } else {
             $maxDropdownQty = 30;
           }
     ?>      
    <?php do { ?>
    <option value="<?php echo $i?>">
        <?php echo $i?>
        <?php $i+= Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQtyIncrements() ?>
    </option>

    <?php } while ($i <= $maxDropdownQty) ?>
</select>
</div> 
|improve this answer|||||

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.