Sunday, October 25, 2015

Set Product Quantity Dropdown Instead of Textbox in Magento


Maganto is an open source eCommerce platform for creates shopping based website. Magento also provides community and enterprise edition.community edition is free for download and easy to use for storing many products and categories.
If you use Magento very well than must know that Magento provides a product quantity selection textbox for selecting product quantity. when you add that products in the shopping cart then you can see in the chart, there is also a textbox for change product quantity. So, according to website design sometimes it's important to change that text box and replace product quantity drop-down. Then it's easy to change the textbox to selection box with some changes in below files.

1. app/design/frontend/{THEME}/default/template/catalog/product/view/addtocart.phtml
See input box for quantity and replace that to selection dropdown for that find

<input type="text" pattern="\d*" name="qty" id="qty" maxlength="12" value="<?php echo max($this->getProductDefaultQty() * 1, 1) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" />
Replace with

<select name="qty" id="qty" class="input-text qty">
<?php
        //for get current product stock.
 $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
 for($i = 1;$i<=$stock->getQty();$i++)
 {
 if($this->getProductDefaultQty() == $i){$sel = 'selected="selected"';}else{$sel = '';}
 echo '<option '.$sel.' value="'.$i.'">'.$i.'</option>';
 }
?>
</select>

Here change input box according to product type here below code only for simple product type change associate product type files according to product type.
for getting current product stock use below code as total quantity.

$qunt = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getProduct())->getQty();

This is changes for add dropdown on product details page for changes in shopping cart page change in below file.

2.  app/design/frontend/{THEME}/default/template/checkout/cart/item/default.phtml
See input box for quantity in table data item.find below code

<input type="text" pattern="\d*" name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $this->getQty() ?>" size="4"
 data-cart-item-id="<?php echo $this->jsQuoteEscape($_item->getSku()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" maxlength="12" />
Replace with

<select name="cart[<?php echo $_item->getId() ?>][qty]" id="qty" class="input-text qty">
<?php
 //for get current product stock.
 $productsstk = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($this->getProduct())->getQty();
 for($i = 1;$i<=$productsstk;$i++)
 {
  if($this->getQty() == $i){$sel = 'selected="selected"';}else{$sel = '';}
  echo '<option '.$sel.' value="'.$i.'">'.$i.'</option>';
 }
?>
</select>
 
According to below files changes product quantity textbox replaced with dropdown you can apply the same change for any other files like wishlist,my cart column for setting product quantity dropdown instead of the textbox.

1 comment :

  1. This blog is very informative. Packing can be a long hectic process but it can be made easier by following the tips that you have mentioned above. Thanks for sharing such important and useful information. Keep it up.
    visit mentioned website for printing services.
    Custom macaron boxes
    Vape Cart Boxes
    Disposable Vape Boxes
    CBD Display Box

    ReplyDelete