Magento – Dropdown for add to cart qty in Magento 2

addtocartcartdrop-downsmagento-2.1.2

Rather than having input box, I would like to place dropdown for product qty to add to cart. So to do this, I've done the following:

In my

\app\design\frontend\Vendor\Theme\Magento_Catalog\templates\product\view\addtocart.phtml

I've replaced following code instead of input box.

<select name="qty" id="qty" title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" 
       class="input-text qty" data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>">
       <?php $i = 1 ; ?>
        <?php 
         while( $i < 5) { ?>
            <option value="<?php echo $block->getProductDefaultQty() * $i; ?>"><?php echo $block->getProductDefaultQty() * $i; ?></option>
            <?php $i++; ?>
         <?php } ?>
</select>
<button type="submit"
    title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
    class="btn btn-orange"
    id="product-addtocart-button">
    <span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
</button>

But it's not adding the product into the cart.

Can anyone help me to build dropbox instead of textbox for adding product to the cart?

Best Answer

Replace addtocart.phtml with below code,

File PATH: app/design/frontend/{Vendor}/{themename}/Magento_Catalog/templates/product/view/addtocart.phtml

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()): ?>
        <div class="field qty">
            <label class="label" for="qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>               
            <select name="qty" id="qty" title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" 
                   class="input-text qty" data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>">
                   <?php $i = 1 ; ?>
                    <?php 
                     while( $i < 5) { ?>
                        <option value="<?php echo $block->getProductDefaultQty() * $i; ?>"><?php echo $block->getProductDefaultQty() * $i; ?></option>
                        <?php $i++; ?>
                     <?php } ?>
            </select>

        </div>
        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
            </button>
            <?php echo $block->getChildHtml('', true) ?>
        </div>
    </div>
</div>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "catalogAddToCart": {
                "bindSubmit": true
            }
        }
    }
</script>
<?php endif; ?>