Restrict Quantity for Adding to Cart in Magento 1.9

backordercartmagento-1.9quantity

we needed "pre-order/Back order" feature [ user can buy out-of-stock Products ]

so we wrote custom module. how it works is , if we set value

"yes" to pre order attribute, it will allow for pre-order,

for "NO", it will not show "Pre order" button in view page.

but problem is user can enter any number of quantity & add to cart items.

what we need is what quantity we enter in backend, only it should allow that much quantity to add-to-cart.

<?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">

                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>

                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <?php if($_product->getPreOrder()): ?>
                        <button type="button" title="<?php echo $this->__('Pre-Order') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Pre-Order') ?></span></span></button>

                        <?php endif; ?>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('') ?></span></p>
                    <?php endif; ?>

                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>

complete code : http://pasted.co/8c8e0763

Best Answer

for this you can either create a attribute for product which will allow you to enter product base allowed quantity or make a field in system configuration using system.xml which will use as a global value for all product.Then on app\design\frontend{package_name}{tehme_name}\template\catalog\product\view\addtocart.phtml you can fetch that value and create one jquery function on the same file which will get fired either on keyup event or on addtocart button click which compare the entered value and the set value and if value if greater then show alert and clear the entered value.

For Ex:

where prod_qty_limit is the Attribute Code for attribute created for inserting value of product limit.

Place the following code at the bottom of addtocart.phtml

<?php
    $limit = $_product->getData('prod_qty_limit');
?>
<input type="hidden" name="quantity_limit" id="product_limit" value="<?php echo $limit?>"/>
<script>
    jQuery(document).ready(function(){
        jQuery(".qty").keyup(function(){
            var limit = parseInt(jQuery("#product_limit").val());
            var qty = parseInt(jQuery(".qty").val());
            if(qty > limit)
            {
                alert("Quantity is greater than allowed one please enter valid quantity");
                jQuery(".qty").val(1);
            }
        });
    });
</script>

hope this will help let me know if you need more info.

Related Topic