3

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?

2

2 Answers 2

5

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; ?>
2
  • 2
    how to get dropdown values depends upon the current product qty
    – Mahi M
    Dec 18, 2017 at 8:38
  • Please tell me how to do the same in the update cart page? May 16, 2020 at 8:38
0

Please use the below code to get the dropdown value based on available qty for simple product:-

<?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 class="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 <= $_product->getExtensionAttributes()->getStockItem()->getQty()) { ?>
                        <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; ?>
1
  • I just created custom theme and my theme directory look like app/design/frontend/MyTheme/CustomTheme how to add above file into it? @Purushotam Sharma
    – zus
    Mar 9, 2020 at 6:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.