1

I want change qty box in dropdown .So I'm override template file./vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml

<referenceBlock name="product.info.addtocart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Www_QtyDropdown::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock> 

addtocart.phtml

    <?php $_product = $block->getProduct(); ?>
<?php $qty = $block->getStockMaximumQty(); ?>
<?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><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
                    <div class="control">
                        <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 if ($qty > 4) { ?>
                                <option value = "0"></option>
                                <?php $i = 1; ?>                                
                                <?php while ($i <= 4) {
                                    ?>                            
                                    <option value="<?php echo $block->getProductDefaultQty() * $i; ?>"><?php echo $block->getProductDefaultQty() * $i; ?></option>
                                    <?php $i++; ?>
                                <?php } ?>
                            <?php } else {
                                ?>
                                <option value = "0"></option>
                                <?php $i = 1;
                                ?>
                                <?php while ($i <= $qty) { ?>
                                    <option value="<?php echo $block->getProductDefaultQty() * $i; ?>"><?php echo $block->getProductDefaultQty() * $i; ?></option>
                                    <?php $i++; ?>
                                <?php } ?>
                            <?php } ?>
                        </select>
                    </div>
                </div>
            <?php endif; ?>

But is not working for configurable product.If I need any modification in my code.

5
  • Have you checked the applied conditions in case configurable products? They should be true for them. Otherwise it should work fine. Commented Dec 23, 2017 at 12:21
  • How to check it....@Himmit
    – Mahi M
    Commented Dec 23, 2017 at 12:24
  • I meant; check the conditions <?php if ($_product->isSaleable()): ?> and <?php if ($block->shouldRenderQuantity()): ?> check if the value of $_product->isSaleable() and $block->shouldRenderQuantity() is not 0 for configurable products. Commented Dec 23, 2017 at 12:31
  • i'm added this code in my template <?php if ($_product->isSaleable() && $block->shouldRenderQuantity()): ?>.still not working
    – Mahi M
    Commented Dec 23, 2017 at 12:39
  • both fun r return true
    – Mahi M
    Commented Dec 23, 2017 at 12:42

1 Answer 1

0

Ok, please try adding below code in catalog_product_view.xml:

<referenceBlock name="product.info.addtocart.additional">
       <action method="setTemplate">
          <argument name="template" xsi:type="string">Www_QtyDropdown::product/view/addtocart.phtml</argument>
       </action>
</referenceBlock> 
7
  • Already done it ....
    – Mahi M
    Commented Dec 23, 2017 at 12:52
  • it only gives dropdown....But the qty values not fit here
    – Mahi M
    Commented Dec 23, 2017 at 12:53
  • Are you able to see dropdown? Commented Dec 23, 2017 at 12:55
  • For your information, in case configurable products, the parent product don't have qty. The qty is managed for product configurable options/child products. Commented Dec 23, 2017 at 12:58
  • Yes .But i trying to get child product id...I'm able to get all child product of configurable product.I don't know ..how to get selected child product id .....
    – Mahi M
    Commented Dec 23, 2017 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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