1

Rather then having input box I would like to place dropdown for product qty to add to cart. so to do I've done 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 product to the cart.

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

  • Have you solved your issue using code? – Rakesh Jesadiya Jan 5 '17 at 8:39
3

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; ?>
  • 1
    how to get dropdown values depends upon the current product qty – Mano M Dec 18 '17 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; ?>

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.